plyushkin 0.0.4 → 0.0.5
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/Gemfile.lock +1 -1
- data/lib/plyushkin/base_value.rb +10 -0
- data/lib/plyushkin/persistence.rb +6 -2
- data/lib/plyushkin/property.rb +21 -3
- data/lib/plyushkin/version.rb +1 -1
- data/spec/lib/base_value_spec.rb +16 -0
- data/spec/lib/persistence_spec.rb +21 -0
- data/spec/lib/property_spec.rb +52 -5
- metadata +17 -9
- checksums.yaml +0 -7
data/Gemfile.lock
CHANGED
data/lib/plyushkin/base_value.rb
CHANGED
@@ -2,6 +2,8 @@ class Plyushkin::BaseValue
|
|
2
2
|
include ActiveModel::Validations
|
3
3
|
|
4
4
|
def initialize(attr={})
|
5
|
+
@new_record = attr[:new_record].nil? ? true : attr[:new_record]
|
6
|
+
|
5
7
|
attr = attr.dup
|
6
8
|
@date = attr.delete(:date) || DateTime.current
|
7
9
|
attr.each do |k,v|
|
@@ -9,6 +11,14 @@ class Plyushkin::BaseValue
|
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
14
|
+
def new_record?
|
15
|
+
@new_record
|
16
|
+
end
|
17
|
+
|
18
|
+
def mark_persisted
|
19
|
+
@new_record = false
|
20
|
+
end
|
21
|
+
|
12
22
|
def formatters
|
13
23
|
@formatters.dup
|
14
24
|
end
|
@@ -11,17 +11,21 @@ class Plyushkin::Persistence
|
|
11
11
|
|
12
12
|
def save(id)
|
13
13
|
hash = {}
|
14
|
-
|
14
|
+
props = @properties || {}
|
15
|
+
|
16
|
+
props.each do |name, property|
|
15
17
|
hash[name] = property.value_hashes
|
16
18
|
end
|
17
19
|
model.service.put(model.name, id, hash)
|
18
20
|
model.cache.write(get_key(model.name, id), hash)
|
21
|
+
|
22
|
+
props.values.each(&:mark_persisted)
|
19
23
|
end
|
20
24
|
|
21
25
|
def load(id)
|
22
26
|
@properties = {}
|
23
27
|
cached(id).each do |name, values|
|
24
|
-
property = Plyushkin::Property.
|
28
|
+
property = Plyushkin::Property.load(name, model.registered_types[name.to_sym], values,
|
25
29
|
:callbacks => @callbacks[name.to_sym],
|
26
30
|
:ignore_unchanged_values => @model.ignore_unchanged_values[name.to_sym] )
|
27
31
|
@properties[name.to_sym] = property
|
data/lib/plyushkin/property.rb
CHANGED
@@ -32,7 +32,9 @@ class Plyushkin::Property
|
|
32
32
|
|
33
33
|
@values.insert(insert_position(value.date), value)
|
34
34
|
#write a spec for this option
|
35
|
-
trigger_callback(:after_create)
|
35
|
+
trigger_callback(:after_create) if value.new_record?
|
36
|
+
|
37
|
+
@dirty = true
|
36
38
|
value
|
37
39
|
end
|
38
40
|
|
@@ -61,10 +63,10 @@ class Plyushkin::Property
|
|
61
63
|
@value_type ||= Plyushkin::StringValue
|
62
64
|
end
|
63
65
|
|
64
|
-
def self.
|
66
|
+
def self.load(name, type, values, opts={})
|
65
67
|
opts[:type] = type
|
66
68
|
prop = Plyushkin::Property.new(name, opts).tap do |p|
|
67
|
-
values.each { |value| p
|
69
|
+
values.each { |value| load_value(p, value) }
|
68
70
|
end
|
69
71
|
|
70
72
|
prop
|
@@ -84,4 +86,20 @@ class Plyushkin::Property
|
|
84
86
|
def nil?
|
85
87
|
last.is_a?(Plyushkin::NilValue)
|
86
88
|
end
|
89
|
+
|
90
|
+
def dirty?
|
91
|
+
@dirty
|
92
|
+
end
|
93
|
+
|
94
|
+
def mark_persisted
|
95
|
+
@dirty = false
|
96
|
+
all.each(&:mark_persisted)
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def self.load_value(property, value)
|
102
|
+
property.create(value.merge(:new_record => false))
|
103
|
+
end
|
104
|
+
|
87
105
|
end
|
data/lib/plyushkin/version.rb
CHANGED
data/spec/lib/base_value_spec.rb
CHANGED
@@ -46,6 +46,22 @@ describe Plyushkin::BaseValue do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
describe '#new_record?' do
|
50
|
+
it 'should be true for a new instance' do
|
51
|
+
clazz = Class.new(Plyushkin::StringValue)
|
52
|
+
clazz.new.should be_new_record
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#mark_persisted' do
|
57
|
+
it 'should set new_record to false' do
|
58
|
+
clazz = Class.new(Plyushkin::StringValue)
|
59
|
+
value = clazz.new
|
60
|
+
value.mark_persisted
|
61
|
+
value.should_not be_new_record
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
49
65
|
describe '##persisted_attr' do
|
50
66
|
it 'should add attribute to persisted_attributes' do
|
51
67
|
clazz = Class.new(Plyushkin::BaseValue) do
|
@@ -42,6 +42,14 @@ describe Plyushkin::Persistence do
|
|
42
42
|
persistence.save(1)
|
43
43
|
service.get("widget", 1)["name"].last["value"].should == "Mike"
|
44
44
|
end
|
45
|
+
|
46
|
+
it 'should mark all properties as persisted' do
|
47
|
+
persistence.properties[:name].create(:value => "Mike")
|
48
|
+
persistence.properties[:weight].create(:value => "150")
|
49
|
+
persistence.save(1)
|
50
|
+
persistence.properties[:name].should_not be_dirty
|
51
|
+
persistence.properties[:weight].should_not be_dirty
|
52
|
+
end
|
45
53
|
end
|
46
54
|
|
47
55
|
describe '#load' do
|
@@ -78,6 +86,19 @@ describe Plyushkin::Persistence do
|
|
78
86
|
model.register(:missing_property, Plyushkin::StringValue)
|
79
87
|
persistence.properties[:missing_property].all.should == []
|
80
88
|
end
|
89
|
+
|
90
|
+
it 'should mark loaded values new_record to false' do
|
91
|
+
persistence.properties[:name ].last.should_not be_new_record
|
92
|
+
persistence.properties[:weight].last.should_not be_new_record
|
93
|
+
persistence.properties[:udt ].last.should_not be_new_record
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should mark saved values new_record to false' do
|
97
|
+
persistence.properties[:name].create(:values => 'Mark')
|
98
|
+
persistence.properties[:name].last.should be_new_record
|
99
|
+
persistence.save(1)
|
100
|
+
persistence.properties[:name].last.should_not be_new_record
|
101
|
+
end
|
81
102
|
end
|
82
103
|
|
83
104
|
describe "#caching" do
|
data/spec/lib/property_spec.rb
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Plyushkin::Property do
|
4
|
+
describe '##load' do
|
5
|
+
it 'should mark all values as not new records' do
|
6
|
+
property = Plyushkin::Property.load(:name, Plyushkin::StringValue,
|
7
|
+
[ {:value => 'test'} ])
|
8
|
+
property.last.should_not be_new_record
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
4
12
|
describe '#create' do
|
5
13
|
it 'should add a StringValue' do
|
6
14
|
property = Plyushkin::Property.new(:property_name)
|
@@ -43,6 +51,37 @@ describe Plyushkin::Property do
|
|
43
51
|
end
|
44
52
|
end
|
45
53
|
|
54
|
+
describe '#dirty?' do
|
55
|
+
let(:property) { Plyushkin::Property.new(:property_name) }
|
56
|
+
|
57
|
+
it 'should be false when the property is first created' do
|
58
|
+
property.should_not be_dirty
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should be true when a value is created' do
|
62
|
+
property.create(:value => 5)
|
63
|
+
property.should be_dirty
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#mark_persisted' do
|
68
|
+
let(:property) { Plyushkin::Property.new(:property_name) }
|
69
|
+
|
70
|
+
it 'should reset dirty? to false' do
|
71
|
+
property.create(:value => 5)
|
72
|
+
property.should be_dirty
|
73
|
+
property.mark_persisted
|
74
|
+
property.should_not be_dirty
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should mark all values new_record to false' do
|
78
|
+
property.create(:value => 5)
|
79
|
+
property.last.should be_new_record
|
80
|
+
property.mark_persisted
|
81
|
+
property.last.should_not be_new_record
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
46
85
|
describe 'ignore_unchanged_value' do
|
47
86
|
it 'should not add a value when ignore_unchanged_value is true and value does not change' do
|
48
87
|
property = Plyushkin::Property.new(:property_name, :ignore_unchanged_values => true)
|
@@ -77,20 +116,28 @@ describe Plyushkin::Property do
|
|
77
116
|
end
|
78
117
|
end
|
79
118
|
|
80
|
-
describe '
|
81
|
-
it 'should not run callbacks if option is
|
119
|
+
describe 'new_record' do
|
120
|
+
it 'should not run callbacks if option is false' do
|
82
121
|
called = 0
|
83
122
|
callbacks = { :after_create => lambda{ called += 1 } }
|
84
123
|
property = Plyushkin::Property.new(:property_name, :callbacks => callbacks)
|
85
|
-
property.create(:value => 5, :
|
124
|
+
property.create(:value => 5, :new_record => false)
|
86
125
|
called.should == 0
|
87
126
|
end
|
88
127
|
|
89
|
-
it 'should run callbacks if option is
|
128
|
+
it 'should run callbacks if option is true' do
|
129
|
+
called = 0
|
130
|
+
callbacks = { :after_create => lambda{ called += 1 } }
|
131
|
+
property = Plyushkin::Property.new(:property_name, :callbacks => callbacks)
|
132
|
+
property.create(:value => 5, :new_record => true)
|
133
|
+
called.should == 1
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'should run callbacks if no option is provided' do
|
90
137
|
called = 0
|
91
138
|
callbacks = { :after_create => lambda{ called += 1 } }
|
92
139
|
property = Plyushkin::Property.new(:property_name, :callbacks => callbacks)
|
93
|
-
property.create(:value => 5
|
140
|
+
property.create(:value => 5)
|
94
141
|
called.should == 1
|
95
142
|
end
|
96
143
|
end
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plyushkin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Craig Israel
|
@@ -9,11 +10,12 @@ authors:
|
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2014-
|
13
|
+
date: 2014-03-19 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: activerecord
|
16
17
|
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
17
19
|
requirements:
|
18
20
|
- - ~>
|
19
21
|
- !ruby/object:Gem::Version
|
@@ -21,6 +23,7 @@ dependencies:
|
|
21
23
|
type: :runtime
|
22
24
|
prerelease: false
|
23
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
24
27
|
requirements:
|
25
28
|
- - ~>
|
26
29
|
- !ruby/object:Gem::Version
|
@@ -28,6 +31,7 @@ dependencies:
|
|
28
31
|
- !ruby/object:Gem::Dependency
|
29
32
|
name: bundler
|
30
33
|
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
31
35
|
requirements:
|
32
36
|
- - ~>
|
33
37
|
- !ruby/object:Gem::Version
|
@@ -35,6 +39,7 @@ dependencies:
|
|
35
39
|
type: :development
|
36
40
|
prerelease: false
|
37
41
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
38
43
|
requirements:
|
39
44
|
- - ~>
|
40
45
|
- !ruby/object:Gem::Version
|
@@ -42,15 +47,17 @@ dependencies:
|
|
42
47
|
- !ruby/object:Gem::Dependency
|
43
48
|
name: rake
|
44
49
|
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
45
51
|
requirements:
|
46
|
-
- - '>='
|
52
|
+
- - ! '>='
|
47
53
|
- !ruby/object:Gem::Version
|
48
54
|
version: '0'
|
49
55
|
type: :development
|
50
56
|
prerelease: false
|
51
57
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
52
59
|
requirements:
|
53
|
-
- - '>='
|
60
|
+
- - ! '>='
|
54
61
|
- !ruby/object:Gem::Version
|
55
62
|
version: '0'
|
56
63
|
description: Provides active record extension to capture historical property data
|
@@ -109,26 +116,27 @@ files:
|
|
109
116
|
homepage: http://github.com/OnlifeHealth/plyushkin
|
110
117
|
licenses:
|
111
118
|
- MIT
|
112
|
-
metadata: {}
|
113
119
|
post_install_message:
|
114
120
|
rdoc_options: []
|
115
121
|
require_paths:
|
116
122
|
- lib
|
117
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
118
125
|
requirements:
|
119
|
-
- - '>='
|
126
|
+
- - ! '>='
|
120
127
|
- !ruby/object:Gem::Version
|
121
128
|
version: '0'
|
122
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
123
131
|
requirements:
|
124
|
-
- - '>='
|
132
|
+
- - ! '>='
|
125
133
|
- !ruby/object:Gem::Version
|
126
134
|
version: '0'
|
127
135
|
requirements: []
|
128
136
|
rubyforge_project:
|
129
|
-
rubygems_version:
|
137
|
+
rubygems_version: 1.8.23
|
130
138
|
signing_key:
|
131
|
-
specification_version:
|
139
|
+
specification_version: 3
|
132
140
|
summary: Plyushkin - the attribute hoarder
|
133
141
|
test_files:
|
134
142
|
- spec/lib/base_value_spec.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: f3ffa197a5ec8d8b23b72ba77444540524c17062
|
4
|
-
data.tar.gz: cfd45839d46278a89f81904eee6cc4cedb490b97
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 20a5449a85d1753e8a08ec0bac1063827a1ddc3d1325edfd57d805072f9f90d4cf6bf581e2da6a51457daaf7c493543e750e9652b08e62b1f4dd1782b575ae8a
|
7
|
-
data.tar.gz: 0e26387d0792ec1ec44f78d8f5e1e738dd1f7c51cc1163305087dc76d52b0d6a67be1f84ad8674828452d2fcbcacb281ddebe85c113d29e0d3edcda897e9c28a
|