Class: Billogram::Resource
- Inherits:
-
Object
- Object
- Billogram::Resource
show all
- Defined in:
- lib/billogram/resource.rb
Direct Known Subclasses
Address, AutomaticCollection, AutomaticReminder, Bookkeeping, BookkeepingObject, Callbacks, Contact, Customer, Data, DetailedSums, DomesticBankAccount, ElectricityCollection, Event, Info, InternationalBankAccount, Invoice, InvoiceDefaults, Item, Logotype, Payment, RegionalSweden, Report, Settings, Tax
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/billogram/resource.rb', line 24
def initialize(attributes = {})
Hash(attributes).each do |key, value|
if respond_to?("#{key}=")
public_send("#{key}=", value)
else
warn("#{self.class}: unknown attribute #{key}")
end
end
RelationBuilder.new(self, attributes).call
end
|
Class Method Details
.build_objects(data) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/billogram/resource.rb', line 15
def build_objects(data)
case data
when Hash then new(data)
when Array then data.map{|item| build_objects(item) }
else data
end
end
|
.relation(name, type, class_override: nil) ⇒ Object
10
11
12
13
|
# File 'lib/billogram/resource.rb', line 10
def relation(name, type, class_override: nil)
relations << Relation.new(name, type, class_override: class_override)
attr_accessor name
end
|
.relations ⇒ Object
6
7
8
|
# File 'lib/billogram/resource.rb', line 6
def relations
@relations ||= []
end
|
Instance Method Details
#to_hash ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/billogram/resource.rb', line 40
def to_hash
instance_variables
.each_with_object({}) do |variable, obj|
value = instance_variable_get(variable)
case value
when Resource
value = value.to_hash
when Array
value = value.map(&:to_hash)
end
obj[variable[1..-1]] = value
end
end
|
#to_json(*args) ⇒ Object
36
37
38
|
# File 'lib/billogram/resource.rb', line 36
def to_json(*args)
to_hash.to_json(*args)
end
|