cacheable_delegator 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjBhNDAxOGI2NDljOWZiOTMwMjI2YmE1YTgyYjc0ZDdkNGMwMjE2MQ==
4
+ Yjc0MjE1Y2JmODg4NDYyYTkxOGZkMjQ0MTQxYmZkOGE3MGY4MDUwNA==
5
5
  data.tar.gz: !binary |-
6
- YmU2NGMwNmU2NzUwNDEzMzI5MGE4OWFlMjZhOTM4OTM5NTk5MGVjZQ==
6
+ NWI1NTlhMDBmNTY4NDAyNGM4M2VjZmEwMzQyOTI5OGFkYjhlYjcwYw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NmU3ZjQ4NTM5MDNmYmFmZDNmOGYyZTdjOGZmYjViYTE5NzgyMjRkZDdmNmMy
10
- ZDJlNDk4ODQxMjk0ZGZiNDgwYzUzYTc3MWJkNmNiZWFkZjcwYmIyN2M4MWUw
11
- ZTUyYWQ2ZGE2Zjk3OGJkZTZjZGJhNjFiNGFjZWU2ZDgyZGVkNzk=
9
+ M2ZmOWQyNDQ3Yjg4MjE4YjYyNzFiOGU5ZmI4N2VkOTIzMDk4NjhlOGQ3YzEz
10
+ Mjg4M2NlNDZiZmFjZmRhZmMzMGZkOTE2OTllMTAyNjcwMzY5MmVjZTc4MWMw
11
+ OWQ3NGY2MGZiYWNjNTAwMDc2MGNmNGYzNTY4NWZiZTQ2ODdjNmQ=
12
12
  data.tar.gz: !binary |-
13
- OTgyYWUzMjJkMjY0MWY2Zjg3MjM3NDc1YzZlNGMwZjNlNTc4MWZkMzQyNTRk
14
- ZGI3OWMxNjAxYmY5NGJjYzgwY2ZhOTA4NTY0YjFkYzhkM2Q2MzBhMGRjZGU0
15
- NWExZThlNTM4MzY2NWFkMGIxNGRiZmRjYmUxMTMwNDE4MGNiZWE=
13
+ MWE1ODZlNjEzMzdiYWRjNWQ4YzcyMzhlM2NjYjMyYmJhNzIwZjY5N2ZlZjBj
14
+ ZWM2NTc1NTFjZDUxMTc2Njk3OTE1MDE2ZmVmNTFhYzdmMTJiYjc0MmIzODY5
15
+ Mjk2NjFkMTIwODJmNjIzNDQxYzU3YzM4OTcwYjg3NThlMzQ1ZTc=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "cacheable_delegator"
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dan Nguyen"]
12
- s.date = "2013-10-27"
12
+ s.date = "2013-10-29"
13
13
  s.description = "Create a cache model for your active records"
14
14
  s.email = "dansonguyen@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -15,13 +15,16 @@ module CacheableDelegator
15
15
 
16
16
  module ClassMethods
17
17
 
18
- # this method defines the relation to an existing ActiveModel
19
- # it does not change the schema unless invoked with the bang!
20
- def cache_and_delegate(klass, opts={}, &blk)
18
+ def cache_record_class(klass)
21
19
  raise ArgumentError, "Must pass in a class, not a #{klass}" unless klass.is_a?(Class)
22
20
  self.source_class = klass
23
- belongs_to :source_record, class_name: self.source_class.to_s
21
+ belongs_to :source_record, class_name: self.source_class.to_s
22
+ end
24
23
 
24
+ # this method defines the relation to an existing ActiveModel
25
+ # it does not change the schema unless invoked with the bang!
26
+ def cache_and_delegate(klass, opts={}, &blk)
27
+ cache_record_class(klass)
25
28
  # This is why you have to define custom columns in the given block
26
29
  # or after the call to cache and delegate
27
30
 
@@ -84,6 +87,10 @@ module CacheableDelegator
84
87
  source_class.columns.reject{|c| EXCLUDED_FIELDS.any?{|f| f.to_s == c.name }}
85
88
  end
86
89
 
90
+ def source_column_names
91
+ source_columns.map{|s| s.name}
92
+ end
93
+
87
94
  def source_reflections
88
95
  source_class.reflections
89
96
  end
@@ -112,15 +119,15 @@ module CacheableDelegator
112
119
  def add_custom_column(col_name, opts={})
113
120
  col_str = col_name.to_s
114
121
  is_bespoke = opts.delete :bespoke
115
- if !self.source_class.method_defined?(col_str) && is_bespoke != true
116
- raise ArgumentError, "Since :bespoke != true, #{self.source_class} was expected to respond_to? :#{col_str}"
122
+ if !(self.source_class.method_defined?(col_str) || self.source_class.new.respond_to?(col_str)) && is_bespoke != true
123
+ raise ArgumentError, "Since :bespoke != true, instance of #{self.source_class} was expected to respond_to? :#{col_str}"
117
124
  end
118
125
 
119
126
  custom_columns[col_name.to_s] = opts
120
127
  end
121
128
 
122
129
  def custom_columns
123
- self._custom_columns ||= {}
130
+ self._custom_columns
124
131
  end
125
132
 
126
133
  def reset_custom_columns!
@@ -158,16 +165,23 @@ module CacheableDelegator
158
165
 
159
166
  ################### Builder methods
160
167
  module ClassMethods
161
- def build_cache(source_obj)
168
+
169
+ def build_attributes_hash(source_obj)
162
170
  att_hsh = self.value_column_names.inject({}) do |hsh, cname|
163
- hsh[cname] = source_obj.send cname
171
+ if source_obj.respond_to?(cname)
172
+ hsh[cname] = source_obj.send cname
173
+ end
164
174
  hsh
165
175
  end
176
+ end
166
177
 
167
- obj = self.new(att_hsh)
168
- obj.source_record = source_obj
178
+ def build_cache(source_obj)
179
+ att_hsh = build_attributes_hash(source_obj)
169
180
 
170
- obj
181
+ c_record = self.new(att_hsh)
182
+ c_record.source_record = source_obj
183
+
184
+ c_record
171
185
  end
172
186
 
173
187
  def create_cache(obj)
@@ -178,5 +192,17 @@ module CacheableDelegator
178
192
  end
179
193
  end
180
194
 
195
+ # instance method
196
+ # pre: must already have source_record set
197
+
198
+
199
+ def refresh_cache!
200
+ atts = self.class.build_attributes_hash(self.source_record)
201
+ self.update_attributes(obj)
202
+
203
+ self
204
+ end
205
+
206
+
181
207
  end
182
208
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cacheable_delegator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Nguyen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-27 00:00:00.000000000 Z
11
+ date: 2013-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry