Class: Alman::ApiList

Inherits:
ApiResource show all
Includes:
Enumerable
Defined in:
lib/alman/apibits/api_list.rb

Instance Attribute Summary collapse

Attributes inherited from ApiResource

#json

Instance Method Summary collapse

Methods inherited from ApiResource

add_api_attribute, api_attribute_names, #api_attributes, api_subclass_fetch, api_subclasses, #changed_api_attributes, #clear_api_attributes, client, determine_api_attribute_value, #determine_api_attribute_value, #inspect_api_attributes, #inspect_nested, register_api_subclass, #to_json

Constructor Details

#initialize(klass, json = {}, api_method = nil, client = nil) ⇒ ApiList

Returns a new instance of ApiList.



10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#api_methodObject (readonly)

Returns the value of attribute api_method.



5
6
7
# File 'lib/alman/apibits/api_list.rb', line 5

def api_method
  @api_method
end

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/alman/apibits/api_list.rb', line 6

def client
  @client
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/alman/apibits/api_list.rb', line 7

def data
  @data
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/alman/apibits/api_list.rb', line 8

def klass
  @klass
end

Instance Method Details

#[](k) ⇒ Object



48
49
50
# File 'lib/alman/apibits/api_list.rb', line 48

def [](k)
  data[k]
end

#[]=(k, v) ⇒ Object



52
53
54
# File 'lib/alman/apibits/api_list.rb', line 52

def []=(k, v)
  data[k]=v
end

#each(&blk) ⇒ Object



64
65
66
# File 'lib/alman/apibits/api_list.rb', line 64

def each(&blk)
  data.each(&blk)
end

#inspectObject



68
69
70
# 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_dataObject



72
73
74
75
76
77
78
79
80
81
82
# 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

#lastObject



56
57
58
# File 'lib/alman/apibits/api_list.rb', line 56

def last
  data.last
end

#lengthObject



60
61
62
# File 'lib/alman/apibits/api_list.rb', line 60

def length
  data.length
end

#refresh_from(json, api_method = nil, client = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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