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

Class Method Details

.traverse_vectorObject



16
17
18
# File 'lib/acts_as_elibri_product.rb', line 16

def self.traverse_vector
  @@traverse_vector
end

Instance Method Details

#acts_as_elibri_product(traverse_vector = {}) ⇒ Object



22
23
24
# File 'lib/acts_as_elibri_product.rb', line 22

def acts_as_elibri_product(traverse_vector = {})
  @@traverse_vector = traverse_vector
end

#batch_create_or_update_from_elibri(xml_string, tracing_object = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/acts_as_elibri_product.rb', line 70

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/acts_as_elibri_product.rb', line 40

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



61
62
63
64
65
66
67
68
# File 'lib/acts_as_elibri_product.rb', line 61

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_chainObject



28
29
30
31
# File 'lib/acts_as_elibri_product.rb', line 28

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_chainObject



33
34
35
36
37
38
# File 'lib/acts_as_elibri_product.rb', line 33

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