Class: Shortwave::Model::BaseModel
- Inherits:
-
Object
- Object
- Shortwave::Model::BaseModel
- Defined in:
- lib/shortwave/model/base_model.rb
Overview
Root class for all Shortwave models.
Direct Known Subclasses
Album, Artist, ChartDates, Comparison, Event, Group, Location, Playlist, Shout, Tag, Track, User, Venue
Class Method Summary collapse
- .facade_name ⇒ Object
- .identified_by(*methods) ⇒ Object
- .inherited(klass) ⇒ Object
- .link_to(klass, method, remote_method = nil) ⇒ Object
- .sharable ⇒ Object
-
.shoutable ⇒ Object
Adds methods to a model to deal with shouts: shouts and shout.
-
.taggable ⇒ Object
Adds methods to a model to deal with tags: tags, add_tags and remove_tag.
Instance Method Summary collapse
-
#session=(session) ⇒ Object
Sets the session on this model, and all nested models.
Class Method Details
.facade_name ⇒ Object
17 18 19 |
# File 'lib/shortwave/model/base_model.rb', line 17 def facade_name @facade_name ||= (name.split("::").last.downcase + "_facade").to_sym end |
.identified_by(*methods) ⇒ Object
21 22 23 |
# File 'lib/shortwave/model/base_model.rb', line 21 def identified_by(*methods) @lastfm_keys = methods end |
.inherited(klass) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shortwave/model/base_model.rb', line 75 def inherited(klass) klass.send(:include, HappyMapper) klass.send(:tag, klass.name.split("::").last.downcase) # Store every has_one/has_many declaration and use this information # to decide which attributes need a session adding class << klass def has_ones @has_ones ||= [] end def has_manys @has_manys ||= [] end def has_one(sym, *args) super has_ones << sym end def has_many(sym, *args) super has_manys << sym end end end |
.link_to(klass, method, remote_method = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/shortwave/model/base_model.rb', line 65 def link_to(klass, method, remote_method=nil) remote_method ||= method class_eval <<-EOV def #{method} response = @session.#{facade_name}.#{remote_method}(#{@lastfm_keys.join(',')}) #{klass}.parse(response).each {|obj| obj.session = @session } end EOV end |
.sharable ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/shortwave/model/base_model.rb', line 55 def sharable class_eval <<-EOV def share(recipients, message=nil) params = {} params[:message] = message if message @session.#{facade_name}.share(#{@lastfm_keys.join(",")}, recipients[0...10].map{|r| r.to_s }.join(','), params) end EOV end |
.shoutable ⇒ Object
Adds methods to a model to deal with shouts: shouts and shout
26 27 28 29 30 31 32 33 34 |
# File 'lib/shortwave/model/base_model.rb', line 26 def shoutable class_eval <<-EOV def shout(message) @session.#{facade_name}.shout(#{@lastfm_keys.join(",")}, message) end link_to "Shout", :shouts EOV end |
.taggable ⇒ Object
Adds methods to a model to deal with tags: tags, add_tags and remove_tag
Up to ten tags can be added, either strings, or Tag models A single tag can be removed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/shortwave/model/base_model.rb', line 40 def taggable class_eval <<-EOV def add_tags(*tags) tag_param = tags[0...10].map {|t| t.to_s }.join(",") @session.#{facade_name}.add_tags(#{@lastfm_keys.join(",")}, tag_param) end def remove_tag(tag) @session.#{facade_name}.remove_tag(#{@lastfm_keys.join(",")}, tag.to_s) end link_to "Tag", :my_tags, :tags EOV end |
Instance Method Details
#session=(session) ⇒ Object
Sets the session on this model, and all nested models
6 7 8 9 10 11 12 13 14 |
# File 'lib/shortwave/model/base_model.rb', line 6 def session=(session) @session = session self.class.has_ones.each do |sym| obj = send(sym) obj.session = session if obj end self.class.has_manys.each {|s| (send(s) || []).each {|o| o.session = session } } session end |