Class: Architect4r::Model::Node
- Inherits:
-
Object
- Object
- Architect4r::Model::Node
- Extended by:
- Adapters::CarrierWave
- Includes:
- Callbacks, Connection, Persistency, Queries, Relationships
- Defined in:
- lib/architect4r/model/node.rb,
lib/architect4r/adapters/carrier_wave.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#raw_data ⇒ Object
Returns the value of attribute raw_data.
Class Method Summary collapse
Instance Method Summary collapse
-
#create(options = {}) ⇒ Object
Create the document.
- #destroy ⇒ Object
-
#initialize(properties = {}) ⇒ Node
constructor
A new instance of Node.
-
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new.
Methods included from Adapters::CarrierWave
Constructor Details
#initialize(properties = {}) ⇒ Node
Returns a new instance of Node.
49 50 51 52 53 |
# File 'lib/architect4r/model/node.rb', line 49 def initialize(properties={}) run_callbacks :initialize do parse_properties(properties) end end |
Instance Attribute Details
#raw_data ⇒ Object
Returns the value of attribute raw_data.
47 48 49 |
# File 'lib/architect4r/model/node.rb', line 47 def raw_data @raw_data end |
Class Method Details
.inherited(subklass) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/architect4r/model/node.rb', line 16 def self.inherited(subklass) super subklass.send(:include, ActiveModel::Conversion) subklass.extend ActiveModel::Naming subklass.send(:include, Architect4r::Model::Properties) subklass.send(:include, Architect4r::Model::Validations) subklass.class_exec do def self.model_root @model_root ||= begin # Check if there is already a model root, query = "start root = node(0) match (root)-[r:#{ model_root_relation_type}]->(x) where r.architect4r_type and r.architect4r_type = '#{name}' return x" the_root = connection.execute_cypher(query).to_a.first the_root &&= the_root['x'] # otherwise create one the_root ||= begin m_root = connection.create_node(:name => "#{name} Root") connection.create_relationship(0, m_root, model_root_relation_type, { 'architect4r_type' => name }) m_root end # Return model root node GenericNode.send(:build_from_database, the_root) end end end end |
.model_root_relation_type ⇒ Object
109 110 111 |
# File 'lib/architect4r/model/node.rb', line 109 def self.model_root_relation_type 'model_root' end |
Instance Method Details
#create(options = {}) ⇒ Object
Create the document. Validation is enabled by default and will return false if the document is not valid. If all goes well, the document will be returned.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/architect4r/model/node.rb', line 58 def create( = {}) run_callbacks :create do run_callbacks :save do # only create valid records return false unless perform_validations() # perform creation if result = connection.create_node(self._to_database_hash) self.raw_data = result # Link the node with a model root node connection.create_relationship(self.id, self.class.model_root.id, 'model_type') end # if something goes wrong we receive a nil value and return false !result.nil? end end end |
#destroy ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/architect4r/model/node.rb', line 99 def destroy run_callbacks :destroy do if result = connection.delete_node(self.id) @_destroyed = true self.freeze end result end end |
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/architect4r/model/node.rb', line 80 def update( = {}) run_callbacks :update do run_callbacks :save do # Check if record can be updated raise "Cannot save a destroyed document!" if destroyed? raise "Calling #{self.class.name}#update on document that has not been created!" if new? # Check if we can continue return false unless perform_validations() # perform update result = connection.update_node(self.id, self._to_database_hash) # if something goes wrong we receive a nil value and return false !result.nil? end end end |