platanus 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/platanus/model_shims.rb +69 -0
- data/lib/platanus/version.rb +1 -1
- metadata +2 -1
@@ -0,0 +1,69 @@
|
|
1
|
+
# api_boilerplate.rb : ActiveRecord Activable mod.
|
2
|
+
#
|
3
|
+
# Copyright July 2012, Ignacio Baixas +mailto:ignacio@platan.us+.
|
4
|
+
|
5
|
+
module Platanus
|
6
|
+
|
7
|
+
# # Boilerplate for platanus json api controllers
|
8
|
+
#
|
9
|
+
# Provides base error handling and rendering methods.
|
10
|
+
# Also provides a couple of opt-in behaviours..
|
11
|
+
#
|
12
|
+
module ModelShims
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.extend ClassMethods
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
def shims_for(_name, _options={})
|
21
|
+
|
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
|
24
|
+
|
25
|
+
name = _name.to_s
|
26
|
+
model = _options.fetch(:class_name, name.camelize).constantize
|
27
|
+
prefix = _options.fetch :prefix, name
|
28
|
+
cache_var = _options.fetch :into, "@_#{name}_shims"
|
29
|
+
|
30
|
+
model.accessible_attributes.each do |attr_name|
|
31
|
+
|
32
|
+
full_attr_name = if prefix then "#{prefix}_#{attr_name}" else attr_name end
|
33
|
+
|
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}"
|
43
|
+
end
|
44
|
+
|
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)
|
50
|
+
if child then child.send(attr_name) else nil end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
Rails.logger.warn "shims_for: failed to generate getter for #{full_attr_name} in #{self.to_s}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
define_method("#{name}_shims_changed?") do
|
58
|
+
not instance_variable_get(cache_var).nil?
|
59
|
+
end
|
60
|
+
|
61
|
+
define_method("#{name}_shims_flush") do
|
62
|
+
cache = instance_variable_get(cache_var)
|
63
|
+
instance_variable_set(cache_var, nil)
|
64
|
+
return cache
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/platanus/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/platanus/gcontroller.rb
|
66
66
|
- lib/platanus/http_helpers.rb
|
67
67
|
- lib/platanus/layered.rb
|
68
|
+
- lib/platanus/model_shims.rb
|
68
69
|
- lib/platanus/onetime.rb
|
69
70
|
- lib/platanus/serializers/json_sym.rb
|
70
71
|
- lib/platanus/stacked.rb
|