Module: Harvest::Model::ClassMethods

Defined in:
lib/harvest/model.rb

Instance Method Summary collapse

Instance Method Details

#api_path(path = nil) ⇒ void

This method returns an undefined value.

This sets the API path so the API collections can use them in an agnostic way



47
48
49
# File 'lib/harvest/model.rb', line 47

def api_path(path = nil)
  @_api_path ||= path
end

#delegate_methods(options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/harvest/model.rb', line 81

def delegate_methods(options)
  raise "no methods given" if options.empty?
  options.each do |source, dest|
    class_eval <<-EOV
      def #{source}
        #{dest}
      end
    EOV
  end
end

#json_rootObject



64
65
66
67
68
# File 'lib/harvest/model.rb', line 64

def json_root
  Harvest::Model::Utility.underscore(
    Harvest::Model::Utility.demodulize(to_s)
  )
end

#parse(json) ⇒ Object



59
60
61
62
# File 'lib/harvest/model.rb', line 59

def parse(json)
  parsed = String === json ? JSON.parse(json) : json
  Array.wrap(parsed).map {|attrs| skip_json_root? ? new(attrs) : new(attrs[json_root])}
end

#skip_json_root(skip = nil) ⇒ Object



51
52
53
# File 'lib/harvest/model.rb', line 51

def skip_json_root(skip = nil)
  @_skip_json_root ||= skip
end

#skip_json_root?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/harvest/model.rb', line 55

def skip_json_root?
  @_skip_json_root == true
end

#wrap(model_or_attrs) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/harvest/model.rb', line 70

def wrap(model_or_attrs)
  case model_or_attrs
  when Hashie::Mash
    model_or_attrs
  when Hash
    new(model_or_attrs)
  else
    model_or_attrs
  end
end