Class: EcwidApi::Entity
- Inherits:
-
Object
- Object
- EcwidApi::Entity
- Defined in:
- lib/ecwid_api/entity.rb
Direct Known Subclasses
Category, Customer, Order, OrderItem, Person, Product, ProductCombination, ProductType, ProductTypeAttribute
Class Attribute Summary collapse
-
.url_root ⇒ Object
Returns the value of attribute url_root.
Class Method Summary collapse
- .define_accessor(attribute, &block) ⇒ Object
-
.ecwid_accessor(*attrs) ⇒ Object
Public: Creates a snake_case accessor method from an Ecwid property name.
-
.ecwid_reader(*attrs) ⇒ Object
Public: Creates a snake_case access method from an Ecwid property name.
-
.ecwid_writer(*attrs) ⇒ Object
Public: Creates a snake_case writer method from an Ecwid property name.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Object
Public: Returns a property of the data (actual property name).
- #assign_attributes(attributes) ⇒ Object
- #assign_raw_attributes(attributes) ⇒ Object
-
#destroy! ⇒ Object
Public: Destroys the Entity.
-
#initialize(data, opts = {}) ⇒ Entity
constructor
Public: Initialize a new entity with a reference to the client and data.
- #marshal_dump ⇒ Object
- #marshal_load(array) ⇒ Object
-
#save ⇒ Object
Public: Saves the Entity.
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #update_attributes(attributes) ⇒ Object
- #update_raw_attributes(attributes) ⇒ Object
-
#url ⇒ Object
Public: The URL of the entity.
Constructor Details
#initialize(data, opts = {}) ⇒ Entity
Public: Initialize a new entity with a reference to the client and data
data - A Hash of data that represents the properties of this Entity opts - A Hash of options
:client - The EcwidApi::Client creating the Entity
92 93 94 95 |
# File 'lib/ecwid_api/entity.rb', line 92 def initialize(data, opts={}) @client, @data = opts[:client], data @new_data = {} end |
Class Attribute Details
.url_root ⇒ Object
Returns the value of attribute url_root.
11 12 13 |
# File 'lib/ecwid_api/entity.rb', line 11 def url_root @url_root end |
Class Method Details
.define_accessor(attribute, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ecwid_api/entity.rb', line 13 def define_accessor(attribute, &block) if const_defined?(:Accessors, false) mod = const_get(:Accessors) else mod = const_set(:Accessors, Module.new) include mod end mod.module_eval do define_method(attribute, &block) end end |
.ecwid_accessor(*attrs) ⇒ Object
80 81 82 83 |
# File 'lib/ecwid_api/entity.rb', line 80 def ecwid_accessor(*attrs) ecwid_reader(*attrs) ecwid_writer(*attrs) end |
.ecwid_reader(*attrs) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ecwid_api/entity.rb', line 39 def ecwid_reader(*attrs) attrs.map(&:to_s).each do |attribute| method = attribute.underscore define_accessor(method) do self[attribute] end unless method_defined?(attribute.underscore) end end |
.ecwid_writer(*attrs) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/ecwid_api/entity.rb', line 59 def ecwid_writer(*attrs) attrs.map(&:to_s).each do |attribute| method = "#{attribute.underscore}=" define_accessor(method) do |value| @new_data[attribute] = value end unless method_defined?(method) end end |
Instance Method Details
#==(other) ⇒ Object
185 186 187 |
# File 'lib/ecwid_api/entity.rb', line 185 def ==(other) data == other.data && new_data == other.new_data end |
#[](key) ⇒ Object
Public: Returns a property of the data (actual property name)
key - A Symbol or String of the property. The key should be the actual
key according to the Ecwid API documentation for the given entity.
Typically, this is camel cased.
Examples
entity[:parentId]
entity["parentId"]
Returns the value of the property, or nil if it doesn’t exist
109 110 111 |
# File 'lib/ecwid_api/entity.rb', line 109 def [](key) @new_data[key.to_s] || data[key.to_s] end |
#assign_attributes(attributes) ⇒ Object
127 128 129 130 131 |
# File 'lib/ecwid_api/entity.rb', line 127 def assign_attributes(attributes) attributes.each do |key, val| send("#{key}=", val) end end |
#assign_raw_attributes(attributes) ⇒ Object
133 134 135 136 137 |
# File 'lib/ecwid_api/entity.rb', line 133 def assign_raw_attributes(attributes) attributes.each do |key, val| @new_data[key.to_s] = val end end |
#destroy! ⇒ Object
Public: Destroys the Entity
165 166 167 |
# File 'lib/ecwid_api/entity.rb', line 165 def destroy! client.delete(url) end |
#marshal_dump ⇒ Object
177 178 179 |
# File 'lib/ecwid_api/entity.rb', line 177 def marshal_dump [@data, @new_data] end |
#marshal_load(array) ⇒ Object
181 182 183 |
# File 'lib/ecwid_api/entity.rb', line 181 def marshal_load(array) @data, @new_data = *array end |
#save ⇒ Object
Public: Saves the Entity
Saves anything stored in the @new_data hash
path - the URL of the entity
155 156 157 158 159 160 161 162 |
# File 'lib/ecwid_api/entity.rb', line 155 def save unless @new_data.empty? client.put(url, @new_data).tap do |response| @data.merge!(@new_data) @new_data.clear end end end |
#to_hash ⇒ Object
169 170 171 |
# File 'lib/ecwid_api/entity.rb', line 169 def to_hash data end |
#to_json(*args) ⇒ Object
173 174 175 |
# File 'lib/ecwid_api/entity.rb', line 173 def to_json(*args) data.to_json(*args) end |
#update_attributes(attributes) ⇒ Object
139 140 141 142 |
# File 'lib/ecwid_api/entity.rb', line 139 def update_attributes(attributes) assign_attributes(attributes) save end |
#update_raw_attributes(attributes) ⇒ Object
144 145 146 147 |
# File 'lib/ecwid_api/entity.rb', line 144 def update_raw_attributes(attributes) assign_raw_attributes(attributes) save end |
#url ⇒ Object
Public: The URL of the entity
Returns a String that is the URL of the entity
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ecwid_api/entity.rb', line 116 def url url_root = self.class.url_root raise Error.new("Please specify a url_root for the #{self.class.to_s}") unless url_root if url_root.respond_to?(:call) url_root = instance_exec(&url_root) end url_root + "/#{id}" end |