plyushkin 0.0.1 → 0.0.2

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.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .bundle
2
2
  .DS_Store
3
3
  *.swp
4
+ plyushkin*.gem
@@ -36,7 +36,7 @@ class Plyushkin::BaseValue
36
36
  end
37
37
  end
38
38
 
39
- persisted_attr :date
39
+ persisted_attr :date, :formatter => :to_date
40
40
  validates_each :date do |record, attr_name, value|
41
41
  record.errors.add(attr_name, "cannot be in the future") unless value < DateTime.now
42
42
  end
@@ -12,7 +12,7 @@ class Plyushkin::Persistence
12
12
  def save(id)
13
13
  hash = {}
14
14
  (@properties || {}).each do |name, property|
15
- hash[name] = [property.value_hashes]
15
+ hash[name] = property.value_hashes
16
16
  end
17
17
  model.service.put(id, hash.to_json)
18
18
  end
@@ -20,14 +20,19 @@ class Plyushkin::Property
20
20
  end
21
21
 
22
22
  def create(attr={})
23
+ #write a spec for this option
24
+ ignore_invalid = attr.delete(:ignore_invalid)
23
25
  value = value_type.new(attr)
24
26
  if @ignore_unchanged_values
25
27
  last = @values.last
26
28
  return last if last && last.equal_value?(value)
27
29
  end
28
30
 
31
+ return if !value.valid? && ignore_invalid
32
+
29
33
  @values.insert(insert_position(value.date), value)
30
- trigger_callback(:after_create)
34
+ #write a spec for this option
35
+ trigger_callback(:after_create) unless attr[:without_callbacks]
31
36
  value
32
37
  end
33
38
 
@@ -58,9 +63,11 @@ class Plyushkin::Property
58
63
 
59
64
  def self.build(name, type, values, opts={})
60
65
  opts[:type] = type
61
- Plyushkin::Property.new(name, opts).tap do |p|
62
- values.each { |value| p.create(value) }
66
+ prop = Plyushkin::Property.new(name, opts).tap do |p|
67
+ values.each { |value| p.create(value.merge(:without_callbacks => true)) }
63
68
  end
69
+
70
+ prop
64
71
  end
65
72
 
66
73
  def value_hashes
@@ -71,6 +78,6 @@ class Plyushkin::Property
71
78
  end
72
79
  end
73
80
 
74
- value_hashes
81
+ value_hashes == {} ? [] : [value_hashes]
75
82
  end
76
83
  end
@@ -1,3 +1,3 @@
1
1
  module Plyushkin
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -51,6 +51,49 @@ describe Plyushkin::Property do
51
51
  property.all.length.should == 1
52
52
  end
53
53
  end
54
+
55
+ describe 'ignore_invalid option' do
56
+ it 'should not create a value if value is invalid and option is true' do
57
+ property = Plyushkin::Property.new(:property_name, :type => Plyushkin::Test::PresenceTestValue)
58
+ property.create(:ignore_invalid => true )
59
+ property.all.length.should == 0
60
+ property.should be_valid
61
+ end
62
+
63
+ it 'should create a value if the value is invalid and the option is not provided' do
64
+ property = Plyushkin::Property.new(:property_name,
65
+ :type => Plyushkin::Test::PresenceTestValue)
66
+ property.create
67
+ property.all.length.should == 1
68
+ property.should_not be_valid
69
+ end
70
+
71
+ it 'should create a value if the value is valid and the option is true' do
72
+ property = Plyushkin::Property.new(:property_name,
73
+ :type => Plyushkin::Test::PresenceTestValue)
74
+ property.create(:value => 5, :ignore_invalid => true)
75
+ property.all.length.should == 1
76
+ property.should be_valid
77
+ end
78
+ end
79
+
80
+ describe 'without_callbacks' do
81
+ it 'should not run callbacks if option is true' do
82
+ called = 0
83
+ callbacks = { :after_create => lambda{ called += 1 } }
84
+ property = Plyushkin::Property.new(:property_name, :callbacks => callbacks)
85
+ property.create(:value => 5, :without_callbacks => true)
86
+ called.should == 0
87
+ end
88
+
89
+ it 'should run callbacks if option is false' do
90
+ called = 0
91
+ callbacks = { :after_create => lambda{ called += 1 } }
92
+ property = Plyushkin::Property.new(:property_name, :callbacks => callbacks)
93
+ property.create(:value => 5, :without_callbacks => false)
94
+ called.should == 1
95
+ end
96
+ end
54
97
  end
55
98
 
56
99
  describe '#all' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plyushkin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-01-22 00:00:00.000000000 Z
13
+ date: 2014-01-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord