class EcwidApi::Entity

Attributes

url_root[RW]
client[R]
data[R]

Private: Gets the Hash of data

new_data[R]

Private: Gets the Hash of data

Public Class Methods

ecwid_accessor(*attrs) click to toggle source

Public: Creates a snake_case accessor method from an Ecwid property name

Example

class Product < Entity
  ecwid_accessor :inStock
end

product = client.products.find(12)
product.in_stock
product.in_stock = true
# File lib/ecwid_api/entity.rb, line 80
def ecwid_accessor(*attrs)
  ecwid_reader(*attrs)
  ecwid_writer(*attrs)
end
ecwid_reader(*attrs) click to toggle source

Public: Creates a snake_case access method from an Ecwid property name

Example

class Product < Entity
  ecwid_reader :id, :inStock
end

product = client.products.find(12)
product.in_stock
# 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) click to toggle source

Public: Creates a snake_case writer method from an Ecwid property name

Example

class Product < Entity
  ecwid_writer :inStock
end

product = client.products.find(12)
product.in_stock = true
# 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
new(data, opts={}) click to toggle source

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
# File lib/ecwid_api/entity.rb, line 92
def initialize(data, opts={})
  @client, @data = opts[:client], data
  @new_data = {}
end

Private Class Methods

define_accessor(attribute, &block) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/ecwid_api/entity.rb, line 185
def ==(other)
  data == other.data && new_data == other.new_data
end
[](key) click to toggle source

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

# File lib/ecwid_api/entity.rb, line 109
def [](key)
  @new_data[key.to_s] || data[key.to_s]
end
assign_attributes(attributes) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source

Public: Destroys the Entity

# File lib/ecwid_api/entity.rb, line 165
def destroy!
  client.delete(url)
end
marshal_dump() click to toggle source
# File lib/ecwid_api/entity.rb, line 177
def marshal_dump
  [@data, @new_data]
end
marshal_load(array) click to toggle source
# File lib/ecwid_api/entity.rb, line 181
def marshal_load(array)
  @data, @new_data = *array
end
save() click to toggle source

Public: Saves the Entity

Saves anything stored in the @new_data hash

path - the URL of the entity

# 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() click to toggle source
# File lib/ecwid_api/entity.rb, line 169
def to_hash
  data
end
to_json(*args) click to toggle source
# File lib/ecwid_api/entity.rb, line 173
def to_json(*args)
  data.to_json(*args)
end
update_attributes(attributes) click to toggle source
# File lib/ecwid_api/entity.rb, line 139
def update_attributes(attributes)
  assign_attributes(attributes)
  save
end
update_raw_attributes(attributes) click to toggle source
# File lib/ecwid_api/entity.rb, line 144
def update_raw_attributes(attributes)
  assign_raw_attributes(attributes)
  save
end
url() click to toggle source

Public: The URL of the entity

Returns a String that is the URL of the entity

# 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