Module: Harvest::Model::ClassMethods
- Defined in:
- lib/harvest/model.rb
Instance Method Summary collapse
-
#api_path(path = nil) ⇒ void
This sets the API path so the API collections can use them in an agnostic way.
- #delegate_methods(options) ⇒ Object
- #json_root ⇒ Object
- #parse(json) ⇒ Object
- #skip_json_root(skip = nil) ⇒ Object
- #skip_json_root? ⇒ Boolean
- #to_json(json) ⇒ Object
- #wrap(model_or_attrs) ⇒ Object
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() raise "no methods given" if .empty? .each do |source, dest| class_eval " def \#{source}\n \#{dest}\n end\n EOV\n end\nend\n" |
#json_root ⇒ Object
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
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 |