Module: Forecast::Model::ClassMethods

Defined in:
lib/forecast/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/forecast/model.rb', line 47

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

#delegate_methods(options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/forecast/model.rb', line 104

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



88
89
90
91
# File 'lib/forecast/model.rb', line 88

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

#parse(json) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/forecast/model.rb', line 59

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

  # parses json into a ruby hash
  # {'projects' => [{id: 123}, {id: 456}]}
  parsed = String === json ? JSON.parse(json) : json


  # @todo @weston
  # total hack
  if parsed.keys == [json_root]
    # single object
    # before  # {"project"=>{"id"=>123"}}
    # after   # {"id"=>123}
    object_hash = parsed[json_root]
    new(object_hash)
  else
    # list of objects
    # before # {'projects' => [{id: 123}, {id: 456}]}
    # after  # [{id: 123}, {id: 456}]
    object_hashes = parsed.to_a[0][1]
    return object_hashes.map {|attrs| new(attrs)}
  end
end

#skip_json_root(skip = nil) ⇒ Object



51
52
53
# File 'lib/forecast/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/forecast/model.rb', line 55

def skip_json_root?
  @_skip_json_root == true
end

#wrap(model_or_attrs) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/forecast/model.rb', line 93

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