Module: ActsAsElibriProduct::ClassMethods
- Defined in:
- lib/acts_as_elibri_product.rb
Constant Summary collapse
- @@traverse_vector =
{}
- @@policy_chain =
[]
Class Method Summary collapse
Instance Method Summary collapse
- #acts_as_elibri_product(traverse_vector = {}) ⇒ Object
- #batch_create_or_update_from_elibri(xml_string, tracing_object = nil) ⇒ Object
- #create_from_elibri(xml_string, tracing_object = nil) ⇒ Object
- #create_or_update_from_elibri(xml_string, tracing_object = nil) ⇒ Object
- #policy_chain ⇒ Object
- #validate_policy_chain ⇒ Object
Class Method Details
.traverse_vector ⇒ Object
14 15 16 |
# File 'lib/acts_as_elibri_product.rb', line 14 def self.traverse_vector @@traverse_vector end |
Instance Method Details
#acts_as_elibri_product(traverse_vector = {}) ⇒ Object
20 21 22 |
# File 'lib/acts_as_elibri_product.rb', line 20 def acts_as_elibri_product(traverse_vector = {}) @@traverse_vector = traverse_vector end |
#batch_create_or_update_from_elibri(xml_string, tracing_object = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/acts_as_elibri_product.rb', line 68 def batch_create_or_update_from_elibri(xml_string, tracing_object = nil) if xml_string.is_a?(Elibri::ONIX::Release_3_0::ONIXMessage) recreated_products = xml_string else recreated_products = Elibri::ONIX::Release_3_0::ONIXMessage.new(xml_string) end recreated_products.products.each do |product| xml = product.to_xml.to_s dialect = product.elibri_dialect header = "<?xml version='1.0' encoding='UTF-8'?> <ONIXMessage xmlns:elibri='http://elibri.com.pl/ns/extensions' xmlns='`http://www.editeur.org/onix/3.0/reference' release='3.0'> <elibri:Dialect>#{dialect}</elibri:Dialect>" xml = header + xml + "</ONIXMessage>" create_or_update_from_elibri(xml, tracing_object) end end |
#create_from_elibri(xml_string, tracing_object = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/acts_as_elibri_product.rb', line 38 def create_from_elibri(xml_string, tracing_object = nil) product = Elibri::ONIX::Release_3_0::ONIXMessage.new(xml_string).products.first db_product = self.new @@traverse_vector.each_pair do |k, v| if v.is_a?(Symbol) db_product.send(:write_attribute, v, product.send(k)) elsif v.is_a?(Hash) object = db_product.send(v.keys.first) ActsAsElibriProduct.set_objects_from_array(k, v.keys.first, v.values.first, product.send(k), db_product) if product.send(k) elsif v.is_a?(Array) if v[0].nil? v[1].call(db_product, product.send(k)) else db_product.send(:write_attribute, v[0], v[1].call(db_product, product.send(k))) end end end db_product.old_xml = xml_string db_product.save end |
#create_or_update_from_elibri(xml_string, tracing_object = nil) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/acts_as_elibri_product.rb', line 59 def create_or_update_from_elibri(xml_string, tracing_object=nil) recreated_product = Elibri::ONIX::Release_3_0::ONIXMessage.new(xml_string).products.first if Product.find(:first, :conditions => {:record_reference => recreated_product.record_reference}) #update Product.find(:first, :conditions => {:record_reference => recreated_product.record_reference}).update_product_from_elibri(xml_string, tracing_object) else Product.create_from_elibri(xml_string, tracing_object) end end |
#policy_chain ⇒ Object
26 27 28 29 |
# File 'lib/acts_as_elibri_product.rb', line 26 def policy_chain @@policy_chain ### policy musi przyjmowac cztery argumenty -> nazwa obiektu (jesli main level to product po prostu), nazwa atrybutu, wartosc przed, wartosc po end |
#validate_policy_chain ⇒ Object
31 32 33 34 35 36 |
# File 'lib/acts_as_elibri_product.rb', line 31 def validate_policy_chain @@policy_chain.each do |policy| raise "Policy #{policy} don't respond to call method" unless policy.respond_to? :call end return true end |