Module: Dawanda::Model::ClassMethods

Defined in:
lib/dawanda/model.rb

Instance Method Summary collapse

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, options=nil)
  options = name if options.nil?
  if options.is_a? Array
    class_eval "      def \#{name}\n        @result\#{to_result_string(options)}\n      end\n    CODE\n  else\n    class_eval <<-CODE\n      def \#{name}\n        @result['\#{options}']\n      end\n    CODE\n  end\nend\n"

#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 "    def self.find_all_by_\#{parameter}(\#{parameter}, params = {})\n      response = Request.get(\"\#{endpoint}\", params.reject{|key, value| key == :raw_response})\n      results = response.results.map {|listing| new(listing) }\n      return OpenStruct.new({ :results => results, :entries => response.entries, :pages => response.pages, :type => response.type}) if params[:raw_response]\n      results\n    end\n  CODE\nend\n"

#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 "    def self.find_\#{name}(params = {})\n      response = Request.get(\"\#{endpoint}\", params.reject{|key, value| key == :raw_response})\n      return response if params[:raw_response]\n      if response.result.nil?\n        return OpenStruct.new({ :results => response.results.map {|listing| new(listing) }, :entries => response.entries, :pages => response.pages, :type => response.type}) if params[:raw_response]\n        response.results.map {|listing| new(listing) }\n      else\n        return OpenStruct.new({ :result => new(response.result), :entries => response.entries, :pages => response.pages, :type => type}) if params[:raw_response]\n        new response.result\n      end\n    end\n  CODE\nend\n"

#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 "    def self.find_by_\#{parameter}(\#{parameter}, params = {})\n      response = Request.get(\"\#{endpoint}\", params.reject{|key, value| key == :raw_response})\n      return response if params[:raw_response]\n      new response.result\n    end\n  CODE\nend\n"

#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 options
  options.map! do |o| 
    if o.is_a? Numeric
      "[#{o}]"
    else
      "['#{o}']"
    end
  end
  options.join
end