class Alman::ApiList

Attributes

api_method[R]
client[R]
data[R]
klass[R]

Public Class Methods

new(klass, json={}, api_method=nil, client=nil) click to toggle source
# File lib/alman/apibits/api_list.rb, line 10
def initialize(klass, json={}, api_method=nil, client=nil)
  if klass.is_a?(Class)
    @klass = klass
  else
    @klass = Util.constantize(klass)
  end

  refresh_from(json, api_method, client)
end

Public Instance Methods

[](k) click to toggle source
# File lib/alman/apibits/api_list.rb, line 48
def [](k)
  data[k]
end
[]=(k, v) click to toggle source
# File lib/alman/apibits/api_list.rb, line 52
def []=(k, v)
  data[k]=v
end
each(&blk) click to toggle source
# File lib/alman/apibits/api_list.rb, line 64
def each(&blk)
  data.each(&blk)
end
inspect() click to toggle source
# File lib/alman/apibits/api_list.rb, line 68
def inspect
  "#<#{self.class}[#{@klass}]:0x#{self.object_id.to_s(16)}> Data: " + JSON.pretty_generate(inspect_data)
end
inspect_data() click to toggle source
# File lib/alman/apibits/api_list.rb, line 72
def inspect_data
  ret = []
  data.each do |d|
    if d.respond_to?(:inspect_nested)
      ret << d.inspect_nested
    else
      ret << d
    end
  end
  ret
end
last() click to toggle source
# File lib/alman/apibits/api_list.rb, line 56
def last
  data.last
end
length() click to toggle source
# File lib/alman/apibits/api_list.rb, line 60
def length
  data.length
end
refresh_from(json, api_method=nil, client=nil) click to toggle source
# File lib/alman/apibits/api_list.rb, line 20
def refresh_from(json, api_method=nil, client=nil)
  unless json.is_a?(Hash)
    json = {
      :data => json
    }
  end
  json = Util.symbolize_keys(json)

  clear_api_attributes
  @api_method = api_method
  @client = client
  @data = []
  @json = Util.sorta_deep_clone(json)

  json.each do |k, v|
    if k.to_sym == :data
      if v.respond_to?(:map)
        instance_variable_set("@#{k}", v.map{ |i| @klass.new(i, @api_method, @client) })
      else
        instance_variable_set("@#{k}", v || [])
      end
    elsif self.class.api_attribute_names.include?(k)
      instance_variable_set("@#{k}", determine_api_attribute_value(k, v))
    end
  end
  self
end