Module: Dawanda::Model::ClassMethods
- Defined in:
- lib/dawanda/model.rb
Instance Method Summary collapse
- #attribute(name, options = nil) ⇒ Object
- #attributes(*names) ⇒ Object
- #find_all(parameter, endpoint) ⇒ Object
- #find_generic(name, endpoint) ⇒ Object
- #find_one(parameter, endpoint) ⇒ Object
- #finder(type, endpoint, name = nil) ⇒ Object
- #to_result_string(options) ⇒ Object
Instance Method Details
#attribute(name, options = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dawanda/model.rb', line 6 def attribute(name, =nil) = name if .nil? if .is_a? Array class_eval <<-CODE def #{name} @result#{to_result_string()} end CODE else class_eval <<-CODE def #{name} @result['#{}'] end CODE end end |
#attributes(*names) ⇒ Object
34 35 36 |
# File 'lib/dawanda/model.rb', line 34 def attributes(*names) names.each {|name| attribute(name) } end |
#find_all(parameter, endpoint) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dawanda/model.rb', line 51 def find_all(parameter, endpoint) class_eval <<-CODE def self.find_all_by_#{parameter}(#{parameter}, params = {}) response = Request.get("#{endpoint}", params.reject{|key, value| key == :raw_response}) results = response.results.map {|listing| new(listing) } return OpenStruct.new({ :results => results, :entries => response.entries, :pages => response.pages, :type => response.type}) if params[:raw_response] results end CODE end |
#find_generic(name, endpoint) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dawanda/model.rb', line 72 def find_generic(name, endpoint) class_eval <<-CODE def self.find_#{name}(params = {}) response = Request.get("#{endpoint}", params.reject{|key, value| key == :raw_response}) return response if params[:raw_response] if response.result.nil? return OpenStruct.new({ :results => response.results.map {|listing| new(listing) }, :entries => response.entries, :pages => response.pages, :type => response.type}) if params[:raw_response] response.results.map {|listing| new(listing) } else return OpenStruct.new({ :result => new(response.result), :entries => response.entries, :pages => response.pages, :type => type}) if params[:raw_response] new response.result end end CODE end |
#find_one(parameter, endpoint) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/dawanda/model.rb', line 62 def find_one(parameter, endpoint) class_eval <<-CODE def self.find_by_#{parameter}(#{parameter}, params = {}) response = Request.get("#{endpoint}", params.reject{|key, value| key == :raw_response}) return response if params[:raw_response] new response.result end CODE end |
#finder(type, endpoint, name = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dawanda/model.rb', line 38 def finder(type, endpoint, name=nil) if name.nil? parameter = endpoint.scan(/:\w+/).first parameter.sub!(/^:/, '') endpoint.sub!(":#{parameter}", '#{' + parameter + '}') send("find_#{type}", parameter, endpoint) elsif type == :generic send("find_#{type}", name, endpoint) end end |
#to_result_string(options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dawanda/model.rb', line 23 def to_result_string .map! do |o| if o.is_a? Numeric "[#{o}]" else "['#{o}']" end end .join end |