Class: Entity

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sale/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, client, original_params = nil) ⇒ Entity

Returns a new instance of Entity.



9
10
11
# File 'lib/sale/entity.rb', line 9

def initialize(data, client, original_params = nil)
  @data, @client, @original_params = maybe_stringify(data), client, original_params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sale/entity.rb', line 81

def method_missing(name, *args)
  name = name.to_s
  if is_hash?(data[name])
    Entity.new(data[name], client)
  elsif data[name]
    data[name]
  else
    nil
    # raise "Not found: #{name} in #{data}"
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/sale/entity.rb', line 7

def data
  @data
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/sale/entity.rb', line 17

def [](key)
  data[key.to_s]
end

#[]=(key, obj) ⇒ Object



21
22
23
# File 'lib/sale/entity.rb', line 21

def []=(key, obj)
  data[key.to_s] = obj
end

#countObject Also known as: size



42
43
44
# File 'lib/sale/entity.rb', line 42

def count
  to_a.size
end

#each(&block) ⇒ Object



38
39
40
# File 'lib/sale/entity.rb', line 38

def each(&block)
  to_a.each(&block)
end

#firstObject



57
58
59
# File 'lib/sale/entity.rb', line 57

def first
  items ? items.first : nil
end

#has_next_page?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/sale/entity.rb', line 69

def has_next_page?
  !!data['next']
end

#has_prev_page?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sale/entity.rb', line 65

def has_prev_page?
  !!data['prev']
end

#itemsObject



52
53
54
55
# File 'lib/sale/entity.rb', line 52

def items
  return if data['items'].nil?
  @items ||= data['items'].map { |obj| Entity.new(obj, client) }
end

#lastObject



61
62
63
# File 'lib/sale/entity.rb', line 61

def last
  items ? items.last : nil
end

#load(params = nil) ⇒ Object



29
30
31
# File 'lib/sale/entity.rb', line 29

def load(params = nil)
  client.get(data['href'], params)
end

#next_pageObject



77
78
79
# File 'lib/sale/entity.rb', line 77

def next_page
  data['next'] ? follow_link(fix_link(data['next']), @original_params) : nil
end

#prev_pageObject



73
74
75
# File 'lib/sale/entity.rb', line 73

def prev_page
  data['prev'] ? follow_link(fix_link(data['prev']), @original_params) : nil
end

#reloadObject



33
34
35
36
# File 'lib/sale/entity.rb', line 33

def reload
  # @data = client.request('get', data['href'])
  load # just return a new instance
end

#to_aObject



25
26
27
# File 'lib/sale/entity.rb', line 25

def to_a
  data['items'] ? items : []
end

#to_sObject



13
14
15
# File 'lib/sale/entity.rb', line 13

def to_s
  @data.inspect
end

#total_entriesObject



46
47
48
# File 'lib/sale/entity.rb', line 46

def total_entries
  data['count']
end