Class: Lightspeed::Resource
- Inherits:
-
Object
- Object
- Lightspeed::Resource
show all
- Defined in:
- lib/lightspeed/resource.rb
Direct Known Subclasses
Account, Category, Employee, Image, Inventory, Item, ItemAttributeSet, ItemMatrix, Order, Sale, SaleLine, Shop, SpecialOrder, Vendor
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client: nil, context: nil, attributes: {}) ⇒ Resource
Returns a new instance of Resource.
15
16
17
18
19
|
# File 'lib/lightspeed/resource.rb', line 15
def initialize(client: nil, context: nil, attributes: {})
self.client = client
self.context = context
self.attributes = attributes
end
|
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
13
14
15
|
# File 'lib/lightspeed/resource.rb', line 13
def account
@account
end
|
#attributes ⇒ Object
Returns the value of attribute attributes.
13
14
15
|
# File 'lib/lightspeed/resource.rb', line 13
def attributes
@attributes
end
|
#client ⇒ Object
Returns the value of attribute client.
13
14
15
|
# File 'lib/lightspeed/resource.rb', line 13
def client
@client
end
|
#context ⇒ Object
Returns the value of attribute context.
13
14
15
|
# File 'lib/lightspeed/resource.rb', line 13
def context
@context
end
|
#id ⇒ Object
Returns the value of attribute id.
13
14
15
|
# File 'lib/lightspeed/resource.rb', line 13
def id
@id
end
|
Class Method Details
.collection_name ⇒ Object
89
90
91
|
# File 'lib/lightspeed/resource.rb', line 89
def self.collection_name
resource_name.pluralize
end
|
.fields(fields = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/lightspeed/resource.rb', line 34
def self.fields(fields = {})
@fields ||= []
attr_writer(*fields.keys)
fields.each do |name, klass|
@fields << define_method(name) do
get_transformed_value(name, klass)
end
end
@fields
end
|
.id_field ⇒ Object
93
94
95
|
# File 'lib/lightspeed/resource.rb', line 93
def self.id_field
"#{resource_name.camelize(:lower)}ID"
end
|
.relationships(*args) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/lightspeed/resource.rb', line 101
def self.relationships(*args)
@relationships ||= []
paired_args = args.flat_map { |r| r.is_a?(Hash) ? r.to_a : [[r, r]] }
paired_args.each do |(relation_name, class_name)|
method_name = relation_name.to_s.underscore.to_sym
@relationships << define_method(method_name) do
instance_variable_get("@#{method_name}") || get_relation(method_name, relation_name, class_name)
end
end
@relationships
end
|
.resource_name ⇒ Object
85
86
87
|
# File 'lib/lightspeed/resource.rb', line 85
def self.resource_name
name.demodulize
end
|
Instance Method Details
#as_json(*args) ⇒ Object
Also known as:
to_h
122
123
124
|
# File 'lib/lightspeed/resource.rb', line 122
def as_json(*args)
fields_to_h.merge(relationships_to_h).reject { |_, v| v.nil? || v == {} }.as_json(*args)
end
|
#base_path ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/lightspeed/resource.rb', line 127
def base_path
if context.is_a?(Lightspeed::Collection)
"#{context.base_path}/#{id}"
else
"#{account.base_path}/#{resource_name}/#{id}"
end
end
|
#destroy ⇒ Object
80
81
82
83
|
# File 'lib/lightspeed/resource.rb', line 80
def destroy
self.attributes = delete[resource_name]
self
end
|
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/lightspeed/resource.rb', line 45
def get_transformed_value(name, kind)
value = instance_variable_get("@#{name}")
if value.is_a?(String)
case kind
when :string then value
when :integer then value.to_i
when :id then value.to_i
when :datetime then DateTime.parse(value)
when :boolean then value == 'true'
when :decimal then BigDecimal.new(value)
when :hash then Hash.new(value)
else
raise ArgumentError, "Could not transform value #{value} to a #{kind}"
end
else
value
end
end
|
#id_params ⇒ Object
97
98
99
|
# File 'lib/lightspeed/resource.rb', line 97
def id_params
{ self.class.id_field => id }
end
|
#inspect ⇒ Object
113
114
115
|
# File 'lib/lightspeed/resource.rb', line 113
def inspect
"#<#{self.class.name} API#{base_path}>"
end
|
#load ⇒ Object
68
69
70
|
# File 'lib/lightspeed/resource.rb', line 68
def load
self.attributes = get[resource_name] if (attributes.keys - [self.class.id_field]).empty?
end
|
#read_attribute_for_serialization(method_name) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/lightspeed/resource.rb', line 143
def read_attribute_for_serialization(method_name)
method_name = method_name.to_sym
if self.class.fields.include?(method_name) || self.class.relationships.include?(method_name)
send(method_name)
end
end
|
#reload ⇒ Object
72
73
74
|
# File 'lib/lightspeed/resource.rb', line 72
def reload
self.attributes = get[resource_name]
end
|
#resource_name ⇒ Object
139
140
141
|
# File 'lib/lightspeed/resource.rb', line 139
def resource_name
self.class.resource_name
end
|
#singular_path_parent ⇒ Object
135
136
137
|
# File 'lib/lightspeed/resource.rb', line 135
def singular_path_parent
context
end
|
#to_json(*args) ⇒ Object
118
119
120
|
# File 'lib/lightspeed/resource.rb', line 118
def to_json(*args)
as_json.to_json(*args)
end
|
#update(attributes = {}) ⇒ Object
76
77
78
|
# File 'lib/lightspeed/resource.rb', line 76
def update(attributes = {})
self.attributes = put(body: attributes.to_json)[resource_name]
end
|