Class: Fire::NestedModel
- Inherits:
-
Model
- Object
- OpenStruct
- Model
- Fire::NestedModel
show all
- Defined in:
- lib/model/nested/base.rb
Defined Under Namespace
Classes: CollectionIsSetError, DuplicatedIdKeyError, DuplicatedParentPathKeyError, ParentModelNotSetError, QueryingNotSupportedError
Constant Summary
Constants inherited
from Model
Model::LEVEL_SEPARATOR
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Model
#==, all_path_keys, #cache, #collection_name, connection, create, #custom_data, #data, #delete, #delete_field, #has_data?, #id_key, #id_value, next_id, #path, #path_changed?, #path_data, path_value_param, #path_values, #persisted?, prepare_hash, #reload, #set, set_id_key, take, #update, #update_field
Constructor Details
#initialize(hash = {}, parent_original = {}) ⇒ NestedModel
Returns a new instance of NestedModel.
6
7
8
9
|
# File 'lib/model/nested/base.rb', line 6
def initialize(hash={}, parent_original={})
@parent_original = parent_original
super(hash)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/model/nested/base.rb', line 103
def method_missing(*args)
if args.first.to_s == self.class.folder
self.class.folder
else
super
end
end
|
Class Method Details
.all ⇒ Object
88
89
90
|
# File 'lib/model/nested/base.rb', line 88
def all
query
end
|
.collection_name ⇒ Object
66
67
68
|
# File 'lib/model/nested/base.rb', line 66
def collection_name
parent.collection_name
end
|
.folder ⇒ Object
70
71
72
|
# File 'lib/model/nested/base.rb', line 70
def folder
path_value_param(self.nested_options.folder ? self.nested_options.folder : default_folder_name)
end
|
.folder_content(parent) ⇒ Object
74
75
76
77
78
79
80
81
82
|
# File 'lib/model/nested/base.rb', line 74
def folder_content(parent)
levels_count = (path_keys || []).count
nested_folder = parent.send(folder) || {}
objects = self.down_levels(nested_folder, levels_count)
objects.map{|parent_original|
full_data = parent_original.clone.merge(parent.path_data)
new(full_data, parent_original)
}
end
|
.has_path_keys(*keys) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/model/nested/base.rb', line 47
def has_path_keys(*keys)
raise ParentModelNotSetError.new(self) unless self.parent
super(*keys)
keys.each do |key|
raise DuplicatedParentPathKeyError.new(key, self.parent) if self.parent.all_path_keys.include?(key)
end
end
|
.id_key ⇒ Object
32
33
34
|
# File 'lib/model/nested/base.rb', line 32
def self.id_key
self.id_key_name || "#{self.name.demodulize.singularize.dasherize.downcase}_id".to_sym
end
|
.in_collection(name) ⇒ Object
43
44
45
|
# File 'lib/model/nested/base.rb', line 43
def in_collection(name)
raise CollectionIsSetError.new(self)
end
|
.nested_in(parent, options = {}) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/model/nested/base.rb', line 55
def nested_in(parent, options={})
self.parent = parent
self.nested_options = OpenStruct.new(options)
self.parent.has_nested(self)
validate_id_key!
end
|
.own_path_keys ⇒ Object
62
63
64
|
# File 'lib/model/nested/base.rb', line 62
def own_path_keys
parent.all_path_keys + [ folder ] + super()
end
|
.query(params = {}, &filter_condition) ⇒ Object
Instance Method Details
#nested_options ⇒ Object
28
29
30
|
# File 'lib/model/nested/base.rb', line 28
def nested_options
self.class.nested_options
end
|
#save ⇒ Object
23
24
25
26
|
# File 'lib/model/nested/base.rb', line 23
def save
sync_parent
super
end
|
#saving_data ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/model/nested/base.rb', line 15
def saving_data
return super() if nested_options.parent_values
self.class.parent.all_path_keys.each_with_object(data.clone) do |k, res|
res.delete(k)
end
end
|
#set_id_key(value) ⇒ Object
36
37
38
39
40
|
# File 'lib/model/nested/base.rb', line 36
def set_id_key(value)
raise ParentModelNotSetError.new(self) unless self.parent
validate_id_key!
super(value)
end
|
#sync_parent ⇒ Object
11
12
13
|
# File 'lib/model/nested/base.rb', line 11
def sync_parent
@parent_original.merge!(saving_data)
end
|