Module: Fleakr::Support::Object::ClassMethods

Defined in:
lib/fleakr/support/object.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



7
8
9
# File 'lib/fleakr/support/object.rb', line 7

def attributes
  @attributes ||= []
end

#find_all(condition, options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fleakr/support/object.rb', line 35

def find_all(condition, options)
  attribute    = options[:using].nil? ? condition.to_s.sub(/^by_/, '') : options[:using]
  target_class = options[:class_name].nil? ? self.name : "Fleakr::Objects::#{options[:class_name]}"

  class_eval <<-CODE
    def self.find_all_#{condition}(value, options = {})
      options.merge!(:#{attribute} => value)
      
      response = Fleakr::Api::MethodRequest.with_response!('#{options[:call]}', options)
      (response.body/'rsp/#{options[:path]}').map {|e| #{target_class}.new(e) }
    end
  CODE
end

#find_one(condition, options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fleakr/support/object.rb', line 49

def find_one(condition, options)
  attribute = options[:using].nil? ? condition.to_s.sub(/^by_/, '') : options[:using]

  class_eval <<-CODE
    def self.find_#{condition}(value, options = {})
      options.merge!(:#{attribute} => value)
      
      response = Fleakr::Api::MethodRequest.with_response!('#{options[:call]}', options)
      #{self.name}.new(response.body)
    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(*names_and_options)
  options = names_and_options.extract_options!

  names_and_options.each do |name|
    self.attributes << Attribute.new(name, options[: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
# File 'lib/fleakr/support/object.rb', line 20

def has_many(*attributes)
  class_name = self.name

  attributes.each do |attribute|
    target           = "Fleakr::Objects::#{attribute.to_s.classify}"
    finder_attribute = "#{class_name.demodulize.underscore}_id"

    class_eval <<-CODE
      def #{attribute}
        @#{attribute} ||= #{target}.send("find_all_by_#{finder_attribute}".to_sym, self.id)
      end
    CODE
  end
end

#lazily_load(*attributes) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fleakr/support/object.rb', line 72

def lazily_load(*attributes)
  options = attributes.extract_options!

  attributes.each do |attribute|
    class_eval <<-CODE
      def #{attribute}_with_loading
        self.send(:#{options[:with]}) if @#{attribute}.nil?
        #{attribute}_without_loading
      end
      alias_method_chain :#{attribute}, :loading
    CODE
  end
end

#scoped_searchObject



62
63
64
65
66
67
68
69
70
# File 'lib/fleakr/support/object.rb', line 62

def scoped_search
  key = "#{self.name.demodulize.underscore.downcase}_id".to_sym

  class_eval <<-CODE
    def search(search_text)
      Fleakr::Objects::Search.new(:text => search_text, :#{key} => self.id).results
    end
  CODE
end