Class: Paid::APIList

Inherits:
APIResource show all
Includes:
Enumerable
Defined in:
lib/paid/api_list.rb

Direct Known Subclasses

RefundList

Instance Attribute Summary collapse

Attributes inherited from APIResource

#api_method, #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, #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) ⇒ APIList

Returns a new instance of APIList.



8
9
10
11
12
13
14
15
16
# File 'lib/paid/api_list.rb', line 8

def initialize(klass, json={}, api_method=nil)
  if klass.is_a?(Class)
    @klass = klass
  else
    @klass = Util.constantize(klass)
  end

  refresh_from(json)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

Instance Method Details

#[](k) ⇒ Object



45
46
47
# File 'lib/paid/api_list.rb', line 45

def [](k)
  data[k]
end

#[]=(k, v) ⇒ Object



49
50
51
# File 'lib/paid/api_list.rb', line 49

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

#each(&blk) ⇒ Object



61
62
63
# File 'lib/paid/api_list.rb', line 61

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

#inspectObject



65
66
67
# File 'lib/paid/api_list.rb', line 65

def inspect
  "#<#{self.class}[#{@klass}]:0x#{self.object_id.to_s(16)}> Data: " + JSON.pretty_generate(inspect_data)
end

#inspect_dataObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/paid/api_list.rb', line 69

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



53
54
55
# File 'lib/paid/api_list.rb', line 53

def last
  data.last
end

#lengthObject



57
58
59
# File 'lib/paid/api_list.rb', line 57

def length
  data.length
end

#refresh_from(json, api_method = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/paid/api_list.rb', line 18

def refresh_from(json, api_method=nil)
  unless json.is_a?(Hash)
    json = {
      :data => json
    }
  end
  json = Util.symbolize_keys(json)

  clear_api_attributes
  @api_method = api_method
  @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) })
      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