acts_as_elibri_product 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +51 -0
- data/lib/acts_as_elibri_product/version.rb +1 -1
- data/lib/acts_as_elibri_product.rb +46 -3
- metadata +22 -8
data/README.md
CHANGED
@@ -36,6 +36,13 @@ Example:
|
|
36
36
|
}
|
37
37
|
|
38
38
|
Important thing is that lambda need to take exactly one argument, and need to return final value of attribute to write into database.
|
39
|
+
Also very important thing is that lambdas are now supported only on first level of vector (not in embedded hashes).
|
40
|
+
|
41
|
+
If you want to use lambda, but don't write anything to database, you should use nil as first attribute in array. For example:
|
42
|
+
```ruby
|
43
|
+
{
|
44
|
+
:title => [nil, lambda {|x| Logger.info("changing title"); return "#{x}_test"}]
|
45
|
+
}
|
39
46
|
|
40
47
|
When dealing with embedded objects and relations, you should use embedded hashes:
|
41
48
|
|
@@ -104,4 +111,48 @@ example vector:
|
|
104
111
|
:name => :name
|
105
112
|
}
|
106
113
|
}
|
114
|
+
```
|
115
|
+
|
116
|
+
Also if you want to check for some particular changes in object - you can use policy chain.
|
117
|
+
|
118
|
+
```
|
119
|
+
policy_chain << PolicyObject
|
120
|
+
policy_chain << lambda
|
121
|
+
```
|
122
|
+
|
123
|
+
PolicyObject must implement class method call, which take four arguments:
|
124
|
+
|
125
|
+
* object is symbol of object that inside which attribute is changing - it should be our name, not elibri one, for example on main level it would :product, inside contributors it will be :contributors etc.
|
126
|
+
|
127
|
+
* attribute is symbol of attribute that will change - it should be our name, not elibri one
|
128
|
+
|
129
|
+
* pre - value of attribute before potential update
|
130
|
+
|
131
|
+
* post - value of attribute after potential update
|
132
|
+
|
133
|
+
Also policy must return one of two values - true (if allow of changing an attribute) or false (if attribute should remain unchanged). You can implement notification etc inside Policy, when attribute is discarded - it's up to you.
|
134
|
+
|
135
|
+
example of Policy (stub):
|
136
|
+
```ruby
|
137
|
+
module Policy #module is optional - however it is good practice to keep all policies in Policy module, and in subdirectory inside lib
|
138
|
+
class Stub
|
139
|
+
|
140
|
+
def self.call(object, attribute, pre, post)
|
141
|
+
### Add policy body here
|
142
|
+
return true
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
example of adding policy lambda:
|
149
|
+
```ruby
|
150
|
+
policy_chain << lambda do |object, attribute, pre, post|
|
151
|
+
if object == :product
|
152
|
+
if attribute == :price_amount
|
153
|
+
return false unless pre == post #won't allow change of product price
|
154
|
+
end
|
155
|
+
end
|
156
|
+
return true
|
157
|
+
end
|
107
158
|
```
|
@@ -19,6 +19,20 @@ module ActsAsElibriProduct
|
|
19
19
|
@@traverse_vector = traverse_vector
|
20
20
|
end
|
21
21
|
|
22
|
+
@@policy_chain = []
|
23
|
+
|
24
|
+
def policy_chain
|
25
|
+
@@policy_chain
|
26
|
+
### policy musi przyjmowac cztery argumenty -> nazwa obiektu (jesli main level to product po prostu), nazwa atrybutu, wartosc przed, wartosc po
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_policy_chain
|
30
|
+
@@policy_chain.each do |policy|
|
31
|
+
raise "Policy #{policy} don't respond to call method" unless policy.respond_to? :call
|
32
|
+
end
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
|
22
36
|
def create_from_elibri(xml_string)
|
23
37
|
product = Elibri::ONIX::Release_3_0::ONIXMessage.from_xml(xml_string).products.first
|
24
38
|
db_product = self.new
|
@@ -29,7 +43,11 @@ module ActsAsElibriProduct
|
|
29
43
|
object = db_product.send(v.keys.first)
|
30
44
|
ActsAsElibriProduct.set_objects_from_array(k, v.keys.first, v.values.first, product.send(k), db_product) if product.send(k)
|
31
45
|
elsif v.is_a?(Array)
|
32
|
-
|
46
|
+
if v[0].nil?
|
47
|
+
v[1].call(product.send(k))
|
48
|
+
else
|
49
|
+
db_product.send(:write_attribute, v[0], v[1].call(product.send(k)))
|
50
|
+
end
|
33
51
|
end
|
34
52
|
end
|
35
53
|
db_product.old_xml = xml_string
|
@@ -76,7 +94,17 @@ module ActsAsElibriProduct
|
|
76
94
|
details = elibri_xml_versions.diff
|
77
95
|
details[:changes].each do |change|
|
78
96
|
if change.is_a?(Symbol) && traverse_vector[change]
|
79
|
-
|
97
|
+
if traverse_vector[change].is_a?(Array)
|
98
|
+
next unless check_policy_chain(self, :product, traverse_vector[change], self.send(traverse_vector[change][0]), product_updated.send(change))
|
99
|
+
if traverse_vector[change][0].nil?
|
100
|
+
traverse_vector[change][1].call(product_updated.send(change))
|
101
|
+
else
|
102
|
+
write_attribute(traverse_vector[change][0], traverse_vector[change][1].call(product_updated.send(change)))
|
103
|
+
end
|
104
|
+
else
|
105
|
+
next unless check_policy_chain(self, :product, traverse_vector[change], self.send(traverse_vector[change]), product_updated.send(change))
|
106
|
+
write_attribute(traverse_vector[change], product_updated.send(change))
|
107
|
+
end
|
80
108
|
elsif change.is_a?(Hash) && traverse_vector[change.keys.first]
|
81
109
|
change.values.first.each do |elibri_attrib|
|
82
110
|
if elibri_attrib.is_a? Hash
|
@@ -85,13 +113,24 @@ module ActsAsElibriProduct
|
|
85
113
|
if db_attrib #found in mapping
|
86
114
|
object = self.send(traverse_vector[change.keys.first].keys.first).find { |x| x.import_id == k }
|
87
115
|
elibri_object = product_updated.send(change.keys.first).find { |x| x.id == k }
|
88
|
-
|
116
|
+
if v.is_a?(Array)
|
117
|
+
next unless check_policy_chain(self, traverse_vector[change.keys.first].keys.first, db_attrib, object.send(db_attrib), elibri_object.send(v[0]))
|
118
|
+
if v[0].nil?
|
119
|
+
v[1].call(elibri_object.send(v[0]))
|
120
|
+
else
|
121
|
+
object.send(:write_attribute, db_attrib, v[1].call(elibri_object.send(v[0])))
|
122
|
+
end
|
123
|
+
else
|
124
|
+
next unless check_policy_chain(self, traverse_vector[change.keys.first].keys.first, db_attrib, object.send(db_attrib), elibri_object.send(v))
|
125
|
+
object.send(:write_attribute, db_attrib, elibri_object.send(v))
|
126
|
+
end
|
89
127
|
end
|
90
128
|
end
|
91
129
|
else
|
92
130
|
db_attrib = traverse_vector[change.keys.first].values.first[elibri_attrib]
|
93
131
|
object = self.send(traverse_vector[change.keys.first].keys.first)
|
94
132
|
elibri_object = product_updated.send(change.keys.first)
|
133
|
+
next unless check_policy_chain(self, traverse_vector[change.keys.first].keys.first, db_attrib, object.send(db_attrib), elibri_object.send(elibri_attrib))
|
95
134
|
object.send(:write_attribute, db_attrib, elibri_object.send(elibri_attrib))
|
96
135
|
end
|
97
136
|
end
|
@@ -126,6 +165,10 @@ end
|
|
126
165
|
ActsAsElibriProduct::ClassMethods.traverse_vector
|
127
166
|
end
|
128
167
|
|
168
|
+
def check_policy_chain(product, object, attribute, pre, post)
|
169
|
+
product.class.policy_chain.all? { |policy| policy.call(object, attribute, pre, post) }
|
170
|
+
end
|
171
|
+
|
129
172
|
def self.set_objects_from_array(elibri_object_name, db_object_name, object_traverse_vector, elibri_objects, db_product)
|
130
173
|
if elibri_objects.is_a?(Array)
|
131
174
|
elibri_objects.each do |elibri_object|
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Piotr Szmielew
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-17 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rails
|
@@ -46,7 +46,7 @@ dependencies:
|
|
46
46
|
prerelease: false
|
47
47
|
requirement: *id002
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name:
|
49
|
+
name: elibri_onix_generator
|
50
50
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
@@ -60,7 +60,7 @@ dependencies:
|
|
60
60
|
prerelease: false
|
61
61
|
requirement: *id003
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: elibri_onix_mocks
|
64
64
|
version_requirements: &id004 !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
@@ -74,7 +74,7 @@ dependencies:
|
|
74
74
|
prerelease: false
|
75
75
|
requirement: *id004
|
76
76
|
- !ruby/object:Gem::Dependency
|
77
|
-
name: rspec
|
77
|
+
name: rspec
|
78
78
|
version_requirements: &id005 !ruby/object:Gem::Requirement
|
79
79
|
none: false
|
80
80
|
requirements:
|
@@ -88,7 +88,7 @@ dependencies:
|
|
88
88
|
prerelease: false
|
89
89
|
requirement: *id005
|
90
90
|
- !ruby/object:Gem::Dependency
|
91
|
-
name:
|
91
|
+
name: rspec-rails
|
92
92
|
version_requirements: &id006 !ruby/object:Gem::Requirement
|
93
93
|
none: false
|
94
94
|
requirements:
|
@@ -101,6 +101,20 @@ dependencies:
|
|
101
101
|
type: :development
|
102
102
|
prerelease: false
|
103
103
|
requirement: *id006
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: sqlite3
|
106
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
type: :development
|
116
|
+
prerelease: false
|
117
|
+
requirement: *id007
|
104
118
|
description: Gem designed to allow easy addition of eLibri based product to your application. Currently only tested under REE.
|
105
119
|
email:
|
106
120
|
- p.szmielew@ava.waw.pl
|