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
-
#==(other) ⇒ Object
(also: #eql?)
Override comparison of instances.
-
#create(options = {}) ⇒ Object
Create the document.
- #destroy ⇒ Object
-
#hash ⇒ Object
Calculate hash manually in order to only include unique properties for comparison.
-
#initialize(properties = {}) ⇒ Node
constructor
A new instance of Node.
-
#to_s ⇒ Object
Override to_s to make debugging easier.
-
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new.
Methods included from Adapters::CarrierWave
Methods included from Relationships
Methods included from Persistency
#create!, #destroyed?, #id, #new?, #persisted?, #save, #save!, #update_attributes
Methods included from Connection
Constructor Details
#initialize(properties = {}) ⇒ Node
Returns a new instance of Node.
48 49 50 51 52 |
# File 'lib/architect4r/model/node.rb', line 48 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.
46 47 48 |
# File 'lib/architect4r/model/node.rb', line 46 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 |
# 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.cypher_query(query).to_a.first the_root &&= the_root['x'] # otherwise create one the_root ||= begin m_root = connection.create_node(:name => "#{name} Root", :root_for => name) connection.create_relationship(0, m_root, model_root_relation_type, { 'architect4r_type' => name }) # Return model root node GenericNode.send(:build_from_database, m_root) end end end end end |
.model_root_relation_type ⇒ Object
128 129 130 |
# File 'lib/architect4r/model/node.rb', line 128 def self.model_root_relation_type 'model_root' end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Override comparison of instances
69 70 71 |
# File 'lib/architect4r/model/node.rb', line 69 def ==(other) other.is_a?(self.class) && id.present? && other.id == id end |
#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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/architect4r/model/node.rb', line 77 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
118 119 120 121 122 123 124 125 126 |
# File 'lib/architect4r/model/node.rb', line 118 def destroy run_callbacks :destroy do if result = connection.delete_node(self.id) @_destroyed = true self.freeze end result end end |
#hash ⇒ Object
Calculate hash manually in order to only include unique properties for comparison
63 64 65 |
# File 'lib/architect4r/model/node.rb', line 63 def hash [self.class, self.id].hash end |
#to_s ⇒ Object
Override to_s to make debugging easier. It now includes the id and properties
56 57 58 59 |
# File 'lib/architect4r/model/node.rb', line 56 def to_s prop_data = @properties_data.collect { |key, value| "#{key}='#{value}'" }.join(' ') "#<#{self.class.name}:#{object_id} id=#{id} #{prop_data} neo4j_uri='#{@raw_data['self']}'>" end |
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/architect4r/model/node.rb', line 99 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 |