Class: Lightspeed::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/lightspeed/resource.rb

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

#accountObject

Returns the value of attribute account.



13
14
15
# File 'lib/lightspeed/resource.rb', line 13

def 
  @account
end

#attributesObject

Returns the value of attribute attributes.



13
14
15
# File 'lib/lightspeed/resource.rb', line 13

def attributes
  @attributes
end

#clientObject

Returns the value of attribute client.



13
14
15
# File 'lib/lightspeed/resource.rb', line 13

def client
  @client
end

#contextObject

Returns the value of attribute context.



13
14
15
# File 'lib/lightspeed/resource.rb', line 13

def context
  @context
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/lightspeed/resource.rb', line 13

def id
  @id
end

Class Method Details

.collection_nameObject



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_fieldObject



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_nameObject



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_pathObject



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
    "#{.base_path}/#{resource_name}/#{id}"
  end
end

#destroyObject



80
81
82
83
# File 'lib/lightspeed/resource.rb', line 80

def destroy
  self.attributes = delete[resource_name]
  self
end

#get_transformed_value(name, kind) ⇒ Object



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_paramsObject



97
98
99
# File 'lib/lightspeed/resource.rb', line 97

def id_params
  { self.class.id_field => id }
end

#inspectObject



113
114
115
# File 'lib/lightspeed/resource.rb', line 113

def inspect
  "#<#{self.class.name} API#{base_path}>"
end

#loadObject



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

#reloadObject



72
73
74
# File 'lib/lightspeed/resource.rb', line 72

def reload
  self.attributes = get[resource_name]
end

#resource_nameObject



139
140
141
# File 'lib/lightspeed/resource.rb', line 139

def resource_name
  self.class.resource_name
end

#singular_path_parentObject



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