Class: Fire::NestedModel

Inherits:
Model
  • Object
show all
Defined in:
lib/model/nested/base.rb

Direct Known Subclasses

SingleNestedModel

Defined Under Namespace

Classes: CollectionIsSetError, 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, #has_data?, id_key, #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

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



88
89
90
91
92
93
94
# File 'lib/model/nested/base.rb', line 88

def method_missing(*args)
  if args.first.to_s == self.class.folder
    self.class.folder
  else
    super
  end
end

Class Method Details

.allObject



77
78
79
# File 'lib/model/nested/base.rb', line 77

def all
  query
end

.collection_nameObject



55
56
57
# File 'lib/model/nested/base.rb', line 55

def collection_name
  parent.collection_name
end

.folderObject



59
60
61
# File 'lib/model/nested/base.rb', line 59

def folder
  path_value_param(self.nested_options.folder ? self.nested_options.folder : default_folder_name)
end

.folder_content(parent) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/model/nested/base.rb', line 63

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



37
38
39
40
41
42
43
# File 'lib/model/nested/base.rb', line 37

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



33
34
35
# File 'lib/model/nested/base.rb', line 33

def in_collection(name)
  raise CollectionIsSetError.new(self)
end

.nested_in(parent, options = {}) ⇒ Object



45
46
47
48
49
# File 'lib/model/nested/base.rb', line 45

def nested_in(parent, options={})
  self.parent = parent
  self.nested_options = OpenStruct.new(options)
  self.parent.has_nested(self)
end

.own_path_keysObject



51
52
53
# File 'lib/model/nested/base.rb', line 51

def own_path_keys
  parent.all_path_keys + [ folder ] + super()
end

.query(params = {}, &filter_condition) ⇒ Object



73
74
75
# File 'lib/model/nested/base.rb', line 73

def query(params={}, &filter_condition)
  raise QueryingNotSupportedError.new
end

Instance Method Details

#nested_optionsObject



28
29
30
# File 'lib/model/nested/base.rb', line 28

def nested_options
  self.class.nested_options
end

#saveObject



23
24
25
26
# File 'lib/model/nested/base.rb', line 23

def save
  sync_parent
  super
end

#saving_dataObject



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

#sync_parentObject



11
12
13
# File 'lib/model/nested/base.rb', line 11

def sync_parent
  @parent_original.merge!(saving_data)
end