e9_rails 0.0.14 → 0.0.15
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.
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_model/naming'
|
2
2
|
require 'active_model/translation'
|
3
3
|
require 'active_support/hash_with_indifferent_access'
|
4
|
+
require 'ostruct'
|
4
5
|
|
5
6
|
module E9Rails::ActiveRecord
|
6
7
|
module InheritableOptions
|
@@ -18,24 +19,21 @@ module E9Rails::ActiveRecord
|
|
18
19
|
class_inheritable_accessor :options_parameters
|
19
20
|
self.options_parameters = []
|
20
21
|
|
21
|
-
class_inheritable_accessor :options_class
|
22
|
-
self.options_class = Options
|
23
|
-
|
24
22
|
class_inheritable_accessor :delegate_options_methods
|
25
23
|
self.delegate_options_methods = false
|
26
24
|
|
27
|
-
|
25
|
+
class_inheritable_accessor :options_class
|
28
26
|
end
|
29
27
|
|
30
28
|
def options=(hash={})
|
31
29
|
ensuring_method_attributes do
|
32
|
-
|
30
|
+
write_options(hash)
|
33
31
|
end
|
34
32
|
end
|
35
33
|
|
36
34
|
def options
|
37
35
|
ensuring_method_attributes do
|
38
|
-
opts =
|
36
|
+
opts = read_options
|
39
37
|
opts.reverse_merge! Hash[options_parameters.map(&:to_s).zip([nil])]
|
40
38
|
options_class.new(opts, self)
|
41
39
|
end
|
@@ -43,6 +41,14 @@ module E9Rails::ActiveRecord
|
|
43
41
|
|
44
42
|
protected
|
45
43
|
|
44
|
+
def write_options(hash={})
|
45
|
+
write_attribute(options_column, hash.stringify_keys)
|
46
|
+
end
|
47
|
+
|
48
|
+
def read_options
|
49
|
+
read_attribute(options_column) || {}
|
50
|
+
end
|
51
|
+
|
46
52
|
def ensuring_method_attributes
|
47
53
|
yield
|
48
54
|
rescue
|
@@ -60,18 +66,96 @@ module E9Rails::ActiveRecord
|
|
60
66
|
end
|
61
67
|
|
62
68
|
def inheritable_options_initialized?
|
63
|
-
|
69
|
+
!!@inheritable_options_initialized
|
64
70
|
end
|
65
71
|
|
66
72
|
def initialize_inheritable_options
|
67
73
|
return if inheritable_options_initialized?
|
68
|
-
|
74
|
+
|
75
|
+
self.options_class ||= self.options_parameters.present? ? Options : IndifferentOptions
|
76
|
+
self.options_class.lookup_ancestors = lookup_ancestors
|
77
|
+
|
78
|
+
if self.options_column
|
79
|
+
serialized_attributes[self.options_column.to_s] = Hash
|
80
|
+
end
|
69
81
|
|
70
82
|
if self.delegate_options_methods
|
71
83
|
self.options_parameters.each do |param|
|
72
84
|
delegate param, "#{param}=", :to => :options
|
73
85
|
end
|
74
86
|
end
|
87
|
+
|
88
|
+
@inheritable_options_initialized = true
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class IndifferentOptions < OpenStruct
|
93
|
+
extend ActiveModel::Naming
|
94
|
+
extend ActiveModel::Translation
|
95
|
+
|
96
|
+
# implementation of lookup_ancestors for AM & i18n
|
97
|
+
class_inheritable_accessor :lookup_ancestors
|
98
|
+
|
99
|
+
attr_reader :base
|
100
|
+
|
101
|
+
class << self
|
102
|
+
def name; 'Options' end
|
103
|
+
def i18n_scope; :activerecord end
|
104
|
+
end
|
105
|
+
|
106
|
+
def extractable_options?
|
107
|
+
false
|
108
|
+
end
|
109
|
+
|
110
|
+
def initialize(hash, base)
|
111
|
+
@table = {}
|
112
|
+
for k,v in hash
|
113
|
+
new_ostruct_member(k)
|
114
|
+
@table[k.to_sym] = v
|
115
|
+
end
|
116
|
+
@base = base
|
117
|
+
end
|
118
|
+
|
119
|
+
def to_hash
|
120
|
+
@table
|
121
|
+
end
|
122
|
+
|
123
|
+
# we respond to anything!
|
124
|
+
def respond_to?(method_name)
|
125
|
+
true
|
126
|
+
end
|
127
|
+
|
128
|
+
def new_ostruct_member(name)
|
129
|
+
name = name.to_sym
|
130
|
+
unless @table.keys.member?(name)
|
131
|
+
class << self; self; end.class_eval do
|
132
|
+
define_method(name) { @table[name] }
|
133
|
+
define_method("#{name}=") do |x|
|
134
|
+
modifiable[name] = x
|
135
|
+
@base.options = to_hash
|
136
|
+
modifiable[name]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
name
|
141
|
+
end
|
142
|
+
|
143
|
+
# If OpenStruct first generates the new_ostruct_member methods with
|
144
|
+
# a setter, it does not use the newly generated methods to set the
|
145
|
+
# attribute the first time, but rather does this:
|
146
|
+
#
|
147
|
+
# modifiable[new_ostruct_member(mname)] = args[0]
|
148
|
+
#
|
149
|
+
# Because of this it's necessary to explicitly call the newly created
|
150
|
+
# setter after new_ostruct_member does its job to ensure that
|
151
|
+
# @base.options is written.
|
152
|
+
#
|
153
|
+
def method_missing(mid, *args)
|
154
|
+
super
|
155
|
+
|
156
|
+
if mid.id2name =~ /=$/
|
157
|
+
send(mid, *args)
|
158
|
+
end
|
75
159
|
end
|
76
160
|
end
|
77
161
|
|
data/lib/e9_rails/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: e9_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.15
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Travis Cox
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-25 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|