Class: RelateIQ::RiqObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/relateiq/riq_object.rb

Direct Known Subclasses

APIResource

Constant Summary collapse

@@permanent_attributes =
Set.new([:id])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RiqObject.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/relateiq/riq_object.rb', line 8

def initialize(id=nil, api_key=nil)
  # parameter overloading!
  if id.kind_of?(Hash)
    @retrieve_options = id.dup
    @retrieve_options.delete(:id)
    id = id[:id]
  else
    @retrieve_options = {}
  end

  @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
  @values[:id] = id if id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/relateiq/riq_object.rb', line 79

def method_missing(name, *args)
  # TODO: only allow setting in updateable classes.
  if name.to_s.end_with?('=')
    attr = name.to_s[0...-1].to_sym
    # Set the value if it's not a permanent attribute
    if @@permanent_attributes.include?(k)
      raise NoMethodError.new("Cannot set #{attr} on this object. HINT: you can't set: #{@@permanent_attributes.to_a.join(', ')}")
    else
      add_accessors([attr])
    end
  else
    return @values[name]
  end

  begin
    # Let's not get all crazy now
    nil
    #super
  rescue NoMethodError => e
    if @transient_values.include?(name)
      raise NoMethodError.new(e.message + ".  HINT: The '#{name}' attribute was set in the past, however.  It was then wiped when refreshing the object with the result returned by Stripe's API, probably as a result of a save().  The attributes currently available on this object are: #{@values.keys.join(', ')}")
    else
      raise
    end
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/relateiq/riq_object.rb', line 5

def api_key
  @api_key
end

#valuesObject

Returns the value of attribute values.



5
6
7
# File 'lib/relateiq/riq_object.rb', line 5

def values
  @values
end

Class Method Details

.construct_from(values, api_key = nil) ⇒ Object



45
46
47
48
49
# File 'lib/relateiq/riq_object.rb', line 45

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

Instance Method Details

#add_accessors(keys) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/relateiq/riq_object.rb', line 60

def add_accessors(keys)
  metaclass.instance_eval do
    keys.each do |k|
      next if @@permanent_attributes.include?(k)
      k_eq = :"#{k}="
      #define_method(k) { @values[k] }
      define_method(k_eq) do |v|
        if v == ""
          raise ArgumentError.new(
            "You cannot set #{k} to an empty string." +
            "We interpret empty strings as nil in requests." +
            "You may set #{self}.#{k} = nil to delete the property.")
        end
        @values[k.to_sym] = v
      end
    end
  end
end

#inspectObject



51
52
53
54
# File 'lib/relateiq/riq_object.rb', line 51

def inspect()
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
end

#metaclassObject



56
57
58
# File 'lib/relateiq/riq_object.rb', line 56

def metaclass
  class << self; self; end
end

#refresh_from(values) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/relateiq/riq_object.rb', line 27

def refresh_from(values)

  @previous_metadata = values[:metadata]
  removed = 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
    add_accessors(added)
  end
  values.each do |k, v|
    @values[k.to_sym] = v
  end
  self
end

#respond_to_missing?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/relateiq/riq_object.rb', line 106

def respond_to_missing?(symbol, include_private = false)
  @values.has_key?(symbol.to_sym) || nil
end