platanus 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,16 @@
1
- # api_boilerplate.rb : ActiveRecord Activable mod.
1
+ # model_shims.rb : ActiveRecord model shims.
2
2
  #
3
- # Copyright July 2012, Ignacio Baixas +mailto:ignacio@platan.us+.
3
+ # Copyright April 2013, Ignacio Baixas +mailto:ignacio@platan.us+.
4
4
 
5
5
  module Platanus
6
6
 
7
- # # Boilerplate for platanus json api controllers
7
+ ## Shim extension for active record models
8
8
  #
9
- # Provides base error handling and rendering methods.
10
- # Also provides a couple of opt-in behaviours..
9
+ # Provide the **shims_for** class method that generates getters and setters
10
+ # for every accesible attribute in another model and makes them accesible too (shims).
11
+ #
12
+ # Values stored using the shims can then be accessed using the _shims_changed?
13
+ # and _shims_flush methods.
11
14
  #
12
15
  module ModelShims
13
16
 
@@ -17,41 +20,59 @@ module Platanus
17
20
 
18
21
  module ClassMethods
19
22
 
23
+ ## Generate shims for a given model.
24
+ #
25
+ # @params [Hash] _options Options:
26
+ # * class_name: if given, the shims model class name, if no the camelize _name is used.
27
+ # * prefix: prefix to be used in shims, defaults to ''.
28
+ # * proxy: an attribute to be proxied, if given then getter shims will return the proxie's
29
+ # value for the property if not set.
30
+ # * sync_to: same as proxy, but also call proxy_will_change! if any shimmed attribute is set.
31
+ #
20
32
  def shims_for(_name, _options={})
21
33
 
22
- # TODO: detect if _name is an association and use the association's settings.
23
- # TODO: separate in attr_composite_writer and attr_composite_reader
34
+ # TODO: detect if _name is an association and use the association's settings.
35
+ # TODO: overriding options.
24
36
 
25
37
  name = _name.to_s
26
38
  model = _options.fetch(:class_name, name.camelize).constantize
27
- prefix = _options.fetch :prefix, name
28
39
  cache_var = _options.fetch :into, "@_#{name}_shims"
40
+ prefix = _options[:prefix]
41
+ sync_to = _options[:sync_to]
42
+ sync_to_fk = self.reflections[sync_to.to_sym].foreign_key if sync_to
43
+ proxy = _options.fetch :proxy, sync_to
29
44
 
30
45
  model.accessible_attributes.each do |attr_name|
31
46
 
32
- full_attr_name = if prefix then "#{prefix}_#{attr_name}" else attr_name end
47
+ full_attr_name = if prefix then "#{prefix}#{attr_name}" else attr_name end
33
48
 
34
- unless method_defined? "#{full_attr_name}="
35
- define_method "#{full_attr_name}=" do |value|
36
- cache = instance_variable_get(cache_var)
37
- cache = instance_variable_set(cache_var, {}) if cache.nil?
38
- cache[attr_name] = value
39
- end
40
- attr_accessible full_attr_name
41
- else
42
- Rails.logger.warn "shims_for: failed to generate setter for #{full_attr_name} in #{self.to_s}"
49
+ if method_defined? full_attr_name
50
+ Rails.logger.warn "shims_for: overriding getter for #{full_attr_name} in #{self.to_s}"
51
+ end
52
+
53
+ if method_defined? "#{full_attr_name}="
54
+ Rails.logger.warn "shims_for: overriding setter for #{full_attr_name} in #{self.to_s}"
43
55
  end
44
56
 
45
- unless method_defined? full_attr_name
46
- define_method full_attr_name do
47
- cache = instance_variable_get(cache_var)
48
- return cache[attr_name] if cache and cache.has_key? attr_name
49
- child = send(name)
57
+ # override getter
58
+ define_method full_attr_name do
59
+ cache = instance_variable_get(cache_var)
60
+ return cache[attr_name] if cache and cache.has_key? attr_name
61
+ if proxy
62
+ child = send(proxy)
50
63
  if child then child.send(attr_name) else nil end
51
64
  end
52
- else
53
- Rails.logger.warn "shims_for: failed to generate getter for #{full_attr_name} in #{self.to_s}"
54
65
  end
66
+
67
+ # override setter
68
+ define_method "#{full_attr_name}=" do |value|
69
+ cache = instance_variable_get(cache_var)
70
+ cache = instance_variable_set(cache_var, {}) if cache.nil?
71
+ cache[attr_name] = value
72
+ send "#{sync_to_fk}_will_change!" if sync_to # force update if synced
73
+ end
74
+ attr_accessible full_attr_name
75
+
55
76
  end
56
77
 
57
78
  define_method("#{name}_shims_changed?") do
@@ -1,3 +1,3 @@
1
1
  module Platanus
2
- VERSION = "0.1.5" # 0.2 will come with tests!
2
+ VERSION = "0.1.6" # 0.2 will come with tests!
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: platanus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
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: 2013-04-12 00:00:00.000000000 Z
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json