active_record_stash 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ *.sqlite3
@@ -19,6 +19,10 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "sqlite3"
24
+ s.add_development_dependency "pry"
25
+ s.add_runtime_dependency "activerecord"
26
+ s.add_runtime_dependency "activemodel"
27
+ s.add_runtime_dependency "activesupport", "> 3.1.0"
24
28
  end
@@ -6,23 +6,21 @@ module ActiveRecordStash
6
6
 
7
7
  NO_TARGET_ERROR = "stashing needs a target serialized column. Supply an options hash with a :in key as the last argument (e.g. stash :apple, :in => :greeter)."
8
8
 
9
- included do
9
+ included do |klass|
10
10
  class_attribute :stashed_attributes
11
- self.stashed_attributes = Hash.new([])
12
-
13
- after_initialize :_load_stashed_attributes
14
- before_save :_stash_attributes
11
+ klass.stashed_attributes = {}
12
+ klass.after_initialize :_load_stashed_attributes
13
+ klass.before_save :_stash_attributes
15
14
  end
16
15
 
17
16
  private
18
17
  def _load_stashed_attributes
19
18
  stashed_attributes.each_pair do |store_name,methods|
20
- store = send(store_name)
21
19
 
22
- next unless store
20
+ return unless store = send(store_name)
23
21
 
24
22
  methods.each do |method|
25
- send :"#{method}=", store[method] unless send(method)
23
+ send :"#{method}=", store[method] if respond_to? method
26
24
  end
27
25
 
28
26
  end
@@ -31,12 +29,12 @@ module ActiveRecordStash
31
29
  def _stash_attributes
32
30
  stashed_attributes.each_pair do |store_name,methods|
33
31
 
34
- eval "self.#{store_name} ||= {}"
35
-
36
- methods.each do |method|
37
- send(store_name)[method] = send method
32
+ data = methods.inject({}) do |data,method|
33
+ data[method] = send method
34
+ data
38
35
  end
39
36
 
37
+ self[store_name] = data
40
38
  end
41
39
  end
42
40
 
@@ -51,7 +49,8 @@ module ActiveRecordStash
51
49
  raise ArgumentError, NO_TARGET_ERROR
52
50
  end
53
51
 
54
- stashed_attributes[serialized_column] += methods.map(&:to_sym)
52
+ self.stashed_attributes = {}
53
+ self.stashed_attributes[serialized_column] = methods.map(&:to_sym)
55
54
 
56
55
  serialize serialized_column
57
56
  attr_accessor *methods
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordStash
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,14 +1,24 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'active_support/concern'
2
+ require 'active_record'
3
+ require 'active_model'
4
+ require './lib/active_record_stash'
2
5
 
3
- conn = {
6
+ conn = {
4
7
  :adapter => 'sqlite3',
5
8
  :database => 'activerecord_unittest',
6
9
  :database => 'spec/testing.sqlite3',
7
- :encoding => 'utf8'
10
+ :encoding => 'utf8'
8
11
  }
9
12
 
10
13
  ActiveRecord::Base.establish_connection(conn)
11
14
 
15
+ class User < ActiveRecord::Base
16
+ connection.create_table :users, :force => true do |t|
17
+ t.string :name
18
+ t.timestamps
19
+ end
20
+ end
21
+
12
22
  class Email < ActiveRecord::Base
13
23
  connection.create_table :emails, :force => true do |t|
14
24
  t.string :name
@@ -20,115 +30,96 @@ class Email < ActiveRecord::Base
20
30
  stash :phone, :address, :postal_code, :in => :data
21
31
  end
22
32
 
23
- describe ActiveRecord::Stash do
33
+ describe 'ActiveRecordStash' do
24
34
  describe "new object initialization" do
25
- before(:all) do
26
- @email = Email.new(
27
- :phone => "123456789",
28
- :address => "1234 Apple way",
35
+ subject do
36
+ Email.new(
37
+ :phone => "123456789",
38
+ :address => "1234 Apple way",
29
39
  :postal_code => "13244"
30
40
  )
31
41
  end
32
42
 
33
- it "is a valid object" do
34
- @email.should be_valid
35
- end
36
-
37
- it "has all the correct forward facing attributes" do
38
- @email.phone.should match("123456789")
39
- @email.address.should match("1234 Apple way")
40
- @email.postal_code.should match("13244")
41
- end
42
-
43
- it "has an empty data column" do
44
- @email.data.should be_nil
45
- end
46
-
47
- describe "save" do
48
- it "is successful" do
49
- @email.save.should be_true
50
- end
51
-
52
- it "still has the correct forward facing attributes" do
53
- @email.phone.should match("123456789")
54
- @email.address.should match("1234 Apple way")
55
- @email.postal_code.should match("13244")
56
- end
57
-
58
- it "does not have an empty data column" do
59
- @email.data.should_not be_empty
60
- end
61
-
62
- it "has the correct serialized data in the serialized column" do
43
+ it { should be_valid }
44
+ its(:phone){ should match("123456789") }
45
+ its(:address){ should match("1234 Apple way") }
46
+ its(:postal_code){ should match("13244") }
47
+ its(:data){ should be_nil }
63
48
 
64
- @email.data.should eql({
65
- :phone => "123456789",
66
- :address => "1234 Apple way",
67
- :postal_code => "13244"
68
- })
69
- end
70
- end
71
49
  end
72
50
 
73
51
  describe "object creation" do
74
- before :all do
52
+ subject do
75
53
  @email = Email.create(
76
- :phone => "123456789",
77
- :address => "1234 Apple way",
54
+ :phone => "123456789",
55
+ :address => "1234 Apple way",
78
56
  :postal_code => "13244"
79
57
  )
80
- end
81
-
82
- it "is a valid object" do
83
- @email.should be_valid
84
- end
85
58
 
86
- it "still has the correct forward facing attributes" do
87
- @email.phone.should match("123456789")
88
- @email.address.should match("1234 Apple way")
89
- @email.postal_code.should match("13244")
59
+ @email.reload
90
60
  end
91
- it "does not have an empty data column" do
92
- @email.data.should_not be_empty
93
- end
94
-
95
- it "has the correct serialized data in the serialized column" do
96
-
97
- @email.data.should eql({
98
- :phone => "123456789",
99
- :address => "1234 Apple way",
61
+ it { should be_valid }
62
+
63
+ its(:phone) { should match("123456789") }
64
+ its(:address) { should match("1234 Apple way") }
65
+ its(:postal_code) { should match("13244") }
66
+ its(:data) do
67
+ should eql({
68
+ :phone => "123456789",
69
+ :address => "1234 Apple way",
100
70
  :postal_code => "13244"
101
71
  })
102
72
  end
103
73
 
104
74
  describe "#update_attributes" do
105
75
  before :all do
106
- @email.update_attributes({
76
+ subject.update_attributes({
107
77
  :phone => "2222222",
108
78
  :address => "4321 yaw elppa"
109
79
  })
110
80
  end
111
- it "has no errors" do
112
- @email.errors.any?.should be_false
113
- end
114
-
115
- it "does not have an empty data column" do
116
- @email.data.should_not be_empty
117
- end
118
-
119
- it "has all the expected contents of the data column" do
120
- @email.data.should eql({
81
+ its(:errors) { should_not be_any }
82
+ its(:data) { should_not be_empty }
83
+ its(:data) do
84
+ should eql({
121
85
  :phone => "2222222",
122
86
  :address => "4321 yaw elppa",
123
87
  :postal_code => "13244"
124
88
  })
125
89
  end
90
+ its(:phone) { should match("2222222") }
91
+ its(:address) { should match("4321 yaw elppa") }
92
+ its(:postal_code) { should match("13244") }
93
+ end
94
+ end
95
+ describe 'artifact cleanup between classes' do
96
+ it 'non-stashed object -> stashed object -> non-stashed object' do
97
+ email = Email.create
98
+ user = User.create
99
+ email.reload
100
+ user.reload
101
+ end
102
+ end
126
103
 
127
- it "still has the correct forward facing attributes" do
128
- @email.phone.should match("2222222")
129
- @email.address.should match("4321 yaw elppa")
130
- @email.postal_code.should match("13244")
104
+ describe 'inheritence, where child should inherit stash' do
105
+ subject do
106
+ class SpecialEmail < Email
107
+ end
108
+ SpecialEmail
131
109
  end
110
+
111
+ its(:stashed_attributes) { should ==({data: [:phone, :address, :postal_code]}) }
112
+ end
113
+
114
+ describe 'inheritence, where child should NOT inherit stash' do
115
+ subject do
116
+ class SpecialEmail < Email
117
+ stash in: :data
118
+ end
119
+ SpecialEmail
132
120
  end
121
+
122
+ its(:stashed_attributes) { should_not==({data: [:phone, :address, :postal_code]}) }
123
+ its(:stashed_attributes) { should ==({data: []}) }
133
124
  end
134
125
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_stash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,73 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-01-23 00:00:00.000000000Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70107239613000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70107239613000
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70107239612580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70107239612580
36
+ - !ruby/object:Gem::Dependency
37
+ name: pry
38
+ requirement: &70107239612160 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70107239612160
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: &70107239611740 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70107239611740
58
+ - !ruby/object:Gem::Dependency
59
+ name: activemodel
60
+ requirement: &70107239611320 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70107239611320
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: &70107239610820 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>'
75
+ - !ruby/object:Gem::Version
76
+ version: 3.1.0
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70107239610820
14
80
  description: ActiveRecordStash provides a helpful syntax, which faciliates active_model
15
81
  compliant attributes, which are stored within a single serialized attribute. Poor
16
82
  mans NoSQL with AR
@@ -34,7 +100,6 @@ files:
34
100
  - lib/active_record_stash/version.rb
35
101
  - spec/active_record_stash_spec.rb
36
102
  - spec/spec.opts
37
- - spec/spec_helper.rb
38
103
  homepage: ''
39
104
  licenses: []
40
105
  post_install_message:
@@ -62,5 +127,4 @@ summary: Validations for serialized ActiveRecrd fields
62
127
  test_files:
63
128
  - spec/active_record_stash_spec.rb
64
129
  - spec/spec.opts
65
- - spec/spec_helper.rb
66
130
  has_rdoc:
@@ -1,18 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
-
4
- require 'rubygems'
5
- require 'bundler'
6
-
7
- Bundler.setup
8
-
9
- require 'active_support/concern'
10
- require 'active_record'
11
- require 'active_model'
12
- require 'active_record_stash'
13
- require 'spec'
14
- require 'spec/autorun'
15
-
16
- Spec::Runner.configure do |config|
17
-
18
- end