Class: Lightspeed::Resource
- Inherits:
-
Object
- Object
- Lightspeed::Resource
show all
- Defined in:
- lib/lightspeed/resource.rb
Direct Known Subclasses
Account, Category, Customer, Employee, Image, Inventory, Item, ItemAttributeSet, ItemMatrix, Order, PriceLevel, 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
90
91
92
|
# File 'lib/lightspeed/resource.rb', line 90
def self.collection_name
resource_name.pluralize
end
|
.fields(fields = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# 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
94
95
96
|
# File 'lib/lightspeed/resource.rb', line 94
def self.id_field
"#{resource_name.camelize(:lower)}ID"
end
|
.relationships(*args) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/lightspeed/resource.rb', line 102
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
86
87
88
|
# File 'lib/lightspeed/resource.rb', line 86
def self.resource_name
name.demodulize
end
|
Instance Method Details
#as_json ⇒ Object
Also known as:
to_h
123
124
125
|
# File 'lib/lightspeed/resource.rb', line 123
def as_json
fields_to_h.merge(relationships_to_h).reject { |_, v| v.nil? || v == {} }
end
|
#base_path ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/lightspeed/resource.rb', line 128
def base_path
if context.is_a?(Lightspeed::Collection)
"#{context.base_path}/#{id}"
else
"#{account.base_path}/#{resource_name}/#{id}"
end
end
|
#destroy ⇒ Object
81
82
83
84
|
# File 'lib/lightspeed/resource.rb', line 81
def destroy
self.attributes = delete[resource_name]
self
end
|
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/lightspeed/resource.rb', line 46
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(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
98
99
100
|
# File 'lib/lightspeed/resource.rb', line 98
def id_params
{ self.class.id_field => id }
end
|
#inspect ⇒ Object
114
115
116
|
# File 'lib/lightspeed/resource.rb', line 114
def inspect
"#<#{self.class.name} API#{base_path}>"
end
|
#load ⇒ Object
69
70
71
|
# File 'lib/lightspeed/resource.rb', line 69
def load
self.attributes = get[resource_name] if (attributes.keys - [self.class.id_field]).empty?
end
|
#read_attribute_for_serialization(method_name) ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/lightspeed/resource.rb', line 144
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
73
74
75
|
# File 'lib/lightspeed/resource.rb', line 73
def reload
self.attributes = get[resource_name]
end
|
#resource_name ⇒ Object
140
141
142
|
# File 'lib/lightspeed/resource.rb', line 140
def resource_name
self.class.resource_name
end
|
#singular_path_parent ⇒ Object
136
137
138
|
# File 'lib/lightspeed/resource.rb', line 136
def singular_path_parent
context
end
|
#to_json ⇒ Object
119
120
121
|
# File 'lib/lightspeed/resource.rb', line 119
def to_json
Yajl::Encoder.encode(as_json)
end
|
#update(attributes = {}) ⇒ Object
77
78
79
|
# File 'lib/lightspeed/resource.rb', line 77
def update(attributes = {})
self.attributes = put(body: Yajl::Encoder.encode(attributes))[resource_name]
end
|