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



45
46
47
# File 'lib/harvest/model.rb', line 45

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

#delegate_methods(options) ⇒ Object



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

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

#json_rootObject



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

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

#parse(json) ⇒ Object



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

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

#skip_json_root(skip = nil) ⇒ Object



49
50
51
# File 'lib/harvest/model.rb', line 49

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

#skip_json_root?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/harvest/model.rb', line 53

def skip_json_root?
  @_skip_json_root == true
end

#to_json(json) ⇒ Object



68
69
70
# File 'lib/harvest/model.rb', line 68

def to_json(json)
  parsed = String === json ? JSON.parse(json) : json
end

#wrap(model_or_attrs) ⇒ Object



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

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