Module: Fleakr::Support::Object::ClassMethods
- Defined in:
- lib/fleakr/support/object.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #find_all(condition, options) ⇒ Object
- #find_one(condition, options) ⇒ Object
- #flickr_attribute(*names_and_options) ⇒ Object
- #has_many(*attributes) ⇒ Object
- #lazily_load(*attributes) ⇒ Object
- #scoped_search ⇒ Object
Instance Method Details
#attributes ⇒ Object
7 8 9 |
# File 'lib/fleakr/support/object.rb', line 7 def attributes @attributes ||= [] end |
#find_all(condition, options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fleakr/support/object.rb', line 39 def find_all(condition, ) attribute = [:using].nil? ? condition.to_s.sub(/^by_/, '') : [:using] target_class = [:class_name].nil? ? self.name : "Fleakr::Objects::#{[:class_name]}" class_eval <<-CODE def self.find_all_#{condition}(value, options = {}) options.merge!(:#{attribute} => value) response = Fleakr::Api::MethodRequest.with_response!('#{[:call]}', options) (response.body/'rsp/#{[:path]}').map {|e| #{target_class}.new(e, options) } end CODE end |
#find_one(condition, options) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fleakr/support/object.rb', line 53 def find_one(condition, ) attribute = [:using].nil? ? condition.to_s.sub(/^by_/, '') : [:using] class_eval <<-CODE def self.find_#{condition}(value, options = {}) options.merge!(:#{attribute} => value) response = Fleakr::Api::MethodRequest.with_response!('#{[:call]}', options) #{self.name}.new(response.body, options) end CODE end |
#flickr_attribute(*names_and_options) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/fleakr/support/object.rb', line 11 def flickr_attribute(*) attributes, = Utility.() attributes.each do |name| self.attributes << Attribute.new(name, [:from]) class_eval "attr_accessor :#{name}" end end |
#has_many(*attributes) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fleakr/support/object.rb', line 20 def has_many(*attributes) class_name = self.name attributes.each do |attribute| target = Utility.class_name_for('Fleakr::Objects', attribute) finder_attribute = Utility.id_attribute_for(class_name) class_eval <<-CODE def #{attribute}(options = {}) options = authentication_options.merge(options) sorted_options = options.sort {|a, b| a[0].to_s <=> b[0].to_s } key = '#{attribute}_' + sorted_options.to_s associations[key] ||= #{target}.send("find_all_by_#{finder_attribute}".to_sym, self.id, options) end CODE end end |
#lazily_load(*attributes) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fleakr/support/object.rb', line 80 def lazily_load(*attributes) attributes, = Utility.(attributes) attributes.each do |attribute| class_eval <<-CODE alias_method :#{attribute}_without_loading, :#{attribute} def #{attribute} self.send(:#{[:with]}) if @#{attribute}.nil? #{attribute}_without_loading end CODE end end |
#scoped_search ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fleakr/support/object.rb', line 66 def scoped_search key = Utility.id_attribute_for(self.name) class_eval <<-CODE def search(*parameters) options = {:#{key} => self.id} options.merge!(self.authentication_options) parameters << options Fleakr::Objects::Search.new(*parameters).results end CODE end |