Class: Eligible::EligibleObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/eligible/eligible_object.rb

Direct Known Subclasses

APIResource

Constant Summary collapse

@@permanent_attributes =
Set.new([:api_key, :error, :balance, :address, :dob])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, api_key = nil) ⇒ EligibleObject

Returns a new instance of EligibleObject.



13
14
15
16
17
18
19
20
21
# File 'lib/eligible/eligible_object.rb', line 13

def initialize(id = nil, api_key = nil)
  @api_key = api_key
  @values = {}
  # This really belongs in APIResource, but not putting it there allows us
  # to have a unified inspect method
  @unsaved_values = Set.new
  @transient_values = Set.new
  self.eligible_id = id if id
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/eligible/eligible_object.rb', line 5

def api_key
  @api_key
end

#eligible_idObject

Returns the value of attribute eligible_id.



6
7
8
# File 'lib/eligible/eligible_object.rb', line 6

def eligible_id
  @eligible_id
end

Class Method Details

.construct_from(values, api_key = nil) ⇒ Object



23
24
25
26
27
# File 'lib/eligible/eligible_object.rb', line 23

def self.construct_from(values, api_key = nil)
  obj = new(values[:eligible_id], api_key)
  obj.refresh_from(values, api_key)
  obj
end

Instance Method Details

#[](k) ⇒ Object



54
55
56
57
# File 'lib/eligible/eligible_object.rb', line 54

def [](k)
  k = k.to_sym if k.is_a?(String)
  @values[k]
end

#[]=(k, v) ⇒ Object



59
60
61
# File 'lib/eligible/eligible_object.rb', line 59

def []=(k, v)
  send(:"#{k}=", v)
end

#each(&blk) ⇒ Object



79
80
81
# File 'lib/eligible/eligible_object.rb', line 79

def each(&blk)
  @values.each(&blk)
end

#errorObject



83
84
85
# File 'lib/eligible/eligible_object.rb', line 83

def error
  keys.include?(:error) ? @values[:error] : nil
end

#keysObject



63
64
65
# File 'lib/eligible/eligible_object.rb', line 63

def keys
  @values.keys
end

#refresh_from(values, api_key, partial = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/eligible/eligible_object.rb', line 29

def refresh_from(values, api_key, partial = false)
  @api_key = api_key

  removed = partial ? Set.new : Set.new(@values.keys - values.keys)
  added = Set.new(values.keys - @values.keys)
  # Wipe old state before setting new.  This is useful for e.g. updating a
  # customer, where there is no persistent card parameter.  Mark those values
  # which don't persist as transient

  instance_eval do
    remove_accessors(removed)
    add_accessors(added)
  end
  removed.each do |k|
    @values.delete(k)
    @transient_values.add(k)
    @unsaved_values.delete(k)
  end
  values.each do |k, v|
    @values[k] = v
    @transient_values.delete(k)
    @unsaved_values.delete(k)
  end
end

#to_hashObject



75
76
77
# File 'lib/eligible/eligible_object.rb', line 75

def to_hash
  @values
end

#to_jsonObject



71
72
73
# File 'lib/eligible/eligible_object.rb', line 71

def to_json
  Eligible::JSON.dump(@values)
end

#valuesObject



67
68
69
# File 'lib/eligible/eligible_object.rb', line 67

def values
  @values.values
end