Class: Magenthor::Product
Instance Attribute Summary collapse
-
#product_id ⇒ Object
readonly
Returns the value of attribute product_id.
Class Method Summary collapse
-
.info(product_id, store_view = '') ⇒ Magenthor::Product
Retrieve the information about a specific product.
-
.list(filters = [], store_view = '') ⇒ Array<Magenthor::Product>
Retrieve the list of Magento product based on filters and store view.
Instance Method Summary collapse
-
#create ⇒ TrueClass, FalseClass
Create on Magento a new product base on the local one.
-
#delete ⇒ TrueClass, FalseClass
Delete from Magento the local product.
-
#initialize(set_id) ⇒ Magenthor::Product
constructor
Initialize a new product entity based on an attribute set id.
-
#update ⇒ TrueClass, FalseClass
Save on Magento the updates made on the local product.
Methods inherited from Base
Constructor Details
#initialize(set_id) ⇒ Magenthor::Product
Initialize a new product entity based on an attribute set id
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/magenthor/product.rb', line 15 def initialize set_id attributes = Magenthor::Catalog.attribute_list set_id attributes.each do |attr| if attr["code"] != 'product_id' and attr["code"] != 'created_at' singleton_class.class_eval do; attr_accessor attr["code"]; end send(attr["code"]+"=", nil) end end singleton_class.class_eval do; attr_accessor "type"; end singleton_class.class_eval do; attr_accessor "set"; end self.set = set_id self.type = nil self.product_id = nil end |
Instance Attribute Details
#product_id ⇒ Object
Returns the value of attribute product_id.
5 6 7 |
# File 'lib/magenthor/product.rb', line 5 def product_id @product_id end |
Class Method Details
.info(product_id, store_view = '') ⇒ Magenthor::Product
Retrieve the information about a specific product
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/magenthor/product.rb', line 101 def info product_id, store_view = '' response = commit('catalog_product.info', [product_id, store_view]) return false if response == false obj = new response["set"] response.each do |key, value| obj.class.class_eval do; attr_accessor key; end unless obj.respond_to?(key) obj.send(key+"=", value) end return obj end |
.list(filters = [], store_view = '') ⇒ Array<Magenthor::Product>
Retrieve the list of Magento product based on filters and store view
86 87 88 89 90 91 92 93 94 |
# File 'lib/magenthor/product.rb', line 86 def list filters = [], store_view = '' response = commit('catalog_product.list', [filters, store_view]) return false if response == false products = [] response.each do |p| products << Magenthor::Product.info(p["product_id"], store_view) end return products end |
Instance Method Details
#create ⇒ TrueClass, FalseClass
Create on Magento a new product base on the local one
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/magenthor/product.rb', line 44 def create if self.type.nil? or self.set.nil? or self.sku.nil? puts "Type, set and sku are mandatory" return false end attributes = {} methods.grep(/\w=$/).each do |m| value = send(m.to_s.gsub('=','')) attributes[m.to_s.gsub('=','')] = value unless value.nil? end response = self.class.commit('catalog_product.create', [self.type, self.set, self.sku, attributes]) return false if response == false obj = self.class.info response methods.grep(/\w=$/).each do |m| send(m, obj.send(m.to_s.gsub('=',''))) end self.product_id = obj.product_id return true end |
#delete ⇒ TrueClass, FalseClass
Delete from Magento the local product
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/magenthor/product.rb', line 67 def delete response = self.class.commit('catalog_product.delete', [self.product_id]) return false if response == false methods.grep(/\w=$/).each do |m| send(m, nil) end self.product_id = nil return true end |
#update ⇒ TrueClass, FalseClass
Save on Magento the updates made on the local product
33 34 35 36 37 38 39 |
# File 'lib/magenthor/product.rb', line 33 def update attributes = {} methods.grep(/\w=$/).each do |m| attributes[m.to_s.gsub('=','')] = send(m.to_s.gsub('=','')) end self.class.commit('catalog_product.update', [self.product_id, attributes]) end |