Class: PostJson::Base
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
15
16
|
# File 'lib/post_json/base.rb', line 10
def initialize(*args)
__local__primary_key = self.class.primary_key
__local__attrs = (args[0] || {}).with_indifferent_access
__local__attrs[__local__primary_key] = __local__attrs[__local__primary_key].to_s if __local__attrs.has_key?(__local__primary_key)
args[0] = {__local__primary_key => __local__attrs[__local__primary_key], '__doc__body' => __local__attrs}.with_indifferent_access
super
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *args, &block) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/post_json/base.rb', line 127
def method_missing(method_symbol, *args, &block)
method_name = method_symbol.to_s
attribute_name = if method_name.end_with?("_changed?")
method_name[0..-10]
elsif method_name.end_with?("_was")
method_name[0..-5]
elsif method_name.end_with?("=")
method_name[0..-2]
elsif method_name.end_with?("_change")
method_name[0..-8]
else
method_name
end
if attribute_name.in?(attribute_names) == false
self.class.define_attribute_accessor(attribute_name)
send(method_symbol, *args)
else
super
end
end
|
Class Method Details
.collection_name ⇒ Object
169
170
171
172
173
174
175
176
|
# File 'lib/post_json/base.rb', line 169
def collection_name
message = "You need to assign a collection name to class \"#{name}\":
class #{name}
self.collection_name = \"people\"
end"
raise ArgumentError, message unless @collection_name.present?
@collection_name
end
|
.collection_name=(name) ⇒ Object
178
179
180
181
182
183
|
# File 'lib/post_json/base.rb', line 178
def collection_name=(name)
raise ArgumentError, "Collection name must be present" unless name.present?
@collection_name = name.to_s.strip
reload_settings!
@collection_name
end
|
.default_scopes ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/post_json/base.rb', line 161
def default_scopes
model_settings = ModelSettings.table_name
query = original_all.joins("INNER JOIN \"#{model_settings}\" ON lower(\"#{model_settings}\".collection_name) = '#{collection_name.downcase}'")
query = query.where("\"#{table_name}\".__doc__model_settings_id = \"#{model_settings}\".id")
super + [Proc.new { query }]
end
|
.define_attribute_accessor(attribute_name) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/post_json/base.rb', line 194
def define_attribute_accessor(attribute_name)
class_eval " def \#{attribute_name}\n __doc__body_read_attribute('\#{attribute_name}')\n end\n\n def \#{attribute_name}=(value)\n __doc__body_write_attribute('\#{attribute_name}', value)\n end\n\n def \#{attribute_name}_changed?\n __doc__body_attribute_changed?('\#{attribute_name}')\n end\n\n def \#{attribute_name}_was\n __doc__body_attribute_was('\#{attribute_name}')\n end\n\n def \#{attribute_name}_change\n __doc__body_attribute_change('\#{attribute_name}')\n end\n RUBY\nend\n"
|
.page(*args) ⇒ Object
157
158
159
|
# File 'lib/post_json/base.rb', line 157
def page(*args)
all.page(*args)
end
|
.post_json_all ⇒ Object
Also known as:
all
150
151
152
|
# File 'lib/post_json/base.rb', line 150
def post_json_all
QueryTranslator.new(original_all)
end
|
.rename_collection(new_name) ⇒ Object
185
186
187
188
189
190
191
192
|
# File 'lib/post_json/base.rb', line 185
def rename_collection(new_name)
new_name = new_name.to_s.strip
if settings.persisted?
settings.collection_name = new_name
settings.save!
end
@collection_name = new_name
end
|
Instance Method Details
#[](attribute_name) ⇒ Object
99
100
101
|
# File 'lib/post_json/base.rb', line 99
def [](attribute_name)
self.__doc__body_read_attribute(attribute_name)
end
|
#[]=(attribute_name, value) ⇒ Object
103
104
105
|
# File 'lib/post_json/base.rb', line 103
def []=(attribute_name, value)
self.__doc__body_write_attribute(attribute_name, value)
end
|
#__doc__body_attribute_change(attribute_name) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/post_json/base.rb', line 72
def __doc__body_attribute_change(attribute_name)
__local__change = [__doc__body_attribute_was(attribute_name), __doc__body_read_attribute(attribute_name)]
if __local__change[0] == __local__change[1]
nil
else
__local__change
end
end
|
#__doc__body_attribute_changed?(attribute_name) ⇒ Boolean
68
69
70
|
# File 'lib/post_json/base.rb', line 68
def __doc__body_attribute_changed?(attribute_name)
(self.__doc__body == nil ? nil : self.__doc__body.with_indifferent_access[attribute_name]) != self.__doc__body_attribute_was(attribute_name)
end
|
#__doc__body_attribute_was(attribute_name) ⇒ Object
64
65
66
|
# File 'lib/post_json/base.rb', line 64
def __doc__body_attribute_was(attribute_name)
self.__doc__body_was == nil ? nil : self.__doc__body_was.with_indifferent_access[attribute_name]
end
|
#__doc__body_convert_attribute_type(attribute_name, value) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/post_json/base.rb', line 81
def __doc__body_convert_attribute_type(attribute_name, value)
case value
when /^[0-9]{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]{3}Z$/
Time.parse(value).in_time_zone
when Hash
value.inject(HashWithIndifferentAccess.new) do |result, (key, value)|
result[key] = convert_document_attribute_type("#{attribute_name}.#{key}", value)
result
end
when Array
value.map.with_index do |array_value, index|
convert_document_attribute_type("#{attribute_name}[#{index}]", array_value)
end
else
value
end
end
|
#__doc__body_read_attribute(attribute_name) ⇒ Object
54
55
56
57
|
# File 'lib/post_json/base.rb', line 54
def __doc__body_read_attribute(attribute_name)
__local__value = self.__doc__body[attribute_name.to_s] if self.__doc__body
__doc__body_convert_attribute_type(attribute_name, __local__value)
end
|
#__doc__body_write_attribute(attribute_name, value) ⇒ Object
59
60
61
62
|
# File 'lib/post_json/base.rb', line 59
def __doc__body_write_attribute(attribute_name, value)
self.__doc__body = HashWithIndifferentAccess.new(self.__doc__body).merge(attribute_name.to_s => value)
value
end
|
#attribute_changed?(attribute_name) ⇒ Boolean
45
46
47
48
49
50
51
52
|
# File 'lib/post_json/base.rb', line 45
def attribute_changed?(attribute_name)
attribute_name = attribute_name.to_s
if attribute_name.in?(attribute_names)
super
else
__doc__body_attribute_changed?(attribute_name)
end
end
|
#attributes ⇒ Object
24
25
26
|
# File 'lib/post_json/base.rb', line 24
def attributes
read_attribute('__doc__body').try(:with_indifferent_access) || HashWithIndifferentAccess.new
end
|
#cache_key ⇒ Object
18
19
20
21
22
|
# File 'lib/post_json/base.rb', line 18
def cache_key
@dashed_name ||= self.class.name.underscore.dasherize
__local__unique_version = __doc__version || Digest::MD5.hexdigest(attributes.inspect)
"#{@dashed_name}-#{id}-version-#{__local__unique_version}"
end
|
#respond_to?(method_symbol, include_all = false) ⇒ Boolean
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/post_json/base.rb', line 107
def respond_to?(method_symbol, include_all = false)
if super
true
else
method_name = method_symbol.to_s
attribute_name = if method_name.end_with?("_changed?")
method_name[0..-10]
elsif method_name.end_with?("_was")
method_name[0..-5]
elsif method_name.end_with?("=")
method_name[0..-2]
elsif method_name.end_with?("_change")
method_name[0..-8]
else
method_name
end
attributes.has_key?(attribute_name)
end
end
|
#to_h ⇒ Object
28
29
30
|
# File 'lib/post_json/base.rb', line 28
def to_h
attributes.deep_dup
end
|
#write_attribute(attribute_name, value) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/post_json/base.rb', line 32
def write_attribute(attribute_name, value)
attribute_name = attribute_name.to_s
if attribute_name == '__doc__body'
value = value.try(:with_indifferent_access)
self.__doc__body_will_change! unless self.__doc__body.try(:with_indifferent_access) == value
super('__doc__body', value)
elsif attribute_name.in?(attribute_names)
super
else
__doc__body_write_attribute(attribute_name, value)
end
end
|