Class: Fire::NestedModel
- Inherits:
-
Model
- Object
- OpenStruct
- Model
- Fire::NestedModel
show all
- Defined in:
- lib/model/nested/base.rb,
lib/model/nested/parent.rb
Defined Under Namespace
Modules: Parent
Classes: CollectionIsSetError, DuplicatedParentPathKeyError, ParentModelNotSetError
Constant Summary
Constants inherited
from Model
Model::LEVEL_SEPARATOR
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Model
#==, all_path_keys, #collection_name, connection, create, #custom_data, #data, #delete, #has_data?, id_key, #id_key, #id_value, #initialize, next_id, #path, #path_changed?, #path_data, path_value_param, #path_values, #persisted?, prepare_hash, #save, set_id_key, take
Constructor Details
This class inherits a constructor from Fire::Model
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/model/nested/base.rb', line 65
def method_missing(*args)
if args.first.to_s == self.class.folder
self.class.folder
else
super
end
end
|
Class Method Details
.collection_name ⇒ Object
41
42
43
|
# File 'lib/model/nested/base.rb', line 41
def collection_name
parent.collection_name
end
|
.folder ⇒ Object
45
46
47
|
# File 'lib/model/nested/base.rb', line 45
def folder
path_value_param(self.nested_options.folder ? self.nested_options.folder : default_folder_name)
end
|
.folder_content(parent) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/model/nested/base.rb', line 49
def folder_content(parent)
levels_count = (path_keys || []).count
objects = self.down_levels(parent.send(folder), levels_count)
objects.map{|x|
full_data = x.merge(parent.path_data)
new(full_data)
}
end
|
.has_path_keys(*keys) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/model/nested/base.rb', line 23
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
|
.in_collection(name) ⇒ Object
19
20
21
|
# File 'lib/model/nested/base.rb', line 19
def in_collection(name)
raise CollectionIsSetError.new(self)
end
|
.nested_in(parent, options = {}) ⇒ Object
31
32
33
34
35
|
# File 'lib/model/nested/base.rb', line 31
def nested_in(parent, options={})
self.parent = parent
self.nested_options = OpenStruct.new(options)
self.parent.has_nested(self)
end
|
.own_path_keys ⇒ Object
37
38
39
|
# File 'lib/model/nested/base.rb', line 37
def own_path_keys
parent.all_path_keys + [ folder ] + super()
end
|
Instance Method Details
#nested_options ⇒ Object
14
15
16
|
# File 'lib/model/nested/base.rb', line 14
def nested_options
self.class.nested_options
end
|
#saving_data ⇒ Object
6
7
8
9
10
11
12
|
# File 'lib/model/nested/base.rb', line 6
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
|