Skip to content

Instantly share code, notes, and snippets.

@rhyhann
Forked from anonymous/gist:34679
Created December 11, 2008 12:28
Show Gist options
  • Save rhyhann/34681 to your computer and use it in GitHub Desktop.
Save rhyhann/34681 to your computer and use it in GitHub Desktop.
require 'dm-migrations'
require 'migration_runner'
module DataMapper
module Types
class TagCollection < DataMapper::Type
primitive String
def self.load(value, property)
value.split(', ').to_a
end
def self.dump(value, property)
result = typecast(value, property)
result.nil? ? result : result.join(', ')
end
def self.typecast(value, property)
case value
when nil: nil
when String; value.split(', ').to_a.uniq
when Array ; value.uniq
when Set ; value.to_a
else ; raise ArgumentError.new \
"+value+ must be nil String, Set or Array"
end
end
end # class Tag
end # module Types
end # module DataMapper
class Post
property :categories, TagCollection
property :tags , TagCollection
def self.elements(type)
elements = Set.new
p all
all.each {|r| p r;p r.tags;elements.merge(r.tags)}
return elements.to_a
end
end
include Sinatra::RenderingHelpers
include Sinatra::Erb
template :thing do
<<-HAML
<% for category in Post.elements(:tags) %>
<li><%= category %></li>
<% end %>
HAML
end
Plugins::Views.menu ||= erb(:thing)
ArgumentError - Unknown property 'tags'
/usr/lib/ruby/gems/1.8/gems/dm-core-0.9.7/lib/dm-core/resource.rb:106:in `attribute_get'
/usr/lib/ruby/gems/1.8/gems/dm-core-0.9.7/lib/dm-core/model.rb:414:in `tags'
./plugins/tags.plugin.rb:36:in `elements'
/usr/lib/ruby/gems/1.8/gems/extlib-0.9.8/lib/extlib/lazy_array.rb:14:in `each'
/usr/lib/ruby/gems/1.8/gems/extlib-0.9.8/lib/extlib/lazy_array.rb:14:in `each'
./plugins/tags.plugin.rb:36:in `elements'
(haml):27:in `render'
/usr/lib/ruby/gems/1.8/gems/haml-2.1.0/lib/haml/engine.rb:150:in `render'
/usr/lib/ruby/gems/1.8/gems/haml-2.1.0/lib/haml/engine.rb:150:in `instance_eval'
/usr/lib/ruby/gems/1.8/gems/haml-2.1.0/lib/haml/engine.rb:150:in `render'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:610:in `render_haml'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:523:in `call'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:523:in `render'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:601:in `haml'
./blog.rb:57:in `send'
./blog.rb:57
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1254:in `instance_eval'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1254:in `dispatch'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1248:in `catch'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1248:in `dispatch'
/usr/lib/ruby/gems/1.8/gems/rack-0.4.0/lib/rack/commonlogger.rb:20:in `call'
/usr/lib/ruby/gems/1.8/gems/rack-0.4.0/lib/rack/commonlogger.rb:20:in `_call'
/usr/lib/ruby/gems/1.8/gems/rack-0.4.0/lib/rack/commonlogger.rb:13:in `call'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1227:in `call'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1172:in `run_safely'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1172:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1172:in `run_safely'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1225:in `call'
/usr/lib/ruby/gems/1.8/gems/rack-0.4.0/lib/rack/handler/mongrel.rb:59:in `process'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
/usr/lib/ruby/gems/1.8/gems/rack-0.4.0/lib/rack/handler/mongrel.rb:32:in `run'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:112:in `run'
/usr/lib/ruby/gems/1.8/gems/sinatra-0.3.2/lib/sinatra.rb:1461
blog.rb:85
(hy@sb) ruby blog.rb
[#<Post id=1 name="Chaft" content=<not loaded> slug="chaft" categories=["niahaha", "niohoho"] tags=["boun", "dakh"]>, #<Post id=2 name="Test floun" content=<not loaded> slug="test-floun" categories=["Morocco", "Sahara"] tags=["western sahara", "moroccan", "how"]>, #<Post id=3 name="Chaft Boun Dakh" content=<not loaded> slug="chaft-boun-dakh" categories=["ecole"] tags=["hadak eddebarh", "benomar"]>, #<Post id=4 name="Thing" content=<not loaded> slug="thing" categories=["Maroc", "Casablanca"] tags=["abc", "def", "mn", "op"]>]
#<Post id=1 name="Chaft" content=<not loaded> slug="chaft" categories=["niahaha", "niohoho"] tags=["boun", "dakh"]>
["boun", "dakh"]
#<Post id=2 name="Test floun" content=<not loaded> slug="test-floun" categories=["Morocco", "Sahara"] tags=["western sahara", "moroccan", "how"]>
["western sahara", "moroccan", "how"]
#<Post id=3 name="Chaft Boun Dakh" content=<not loaded> slug="chaft-boun-dakh" categories=["ecole"] tags=["hadak eddebarh", "benomar"]>
["hadak eddebarh", "benomar"]
#<Post id=4 name="Thing" content=<not loaded> slug="thing" categories=["Maroc", "Casablanca"] tags=["abc", "def", "mn", "op"]>
["abc", "def", "mn", "op"]
== Sinatra/0.3.2 has taken the stage on port 4567 for development with backup by Rack::Handler::Mongrel
[#<Post id=1 name="Chaft" content=<not loaded> slug="chaft">, #<Post id=2 name="Test floun" content=<not loaded> slug="test-floun">, #<Post id=3 name="Chaft Boun Dakh" content=<not loaded> slug="chaft-boun-dakh">, #<Post id=4 name="Thing" content=<not loaded> slug="thing">]
#<Post id=1 name="Chaft" content=<not loaded> slug="chaft">
127.0.0.1 - - [11/Dec/2008 11:25:36] "GET /1 HTTP/1.1" 500 3844 0.0708
127.0.0.1 - - [11/Dec/2008 11:25:36] "GET /sinatra_custom_images/500.png HTTP/1.1" 200 31056 0.0014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment