Class: Fire::NestedModel

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

Defined Under Namespace

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



42
43
44
45
46
47
48
# File 'lib/model/nested/nested_model.rb', line 42

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

Class Method Details

.collection_nameObject



37
38
39
# File 'lib/model/nested/nested_model.rb', line 37

def collection_name
  parent.collection_name
end

.has_path_keys(*keys) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/model/nested/nested_model.rb', line 19

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



15
16
17
# File 'lib/model/nested/nested_model.rb', line 15

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

.nested_in(parent, options) ⇒ Object



27
28
29
30
31
# File 'lib/model/nested/nested_model.rb', line 27

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

.own_path_keysObject



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

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

Instance Method Details

#saving_dataObject



6
7
8
9
10
11
12
# File 'lib/model/nested/nested_model.rb', line 6

def saving_data
  res = data.clone
  self.class.parent.all_path_keys.each do |k|
    res.delete(k)
  end
  res
end