Class: Invoiced::Object

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/invoiced/object.rb

Direct Known Subclasses

Customer, Email, Invoice, LineItem, Subscription, Transaction

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, id = nil, values = {}) ⇒ Object

Returns a new instance of Object.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/invoiced/object.rb', line 9

def initialize(client, id=nil, values={})
	@client = client
	class_name = self.class.name.split('::').last
	@endpoint_base = ''
	@endpoint = '/' + class_name.underscore.pluralize.downcase

	@id = id
	@values = {}

	if !id.nil?
		@endpoint += "/#{id}"
		@unsaved = Set.new
		refresh_from(values.dup.merge({:id => id}))
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/invoiced/object.rb', line 146

def method_missing(name, *args)
	if name.to_s.end_with?('=')
		attr = name.to_s[0...-1].to_sym
		add_accessors([attr])
		begin
			mth = method(name)
		rescue NameError
			raise NoMethodError.new("Cannot set #{attr} on this object. HINT: you can't set: #{@@permanent_attributes.to_a.join(', ')}")
		end
		return mth.call(args[0])
	else
		return @values[name] if @values.has_key?(name)
	end

	begin
		super
	rescue NoMethodError => e
		raise
	end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/invoiced/object.rb', line 5

def client
  @client
end

Instance Method Details

#[](k) ⇒ Object



80
81
82
# File 'lib/invoiced/object.rb', line 80

def [](k)
	@values[k.to_sym]
end

#[]=(k, v) ⇒ Object



84
85
86
# File 'lib/invoiced/object.rb', line 84

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

#add_accessors(keys) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/invoiced/object.rb', line 126

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] = v
				@unsaved.add(k)
			end
		end
	end
end

#each(&blk) ⇒ Object



107
108
109
# File 'lib/invoiced/object.rb', line 107

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

#endpointObject



34
35
36
# File 'lib/invoiced/object.rb', line 34

def endpoint()
	@endpoint_base + @endpoint
end

#endpoint_baseObject



30
31
32
# File 'lib/invoiced/object.rb', line 30

def endpoint_base()
	@endpoint_base
end

#inspectObject



55
56
57
58
# File 'lib/invoiced/object.rb', line 55

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

#keysObject



88
89
90
# File 'lib/invoiced/object.rb', line 88

def keys
	@values.keys
end

#load(opts = {}) ⇒ Object



48
49
# File 'lib/invoiced/object.rb', line 48

def load(opts={})
end

#metaclassObject



111
112
113
# File 'lib/invoiced/object.rb', line 111

def metaclass
	class << self; self; end
end

#refresh_from(values) ⇒ Object



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

def refresh_from(values)
	removed = Set.new(@values.keys - values.keys)
	added = Set.new(values.keys - @values.keys)

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

	return self
end

#remove_accessors(keys) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/invoiced/object.rb', line 115

def remove_accessors(keys)
	metaclass.instance_eval do
		keys.each do |k|
			next if @@permanent_attributes.include?(k)
			k_eq = :"#{k}="
			remove_method(k) if method_defined?(k)
			remove_method(k_eq) if method_defined?(k_eq)
		end
	end
end

#retrieve(id, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/invoiced/object.rb', line 38

def retrieve(id, opts={})
	if !id
		raise ArgumentError.new("Missing ID.")
	end

	response = @client.request(:get, "#{self.endpoint()}/#{id}", opts)

	Util.convert_to_object(self, response[:body])
end

#set_endpoint_base(base) ⇒ Object



25
26
27
28
# File 'lib/invoiced/object.rb', line 25

def set_endpoint_base(base)
	@endpoint_base = base
	self
end

#to_hashObject



100
101
102
103
104
105
# File 'lib/invoiced/object.rb', line 100

def to_hash
	@values.inject({}) do |acc, (key, value)|
    	acc[key] = value.respond_to?(:to_hash) ? value.to_hash : value
    	acc
	end
end

#to_json(*a) ⇒ Object



96
97
98
# File 'lib/invoiced/object.rb', line 96

def to_json(*a)
	JSON.generate(@values)
end

#to_s(*args) ⇒ Object



51
52
53
# File 'lib/invoiced/object.rb', line 51

def to_s(*args)
	JSON.pretty_generate(@values)
end

#valuesObject



92
93
94
# File 'lib/invoiced/object.rb', line 92

def values
	@values.values
end