kosher 0.11.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
data/kosher.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  'activemodel' => '~> 3.0',
17
17
  'certainty' => '~> 0.2',
18
18
  'money' => '~> 3.7',
19
- 'structure' => '~> 0.18'
19
+ 'structure' => '~> 0.19'
20
20
  }.each do |lib, version|
21
21
  s.add_runtime_dependency lib, version
22
22
  end
@@ -0,0 +1,18 @@
1
+ module Kosher
2
+ class Detail < Structure
3
+ include Filter
4
+
5
+ key :condition, Integer
6
+ key :description, String, :default => ''
7
+
8
+ validates_inclusion_of :condition, :in => 1..6
9
+
10
+ def new?
11
+ condition == 1
12
+ end
13
+
14
+ def used?
15
+ !new?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module Kosher
2
+ module Filter
3
+ def self.included(base)
4
+ base.key :kosher, Boolean, :default => true
5
+ end
6
+
7
+ def kosher?
8
+ validate!
9
+ kosher
10
+ end
11
+ end
12
+ end
data/lib/kosher/offer.rb CHANGED
@@ -1,6 +1,9 @@
1
- require 'kosher/base'
1
+ require 'certainty'
2
+
3
+ require 'kosher/filter'
2
4
  require 'kosher/price'
3
5
 
6
+ require 'kosher/detail'
4
7
  require 'kosher/seller'
5
8
  require 'kosher/shipping'
6
9
  require 'kosher/unit'
@@ -10,12 +13,15 @@ module Kosher
10
13
  class Offer < Structure
11
14
  include Comparable
12
15
 
13
- key :id
14
- key :seller
15
- key :shipping
16
- key :unit
16
+ key :id, String
17
+ key :detail, Detail
18
+ key :seller, Seller
19
+ key :shipping, Shipping
20
+ key :unit, Unit
17
21
  key :venue_id, Integer
18
22
 
23
+ validates_presence_of :detail, :seller, :shipping, :unit, :venue
24
+
19
25
  def <=>(other)
20
26
  if kosher? != other.kosher?
21
27
  kosher? ? -1 : 1
@@ -25,7 +31,8 @@ module Kosher
25
31
  end
26
32
 
27
33
  def kosher?
28
- [seller, shipping, unit].all?(&:kosher?)
34
+ validate!
35
+ [seller, shipping, detail].all?(&:kosher?)
29
36
  end
30
37
 
31
38
  def price
data/lib/kosher/price.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'money'
2
+
1
3
  module Kosher
2
4
  module Price
3
5
  def self.included(base)
@@ -9,6 +11,7 @@ module Kosher
9
11
  end
10
12
 
11
13
  def price
14
+ validate!
12
15
  Money.new(cents, currency)
13
16
  end
14
17
  end
data/lib/kosher/seller.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  module Kosher
2
- class Seller < Base
3
- key :id
4
- key :name
2
+ class Seller < Structure
3
+ include Filter
4
+
5
+ key :id, String
6
+ key :name, String
5
7
  key :rating, Float
6
8
 
7
9
  validates_presence_of :name
@@ -1,5 +1,6 @@
1
1
  module Kosher
2
- class Shipping < Base
2
+ class Shipping < Structure
3
+ include Filter
3
4
  include Price
4
5
 
5
6
  key :available, Boolean
data/lib/kosher/unit.rb CHANGED
@@ -1,19 +1,7 @@
1
1
  module Kosher
2
- class Unit < Base
2
+ class Unit < Structure
3
3
  include Price
4
4
 
5
- key :condition, Integer
6
- key :description, String
7
- key :quantity, Integer, :default => 1
8
-
9
- validates_inclusion_of :condition, :in => 1..6
10
-
11
- def new?
12
- condition == 1
13
- end
14
-
15
- def used?
16
- !new?
17
- end
5
+ key :quantity, Integer, :default => 1
18
6
  end
19
7
  end
data/lib/kosher/venue.rb CHANGED
@@ -1,12 +1,34 @@
1
+ require 'yaml'
2
+
1
3
  module Kosher
2
4
  class Venue < Structure
3
- key :country
4
- key :name
5
+ DATA_PATH = File.expand_path("../../../data/venues.yml", __FILE__)
6
+
7
+ key :_id, Integer
8
+ key :country, String
9
+ key :name, String
10
+
11
+ class << self
12
+ include Enumerable
13
+
14
+ def all
15
+ @all ||= YAML.load_file(DATA_PATH).map do |hsh|
16
+ hsh['_id'] ||= hsh.delete('id')
17
+ new(hsh)
18
+ end
19
+ end
20
+
21
+ def amazon
22
+ @amazons ||= find_all { |e| e.name.include? 'Amazon' }
23
+ end
5
24
 
6
- set_data_file File.expand_path("../../../data/venues.yml", __FILE__)
25
+ def each(&block)
26
+ all.each { |item| block.call(item) }
27
+ end
7
28
 
8
- def self.amazon
9
- @amazons ||= find_all { |e| e.name.include? 'Amazon' }
29
+ def find(id)
30
+ super() { |item| item._id == id }
31
+ end
10
32
  end
11
33
  end
12
34
  end
@@ -1,3 +1,3 @@
1
1
  module Kosher
2
- VERSION = '0.11.1'
2
+ VERSION = '0.13.0'
3
3
  end
data/lib/kosher.rb CHANGED
@@ -1,6 +1,22 @@
1
1
  require 'active_model'
2
- require 'certainty'
3
- require 'money'
4
2
  require 'structure'
5
3
 
4
+ module Kosher
5
+ class NotValid < StandardError
6
+ def initialize(record)
7
+ super(record.errors.full_messages.join(', '))
8
+ end
9
+ end
10
+ end
11
+
12
+ class Structure
13
+ include ActiveModel::Validations
14
+
15
+ private
16
+
17
+ def validate!
18
+ raise NotValid.new(self) unless valid?
19
+ end
20
+ end
21
+
6
22
  require 'kosher/offer'
data/test/kosher_test.rb CHANGED
@@ -16,8 +16,9 @@ class TestKosher < MiniTest::Unit::TestCase
16
16
  :currency => 'EUR')
17
17
  assert_equal '10,00 €', unit.price.format
18
18
 
19
- shipping = Shipping.new(:cents => '300',
20
- :currency => 'EUR')
19
+ shipping = Shipping.new(:available => true,
20
+ :cents => '300',
21
+ :currency => 'EUR')
21
22
  assert_equal '3,00 €', shipping.price.format
22
23
 
23
24
  offer = Offer.new(:shipping => shipping,
@@ -34,30 +35,35 @@ class TestKosher < MiniTest::Unit::TestCase
34
35
  end
35
36
 
36
37
  def test_validation
37
- assert_raises(StandardError) { Unit.new.kosher? }
38
- unit = Unit.new(:condition => 1,
39
- :cents => 100,
40
- :currency => 'USD')
41
- assert unit.kosher?
38
+ assert_raises(NotValid) { Detail.new.kosher? }
39
+ detail = Detail.new(:condition => 1)
40
+ assert detail.kosher?
42
41
 
43
- assert_raises(StandardError) { Shipping.new.kosher? }
42
+ assert_raises(NotValid) { Shipping.new.kosher? }
44
43
  shipping = Shipping.new(:available => true,
45
44
  :cents => 100,
46
45
  :currency => 'USD')
47
46
  assert shipping.kosher?
48
47
 
49
- assert_raises(StandardError) { Seller.new.kosher? }
48
+ assert_raises(NotValid) { Seller.new.kosher? }
50
49
  seller = Seller.new(:name => 'John Doe')
51
50
  assert seller.kosher?
52
51
 
53
- assert_raises(NoMethodError) { Offer.new.kosher? }
54
- offer = Offer.new(:unit => unit,
52
+ assert_raises(NotValid) { Offer.new.kosher? }
53
+ offer = Offer.new(:detail => detail,
55
54
  :shipping => shipping,
56
55
  :seller => seller,
56
+ :unit => Unit.new,
57
57
  :venue_id => 1)
58
58
  assert offer.kosher?
59
59
  end
60
60
 
61
+ def test_venues
62
+ assert_kind_of Venue, Venue.first
63
+ assert 'Amazon.com', Venue.find(1).name
64
+ assert_nil Venue.find(0)
65
+ end
66
+
61
67
  def test_amazon_venues
62
68
  assert_equal 8, Venue.amazon.size
63
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kosher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.13.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-26 00:00:00.000000000Z
12
+ date: 2011-08-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70333604988360 !ruby/object:Gem::Requirement
16
+ requirement: &70322729328200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70333604988360
24
+ version_requirements: *70322729328200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: certainty
27
- requirement: &70333604987700 !ruby/object:Gem::Requirement
27
+ requirement: &70322729327560 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.2'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70333604987700
35
+ version_requirements: *70322729327560
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: money
38
- requirement: &70333604987180 !ruby/object:Gem::Requirement
38
+ requirement: &70322729326860 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,18 +43,18 @@ dependencies:
43
43
  version: '3.7'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70333604987180
46
+ version_requirements: *70322729326860
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: structure
49
- requirement: &70333604986660 !ruby/object:Gem::Requirement
49
+ requirement: &70322729326360 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
- version: '0.18'
54
+ version: '0.19'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70333604986660
57
+ version_requirements: *70322729326360
58
58
  description: Online selling, abstracted
59
59
  email: code@papercavalier.com
60
60
  executables: []
@@ -70,7 +70,8 @@ files:
70
70
  - data/venues.yml
71
71
  - kosher.gemspec
72
72
  - lib/kosher.rb
73
- - lib/kosher/base.rb
73
+ - lib/kosher/detail.rb
74
+ - lib/kosher/filter.rb
74
75
  - lib/kosher/offer.rb
75
76
  - lib/kosher/price.rb
76
77
  - lib/kosher/seller.rb
@@ -93,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
94
  version: '0'
94
95
  segments:
95
96
  - 0
96
- hash: -3190499119552710730
97
+ hash: -2784991868490813803
97
98
  required_rubygems_version: !ruby/object:Gem::Requirement
98
99
  none: false
99
100
  requirements:
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  version: '0'
103
104
  segments:
104
105
  - 0
105
- hash: -3190499119552710730
106
+ hash: -2784991868490813803
106
107
  requirements: []
107
108
  rubyforge_project:
108
109
  rubygems_version: 1.8.6
data/lib/kosher/base.rb DELETED
@@ -1,18 +0,0 @@
1
- module Kosher
2
- class Base < Structure
3
- include ActiveModel::Validations
4
-
5
- def self.inherited(child)
6
- child.key :foo
7
- child.key :kosher, Boolean, :default => true
8
- end
9
-
10
- def kosher?
11
- unless valid?
12
- raise StandardError.new(errors.full_messages.join(', '))
13
- end
14
-
15
- kosher
16
- end
17
- end
18
- end