hstore_accessor 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 762278330d698c66a6d2926e1595c539da4fbaf7
4
- data.tar.gz: e4f38f9cb7e9b43384f2880a5b46860b63c3e0d9
3
+ metadata.gz: 460ba6cc8bae8307b3ddfdfd57af74ba02294934
4
+ data.tar.gz: cfd99b46a2ec13e2307c849720f5044315bf36f9
5
5
  SHA512:
6
- metadata.gz: 29530074370a555d44d91a2b60a5b7621935f4f062585e9c329a6ba508e762564f7548d2d6220a247b5c83779a6684545cc39668b48a0d44d905c5575ccce786
7
- data.tar.gz: 7c9227822c95e61b23b900aebd2f93f76ba3302e095e202fd0e50376a2f229278b9bc603d5b0136211a5058b0e9a9ea9a82ed09d6d4384b5bf45713ad04404d2
6
+ metadata.gz: 89245110259014870177d5b8c221d035909c1c8a635dd8a16a57384047ed00bfc80628aa265d2af317a58718d975e0134323e2208f08985c9af9b08b999e3635
7
+ data.tar.gz: a55c4ca8c1aea4ba9b85dae96f79fd1c7a2144e0737a645dac690597115ad2d124139d189cc63f78a18635c21a0903e5de5dde34eeda62af60e5747f6257bdf7
data/README.md CHANGED
@@ -131,6 +131,13 @@ Product.is_popular # => when populer is set to true
131
131
  Product.not_popular # => when populer is set to false
132
132
  ```
133
133
 
134
+ Predicate methods are also available on instances:
135
+
136
+ ```ruby
137
+ product = Product.new(popular: true)
138
+ product.popular? #=> true
139
+ ```
140
+
134
141
  ### Single-table Inheritance
135
142
 
136
143
  One of the big issues with `ActiveRecord` single-table inheritance (STI)
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "rspec"
28
28
  spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "database_cleaner"
29
30
  end
@@ -18,7 +18,7 @@ module HstoreAccessor
18
18
  :array => -> value { (value && value.join(SEPARATOR)) || nil },
19
19
  :hash => -> value { (value && value.to_json) || nil },
20
20
  :time => -> value { value.to_i },
21
- :boolean => -> value { (value == true).to_s },
21
+ :boolean => -> value { (value.to_s == "true").to_s },
22
22
  :date => -> value { (value && value.to_s) || nil }
23
23
  }
24
24
 
@@ -57,6 +57,8 @@ module HstoreAccessor
57
57
  store_key = type[:store_key]
58
58
  end
59
59
 
60
+ data_type = data_type.to_sym
61
+
60
62
  raise InvalidDataTypeError unless VALID_TYPES.include?(data_type)
61
63
 
62
64
  define_method("#{key}=") do |value|
@@ -69,6 +71,12 @@ module HstoreAccessor
69
71
  deserialize(data_type, value)
70
72
  end
71
73
 
74
+ if type == :boolean
75
+ define_method("#{key}?") do
76
+ return send("#{key}")
77
+ end
78
+ end
79
+
72
80
  query_field = "#{hstore_attribute} -> '#{store_key}'"
73
81
 
74
82
  case data_type
@@ -1,3 +1,3 @@
1
1
  module HstoreAccessor
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -54,6 +54,7 @@ describe HstoreAccessor do
54
54
  end
55
55
 
56
56
  context "nil values" do
57
+
57
58
  let!(:timestamp) { Time.now }
58
59
  let!(:datestamp) { Date.today }
59
60
  let!(:product) { Product.new }
@@ -73,6 +74,27 @@ describe HstoreAccessor do
73
74
  end
74
75
 
75
76
  end
77
+
78
+ describe "predicate methods" do
79
+
80
+ let!(:product) { Product.new }
81
+
82
+ it "return the state for true boolean fields" do
83
+ product.popular = true
84
+ product.save
85
+ product.reload
86
+ expect(product.popular?).to be_true
87
+ end
88
+
89
+ it "return the state for false boolean fields" do
90
+ product.popular = false
91
+ product.save
92
+ product.reload
93
+ expect(product.popular?).to be_false
94
+ end
95
+
96
+ end
97
+
76
98
  describe "scopes" do
77
99
 
78
100
  let!(:timestamp) { Time.now }
@@ -270,6 +292,24 @@ describe HstoreAccessor do
270
292
  expect(product.released_at).to eq datestamp
271
293
  end
272
294
 
295
+ context "correctly stores boolean values" do
296
+
297
+ it "when string 'true' is passed" do
298
+ product.popular = 'true'
299
+ product.save
300
+ product.reload
301
+ expect(product.popular).to be_true
302
+ end
303
+
304
+ it "when a real boolean is passed" do
305
+ product.popular = true
306
+ product.save
307
+ product.reload
308
+ expect(product.popular).to be_true
309
+ end
310
+
311
+ end
312
+
273
313
  it "setters call the _will_change! method of the store attribute" do
274
314
  product.should_receive(:options_will_change!)
275
315
  product.color = "green"
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "hstore_accessor"
2
+ require "database_cleaner"
2
3
 
3
4
  ActiveRecord::Base.establish_connection(
4
5
  adapter: "postgresql",
@@ -13,10 +14,12 @@ ActiveRecord::Base.connection.create_table(:products) do |t|
13
14
  t.hstore :options
14
15
  end
15
16
 
17
+ DatabaseCleaner.strategy = :truncation
18
+
16
19
  RSpec.configure do |config|
17
20
  config.mock_with :rspec
18
21
 
19
- config.after do
20
- Product.delete_all
22
+ config.before do
23
+ DatabaseCleaner.clean
21
24
  end
22
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hstore_accessor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Hirn
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-09-30 00:00:00.000000000 Z
13
+ date: 2013-11-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: pg
@@ -110,6 +110,20 @@ dependencies:
110
110
  - - '>='
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: database_cleaner
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
113
127
  description: Adds typed hstore backed fields to an ActiveRecord model.
114
128
  email:
115
129
  - joe@devmynd.com