store_field 0.0.1 → 1.0.0

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.
@@ -1,3 +1,3 @@
1
1
  module StoreField
2
- VERSION = '0.0.1'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/store_field.rb CHANGED
@@ -10,6 +10,7 @@ module StoreField
10
10
  raise ArgumentError.new(':in is invalid') if options[:in] and serialized_attributes[options[:in].to_s].nil?
11
11
  raise ArgumentError.new(':type is invalid') if options[:type] and ![ Hash, Set ].include?(options[:type])
12
12
 
13
+ klass = options[:type]
13
14
  store_attribute = options[:in] || serialized_attributes.keys.first
14
15
  raise ArgumentError.new('store method must be defined before store_field') if store_attribute.nil?
15
16
 
@@ -17,7 +18,7 @@ module StoreField
17
18
  define_method(key) do
18
19
  value = send("#{store_attribute}")[key]
19
20
  if value.nil?
20
- value = store_field_init(options[:type])
21
+ value = klass ? klass.new : {}
21
22
  send("#{store_attribute}")[key] = value
22
23
  end
23
24
  value
@@ -32,11 +33,4 @@ module StoreField
32
33
  end
33
34
  end
34
35
  end
35
-
36
- def store_field_init(klass)
37
- return {} unless klass
38
- klass.new
39
- end
40
36
  end
41
-
42
- ActiveRecord::Base.send(:include, StoreField)
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,8 @@ require 'bundler/setup'
3
3
 
4
4
  require 'store_field'
5
5
 
6
- RSpec.configure do |config|
7
- ActiveRecord::Base.send(:include, StoreField)
6
+ # Activate StoreField
7
+ ActiveRecord::Base.send(:include, StoreField)
8
8
 
9
- # Establish in-memory database connection
10
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
11
- end
9
+ # Establish in-memory database connection
10
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
@@ -6,11 +6,8 @@ end
6
6
 
7
7
  class User < ActiveRecord::Base
8
8
  store :storage
9
- store_field :preference
10
- store_field :count_caches
11
- store_field :notified, type: Set
12
- store_field :displayed, type: Set
13
- store_field :funnel, type: Set
9
+ store_field :tutorials
10
+ store_field :delivered, type: Set
14
11
  end
15
12
 
16
13
  describe StoreField do
@@ -19,38 +16,38 @@ describe StoreField do
19
16
  end
20
17
 
21
18
  it 'raises when store is not defined beforehand' do
22
- expect { Class.new(ActiveRecord::Base) { store :storage; store_field :notified } }.to_not raise_error(ArgumentError)
23
- expect { Class.new(ActiveRecord::Base) { store_field :notified } }.to raise_error(ArgumentError)
19
+ expect { Class.new(ActiveRecord::Base) { store :storage; store_field :delivered } }.to_not raise_error(ArgumentError)
20
+ expect { Class.new(ActiveRecord::Base) { store_field :delivered } }.to raise_error(ArgumentError)
24
21
  end
25
22
 
26
23
  it 'raises when invalid option is given' do
27
- expect { Class.new(ActiveRecord::Base) { store :storage; store_field :notified, type: File } }.to raise_error(ArgumentError)
28
- expect { Class.new(ActiveRecord::Base) { store :storage; store_field :notified, in: :bogus } }.to raise_error(ArgumentError)
24
+ expect { Class.new(ActiveRecord::Base) { store :storage; store_field :delivered, type: File } }.to raise_error(ArgumentError)
25
+ expect { Class.new(ActiveRecord::Base) { store :storage; store_field :delivered, in: :bogus } }.to raise_error(ArgumentError)
29
26
  end
30
27
 
31
28
  it 'initializes with the specified type' do
32
- @user.preference.should == {}
33
- @user.notified.should == Set.new
29
+ @user.tutorials.should == {}
30
+ @user.delivered.should == Set.new
34
31
  end
35
32
 
36
33
  it 'sets and unsets keywords' do
37
- @user.set_notified(:welcome)
38
- @user.set_notified(:first_deposit)
34
+ @user.set_delivered(:welcome)
35
+ @user.set_delivered(:first_deposit)
39
36
 
40
37
  # Consume balance, notify once and only once
41
- @user.set_notified(:balance_low)
42
- @user.set_notified(:balance_negative)
38
+ @user.set_delivered(:balance_low)
39
+ @user.set_delivered(:balance_negative)
43
40
 
44
41
  # Another deposit, restore balance
45
- @user.unset_notified(:balance_low)
46
- @user.unset_notified(:balance_negative)
42
+ @user.unset_delivered(:balance_low)
43
+ @user.unset_delivered(:balance_negative)
47
44
 
48
- @user.notified.should == Set.new([:welcome, :first_deposit])
45
+ @user.delivered.should == Set.new([:welcome, :first_deposit])
49
46
  end
50
47
 
51
48
  it 'saves in-line' do
52
- @user.set_notified(:welcome).save.should == true
49
+ @user.set_delivered(:welcome).save.should == true
53
50
  @user.reload
54
- @user.set_notified?(:welcome).should == true
51
+ @user.set_delivered?(:welcome).should == true
55
52
  end
56
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: store_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-18 00:00:00.000000000 Z
12
+ date: 2012-09-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -98,10 +98,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  requirements: []
100
100
  rubyforge_project:
101
- rubygems_version: 1.8.24
101
+ rubygems_version: 1.8.19
102
102
  signing_key:
103
103
  specification_version: 3
104
104
  summary: Nested fields for ActiveRecord::Store
105
105
  test_files:
106
106
  - spec/spec_helper.rb
107
107
  - spec/store_field_spec.rb
108
+ has_rdoc: