goods 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 587e410504981ece079d24dbbc15a0408ca54985
4
- data.tar.gz: d34b7f6dcbac3169e10ba29fb01bda914c5f0736
3
+ metadata.gz: 6ba028ffc9f2ea9eeec008971060a0b66c2c99ea
4
+ data.tar.gz: 1e80a10fd4e52f22c57789994592ce09c62added
5
5
  SHA512:
6
- metadata.gz: b94d34341aaab7c7319c85dd0df6614e1c769acdc9490c27cc00a45e38c80cd237cbd3f0a269c1df1dc65f8cfcc4fbf190083bc4264d459d692771efd881aa57
7
- data.tar.gz: 216ac093e960bf563e6262cd0973d5ae5152ff47846c6e7e6051a8aaa4eb0fad5e4668b78a6c872f62348a2c73e028b71199518e22da686dbcc4d2bbffa3fd1d
6
+ metadata.gz: ca29741ba878d8c0a29e43c256737d606ed011babe5f0a73102611c4c9bef3d94ca67276f757240a2649ba285d77bf7a5b012ccdaf2c0f62e480102e9bd2cf2b
7
+ data.tar.gz: cc1780dca99d100667402dd7b7ac541b545d853232b71af992cffb22ef8c51b8b64c027134e10abb49959091f698476ecc42f96d0ee62df51e19f3074e9bf6d9
@@ -1,12 +1,10 @@
1
1
  module Goods
2
- class Category
3
- include Containable
2
+ class Category < Element
4
3
  attr_accessor :parent
5
- attr_field :parent_id
6
- attr_field :name
4
+ attr_field :parent_id, :name
7
5
 
8
6
  def initialize(info_hash)
9
- self._info_hash = info_hash
7
+ super
10
8
  @parent_at_level = {}
11
9
  end
12
10
 
@@ -1,18 +1,6 @@
1
1
  module Goods
2
- class Currency
3
- include Containable
4
-
5
- def initialize(info_hash)
6
- self._info_hash = info_hash
7
- end
8
-
9
- def rate
10
- @rate ||= _info_hash[:rate].to_f
11
- end
12
-
13
- def plus
14
- @plus ||= _info_hash[:plus].to_f
15
- end
2
+ class Currency < Element
3
+ attr_field :rate, :plus, type: :float
16
4
 
17
5
  def in(other_currency)
18
6
  self.rate/other_currency.rate
@@ -0,0 +1,78 @@
1
+ module Goods
2
+ class Element
3
+
4
+ def self.generate_accessor(field_name, type)
5
+ define_method field_name do
6
+ if field = instance_variable_get("@#{field_name}")
7
+ field
8
+ else
9
+ setup_instance_variable(field_name, type)
10
+ instance_variable_get("@#{field_name}")
11
+ end
12
+ end
13
+ end
14
+
15
+ # Class macro that defined mathods to access instance variables @some_field
16
+ # in the following way (illustrative example)
17
+ #
18
+ # attr_field :field_name
19
+ #
20
+ # is equivalent to
21
+ #
22
+ # def field_name
23
+ # @field_name ||= description[:field_name]
24
+ # end
25
+ #
26
+ # This method can take several arguments with optional hash at the end.
27
+ # Semantics is the same as with attr_* family of methods.
28
+ #
29
+ # You can specify the type of field by providin {type: :some_type} hash as
30
+ # the last argument. For example type: :float will call #to_f on resulting
31
+ # value.
32
+ def self.attr_field(*args)
33
+ opts = args.last.is_a?(Hash) ? args.pop : {}
34
+ type = opts[:type] || :string
35
+
36
+ args.each do |field_name|
37
+ self.generate_accessor(field_name, type)
38
+ end
39
+ end
40
+
41
+ def initialize(info_hash)
42
+ @info_hash = info_hash
43
+ end
44
+
45
+ attr_field :id
46
+
47
+ def invalid_fields
48
+ @invalid_fields ||= []
49
+ end
50
+
51
+ def valid?
52
+ reset_validation
53
+ apply_validation_rules
54
+ invalid_fields.empty?
55
+ end
56
+
57
+ private
58
+
59
+ def reset_validation
60
+ invalid_fields.clear
61
+ end
62
+
63
+ def validate(field, predicate)
64
+ invalid_fields << field unless predicate.call(send(field))
65
+ end
66
+
67
+ def setup_instance_variable(name, type)
68
+ case type
69
+ when :string
70
+ instance_variable_set("@#{name}", @info_hash[name])
71
+ when :float
72
+ instance_variable_set("@#{name}", @info_hash[name].to_f)
73
+ else
74
+ raise ArgumentError, 'wrong type'
75
+ end
76
+ end
77
+ end
78
+ end
data/lib/goods/offer.rb CHANGED
@@ -1,21 +1,9 @@
1
1
  module Goods
2
- class Offer
3
- include Containable
2
+ class Offer < Element
4
3
  attr_accessor :category, :currency, :price
5
- attr_field :category_id
6
- attr_field :currency_id
7
- attr_field :available
8
- attr_field :description
9
- attr_field :model
10
- attr_field :name
11
- attr_field :picture
12
- attr_field :vendor
13
- attr_field :url
14
-
15
- def initialize(info_hash)
16
- self._info_hash = info_hash
17
- @price = _info_hash[:price].to_f
18
- end
4
+ attr_field :category_id, :currency_id, :available, :description,
5
+ :model, :name, :picture, :vendor, :url
6
+ attr_field :price, type: :float
19
7
 
20
8
  def convert_currency(other_currency)
21
9
  self.price *= currency.in(other_currency)
@@ -28,6 +16,10 @@ module Goods
28
16
  @category_id = other_category.id
29
17
  end
30
18
 
19
+ def price=(price)
20
+ @price = price
21
+ end
22
+
31
23
  private
32
24
 
33
25
  def apply_validation_rules
@@ -40,5 +32,6 @@ module Goods
40
32
  }
41
33
  validate :price, proc { |price| price && price > 0 }
42
34
  end
35
+
43
36
  end
44
37
  end
data/lib/goods/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Goods
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/goods/xml.rb CHANGED
@@ -20,7 +20,7 @@ module Goods
20
20
  @offers ||= extract_offers
21
21
  end
22
22
 
23
- def generation_date
23
+ def generation_date
24
24
  @generation_date ||= extract_catalog_generation_date
25
25
  end
26
26
 
data/lib/goods.rb CHANGED
@@ -2,7 +2,7 @@ require 'open-uri'
2
2
  require "goods/version"
3
3
  require "goods/xml/validator"
4
4
  require "goods/xml"
5
- require "goods/containable"
5
+ require "goods/element"
6
6
  require "goods/container"
7
7
  require "goods/category"
8
8
  require "goods/categories_list"
@@ -4,7 +4,7 @@ describe Goods::Category do
4
4
  let(:valid_description) { {id: "1", name: "Name"} }
5
5
  let(:root) { Goods::Category.new valid_description }
6
6
 
7
- it_should_behave_like "containable" do
7
+ it_should_behave_like "element" do
8
8
  let(:element) { Goods::Category.new(valid_description) }
9
9
  end
10
10
 
@@ -4,7 +4,7 @@ describe Goods::Currency do
4
4
  let(:valid_description) { {id: "VAL_CUR", rate: 1, plus: 0} }
5
5
  let(:valid_currency) { Goods::Currency.new(valid_description) }
6
6
 
7
- it_should_behave_like "containable" do
7
+ it_should_behave_like "element" do
8
8
  let(:element) { valid_currency }
9
9
  end
10
10
 
@@ -13,7 +13,7 @@ describe Goods::Offer do
13
13
  offer
14
14
  end
15
15
 
16
- it_should_behave_like "containable" do
16
+ it_should_behave_like "element" do
17
17
  let(:element) { valid_offer }
18
18
  end
19
19
 
@@ -1,4 +1,4 @@
1
- shared_examples "containable" do
1
+ shared_examples "element" do
2
2
  [:id, :invalid_fields].each do |prop|
3
3
  it "should give access to #{prop}" do
4
4
  expect(element).to respond_to(prop)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Pyanykh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-29 00:00:00.000000000 Z
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,10 +101,10 @@ files:
101
101
  - lib/goods/catalog.rb
102
102
  - lib/goods/categories_list.rb
103
103
  - lib/goods/category.rb
104
- - lib/goods/containable.rb
105
104
  - lib/goods/container.rb
106
105
  - lib/goods/currencies_list.rb
107
106
  - lib/goods/currency.rb
107
+ - lib/goods/element.rb
108
108
  - lib/goods/offer.rb
109
109
  - lib/goods/offers_list.rb
110
110
  - lib/goods/version.rb
@@ -124,8 +124,8 @@ files:
124
124
  - spec/goods/xml_spec.rb
125
125
  - spec/goods_spec.rb
126
126
  - spec/spec_helper.rb
127
- - spec/support/shared_examples_for_containable.rb
128
127
  - spec/support/shared_examples_for_container.rb
128
+ - spec/support/shared_examples_for_element.rb
129
129
  homepage: https://github.com/ArtemPyanykh/goods
130
130
  licenses:
131
131
  - MIT
@@ -164,5 +164,5 @@ test_files:
164
164
  - spec/goods/xml_spec.rb
165
165
  - spec/goods_spec.rb
166
166
  - spec/spec_helper.rb
167
- - spec/support/shared_examples_for_containable.rb
168
167
  - spec/support/shared_examples_for_container.rb
168
+ - spec/support/shared_examples_for_element.rb
@@ -1,56 +0,0 @@
1
- module Goods
2
- module Containable
3
- def self.included(base)
4
- # Class macro that defined mathod to access instance variable @field_name
5
- # in the following way (illustrative example)
6
- #
7
- # def field_name
8
- # @field_name ||= description[field_name]
9
- # end
10
- base.define_singleton_method :attr_field do |field_name|
11
- define_method field_name do
12
- if field = instance_variable_get("@#{field_name}")
13
- field
14
- else
15
- instance_variable_set("@#{field_name}", _info_hash[field_name])
16
- instance_variable_get("@#{field_name}")
17
- end
18
- end
19
- end
20
- end
21
-
22
- def _info_hash
23
- @_info_hash
24
- end
25
- private :_info_hash
26
-
27
- def id
28
- @id ||= _info_hash[:id]
29
- end
30
-
31
- def invalid_fields
32
- @invalid_fields ||= []
33
- end
34
-
35
- def valid?
36
- reset_validation
37
- apply_validation_rules
38
- invalid_fields.empty?
39
- end
40
-
41
- private
42
-
43
- def reset_validation
44
- invalid_fields.clear
45
- end
46
-
47
- def validate(field, predicate)
48
- invalid_fields << field unless predicate.call(send(field))
49
- end
50
-
51
- def _info_hash=(info_hash)
52
- @_info_hash = info_hash
53
- end
54
- private :_info_hash=
55
- end
56
- end