Module: FlattenRecord::Flattener::ClassMethods
- Defined in:
- lib/flatten_record/flattener.rb
Instance Method Summary collapse
- #create_with(normal) ⇒ Object (also: #update_with)
- #denormalize(normal_model, definition_hash) ⇒ Object
- #destroy_with(normal) ⇒ Object
- #destroy_with_id(normal_id, normal_class = normal_model) ⇒ Object
- #find_node(type, value) ⇒ Object
- #find_normals(normal) ⇒ Object
- #find_normals_with_id(normal_id, normal_class = normal_model) ⇒ Object
- #find_with(normal) ⇒ Object
- #find_with_id(normal_id, normal_class = normal_model) ⇒ Object
- #flattener_meta ⇒ Object
Instance Method Details
#create_with(normal) ⇒ Object Also known as: update_with
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/flatten_record/flattener.rb', line 39 def create_with(normal) if normal_model.eql?(normal.class) destroy_with(normal) records = .denormalize(normal.reload, self.new) records.respond_to?(:each) ? records.each(&:save) : records.save records else destroy_with(normal) find_normals(normal).each do |n| create_with(n) end end end |
#denormalize(normal_model, definition_hash) ⇒ Object
20 21 22 23 |
# File 'lib/flatten_record/flattener.rb', line 20 def denormalize(normal_model, definition_hash) self.class_variable_set("@@definition_hash", definition_hash) self.class_variable_set("@@normal_model", normal_model.to_s.camelize.constantize) end |
#destroy_with(normal) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/flatten_record/flattener.rb', line 54 def destroy_with(normal) if normal_model.eql?(normal.class) records = find_with(normal) records.each(&:destroy) else # update associated model find_normals(normal).each do |n| update_with(n) end end records end |
#destroy_with_id(normal_id, normal_class = normal_model) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/flatten_record/flattener.rb', line 67 def destroy_with_id(normal_id, normal_class=normal_model) if normal_model.eql?(normal_class) records = find_with_id(normal_id, normal_class) records.each(&:destroy) else # update associated model find_normals_with_id(normal_id, normal_class).each do |n| update_with(n) end end records end |
#find_node(type, value) ⇒ Object
122 123 124 |
# File 'lib/flatten_record/flattener.rb', line 122 def find_node(type, value) .traverse_by(type, value) end |
#find_normals(normal) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/flatten_record/flattener.rb', line 80 def find_normals(normal) return normal if normal_model.eql?(normal.class) records = find_with(normal) id_name = .id_column.name normal_id_name = .id_column.column.name ids = records.collect{|c| c.send(id_name.to_sym) } normal_model.where(normal_id_name => ids) end |
#find_normals_with_id(normal_id, normal_class = normal_model) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/flatten_record/flattener.rb', line 91 def find_normals_with_id(normal_id, normal_class=normal_model) return normal_class.find(normal_id) if normal_model.eql?(normal_class) records = find_with_id(normal_id, normal_class) id_name = .id_column.name normal_id_name = .id_column.column.name ids = records.collect{|c| c.send(id_name.to_sym) } normal_model.where(normal_id_name => ids) end |
#find_with(normal) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/flatten_record/flattener.rb', line 102 def find_with(normal) node = find_node(:target_model, normal.class) raise "#{normal.class} was not defined in the denormalization" if node.nil? id_name = node.id_column.name normal_id_name = node.id_column.column.name self.where(id_name => normal.send(normal_id_name)) end |
#find_with_id(normal_id, normal_class = normal_model) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/flatten_record/flattener.rb', line 112 def find_with_id(normal_id, normal_class=normal_model) node = find_node(:target_model, normal_class) raise "#{normal_class} was not defined in the denormalization" if node.nil? id_name = node.id_column.name normal_id_name = node.id_column.column.name self.where(id_name => normal_id) end |
#flattener_meta ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/flatten_record/flattener.rb', line 25 def node = self.class_variable_get(:@@root_node) if node.blank? definition = Definition.new(self.definition_hash) root_node = Meta::RootNode.new(self.normal_model, self) root_node.build(definition) node = self.class_variable_set("@@root_node", root_node) end node end |