module Forecast::Model::ClassMethods

Public Instance Methods

api_path(path = nil) click to toggle source

This sets the API path so the API collections can use them in an agnostic way @return [void]

# File lib/forecast/model.rb, line 47
def api_path(path = nil)
  @_api_path ||= path
end
delegate_methods(options) click to toggle source
# 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 <<-EOV
            def #{source}
              #{dest}
            end
          EOV
        end
      end
json_root() click to toggle source
# File lib/forecast/model.rb, line 88
def json_root
  Forecast::Model::Utility.underscore(
    Forecast::Model::Utility.demodulize(to_s))
end
parse(json) click to toggle source
# 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) click to toggle source
# File lib/forecast/model.rb, line 51
def skip_json_root(skip = nil)
  @_skip_json_root ||= skip
end
skip_json_root?() click to toggle source
# File lib/forecast/model.rb, line 55
def skip_json_root?
  @_skip_json_root == true
end
wrap(model_or_attrs) click to toggle source
# 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