module Oshpark::Model

Public Class Methods

included(base) click to toggle source
# File lib/oshpark/model.rb, line 14
def self.included base
  base.send :extend, ClassMethods

  base.instance_eval do
    attr_reader *base.attrs
  end

  base.write_attrs.each do |attr|
    define_method "#{attr}=" do |new_value|
      @dirty_attributes << attr unless @dirty_attributes.include?(attr)
      instance_variable_set "@#{attr}".to_sym, new_value
    end
  end if base.respond_to? :write_attrs
end
new(json) click to toggle source
# File lib/oshpark/model.rb, line 6
def initialize json
  reload_with json
end

Public Instance Methods

dirty?() click to toggle source
# File lib/oshpark/model.rb, line 10
def dirty?
  @dirty_attributes.size > 0
end

Private Instance Methods

clean_json(json) { |json| ... } click to toggle source
# File lib/oshpark/model.rb, line 64
def clean_json json
  json = json[object_name] if json.has_key?(object_name) && (Hash === json[object_name])

  yield json if block_given?
  json
end
guard_against_invalid_arguments(keys) click to toggle source
# File lib/oshpark/model.rb, line 83
def guard_against_invalid_arguments keys
  if (extra = keys.map(&:to_s) - self.class.attrs) && extra.any?
    raise ArgumentError, "Unknown attribute: #{extra.join(' ')}"
  end
end
object_name() click to toggle source
# File lib/oshpark/model.rb, line 56
def object_name
  self.class.object_name
end
object_names() click to toggle source
# File lib/oshpark/model.rb, line 60
def object_names
  self.class.object_names
end
reload_with(json) click to toggle source
# File lib/oshpark/model.rb, line 71
def reload_with json
  clean_json(json) do |json|
    guard_against_invalid_arguments json.keys

    json.each do |key,value|
      instance_variable_set "@#{key}", value
    end
    @dirty_attributes = []
  end
  self
end
time_from(json_time) click to toggle source

Override hook for converting JSON serialized time strings into Ruby Time objects. Only needed if ‘Time.parse` doesn’t work as expected on your platform (ie RubyMotion).

# File lib/oshpark/model.rb, line 52
def time_from json_time
  Time.parse json_time if json_time
end