activerecord-typedstore 0.3.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e093bbab4aa7f56fe8b248cbc6b3ffee4c896523
4
- data.tar.gz: 101fbbb8b13b3687010ce3543e371be6b4561ff1
3
+ metadata.gz: 8790af5ac7b68a121e6d5cd21305421b49b5799c
4
+ data.tar.gz: 0afe8bfce070b6f1d87f5c3f39334870ce9decc2
5
5
  SHA512:
6
- metadata.gz: 81ccdf476b884892c67460b9945784d593e50380a7e452452d0a3b54a60f82c3e39cf8a70f64bd154f01337ecac60675866f525aaf6d39a1536ed551c971d10b
7
- data.tar.gz: da7fc4fc81c1cd22156b78dad835ee14ec788b9cdc69f7d2ccc839dc0e9f77fb5a024cf13dc159db43e297d6251f6e579759a061f2287fd85443bf23fc9b6a6b
6
+ metadata.gz: 10d34e86b936c3c9d1f85dd35c6d2414f18c0c4346784f39ce9e4e741c02b23a8657fed819b8ed39a63c9ffb5d36b8b78b559dd2a877e1be64352a5e3b9b9cc8
7
+ data.tar.gz: d9954012cc7fbec9501a19b7b10e8091c9d3447fc7413cbd09023d822e75f7a00ee505c3712720bac1b8de8d47d6a9ad8b4f5c7f1ca6265a09b454f0328c1621
@@ -12,8 +12,6 @@ module ActiveRecord::TypedStore
12
12
  included do
13
13
  class_attribute :typed_stores, instance_accessor: false
14
14
  class_attribute :typed_store_attributes, instance_accessor: false
15
- self.typed_stores = {}
16
- self.typed_store_attributes = {}
17
15
  end
18
16
 
19
17
  module ClassMethods
@@ -27,20 +25,36 @@ module ActiveRecord::TypedStore
27
25
  store(store_attribute, options.merge(accessors: dsl.column_names))
28
26
  end
29
27
 
30
- typed_stores[store_attribute] ||= {}
31
- typed_stores[store_attribute].merge!(dsl.columns.index_by(&:name))
32
- typed_store_attributes.merge!(dsl.columns.index_by { |c| c.name.to_s })
28
+ register_typed_store_columns(store_attribute, dsl.columns)
29
+ super(store_attribute, dsl) if defined?(super)
33
30
 
34
- dsl.column_names.each { |c| define_virtual_attribute_method(c.to_s) }
35
31
  dsl.column_names.each { |c| define_store_attribute_queries(store_attribute, c) }
36
32
 
37
- super(store_attribute, dsl) if defined?(super)
38
-
39
33
  dsl
40
34
  end
41
35
 
36
+ def define_attribute_methods
37
+ super
38
+ define_typed_store_attribute_methods
39
+ end
40
+
42
41
  private
43
42
 
43
+ def register_typed_store_columns(store_attribute, columns)
44
+ self.typed_stores ||= {}
45
+ self.typed_store_attributes ||= {}
46
+ typed_stores[store_attribute] ||= {}
47
+ typed_stores[store_attribute].merge!(columns.index_by(&:name))
48
+ typed_store_attributes.merge!(columns.index_by { |c| c.name.to_s })
49
+ end
50
+
51
+ def define_typed_store_attribute_methods
52
+ return unless typed_store_attributes
53
+ typed_store_attributes.keys.each do |attribute|
54
+ define_virtual_attribute_method(attribute)
55
+ end
56
+ end
57
+
44
58
  def hstore?(store_attribute)
45
59
  columns_hash[store_attribute.to_s].try(:type) == :hstore
46
60
  end
@@ -66,7 +80,7 @@ module ActiveRecord::TypedStore
66
80
  protected
67
81
 
68
82
  def write_store_attribute(store_attribute, key, value)
69
- column = store_column_definition(store_attribute, key)
83
+ column = store_column(store_attribute, key)
70
84
  if column.try(:type) == :datetime && self.class.time_zone_aware_attributes && value.respond_to?(:in_time_zone)
71
85
  value = value.in_time_zone
72
86
  end
@@ -80,13 +94,17 @@ module ActiveRecord::TypedStore
80
94
  private
81
95
 
82
96
  def cast_store_attribute(store_attribute, key, value)
83
- column = store_column_definition(store_attribute, key)
97
+ column = store_column(store_attribute, key)
84
98
  column ? column.cast(value) : value
85
99
  end
86
100
 
87
- def store_column_definition(store_attribute, key)
88
- store_definition = self.class.typed_stores[store_attribute]
89
- store_definition && store_definition[key]
101
+ def store_column(store_attribute, key)
102
+ store = store_columns(store_attribute)
103
+ store && store[key]
104
+ end
105
+
106
+ def store_columns(store_attribute)
107
+ self.class.typed_stores.try(:[], store_attribute)
90
108
  end
91
109
 
92
110
  def if_store_uninitialized(store_attribute)
@@ -98,6 +116,7 @@ module ActiveRecord::TypedStore
98
116
  end
99
117
 
100
118
  def reload_stores!
119
+ return unless self.class.typed_stores
101
120
  self.class.typed_stores.keys.each do |store_attribute|
102
121
  instance_variable_set("@_#{store_attribute}_initialized", false)
103
122
  end
@@ -107,7 +126,7 @@ module ActiveRecord::TypedStore
107
126
  store = defined?(super) ? super : send(store_attribute)
108
127
  store.tap do |store|
109
128
  if_store_uninitialized(store_attribute) do
110
- if columns = self.class.typed_stores[store_attribute]
129
+ if columns = store_columns(store_attribute)
111
130
  initialize_store(store, columns.values)
112
131
  end
113
132
  end
@@ -133,7 +152,7 @@ module ActiveRecord::TypedStore
133
152
  when true then true
134
153
  when false, nil then false
135
154
  else
136
- column = store_column_definition(store_attribute, key)
155
+ column = store_column(store_attribute, key)
137
156
 
138
157
  if column.number?
139
158
  !value.zero?
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module TypedStore
3
- VERSION = '0.3.1'
3
+ VERSION = '0.3.2'
4
4
  end
5
5
  end
@@ -4,6 +4,16 @@ shared_examples 'any model' do
4
4
 
5
5
  let(:model) { described_class.new }
6
6
 
7
+ describe 'reset_column_information' do
8
+
9
+ it 'do not definitely undefine attributes' do
10
+ expect {
11
+ described_class.reset_column_information
12
+ }.to_not change { model.age_changed? }
13
+ end
14
+
15
+ end
16
+
7
17
  describe 'regular AR::Store' do
8
18
 
9
19
  it 'save attributes as usual' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-typedstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-13 00:00:00.000000000 Z
11
+ date: 2013-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord