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.
- #to_s ⇒ Object
-
#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.
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
113 114 115 |
# File 'lib/architect4r/model/node.rb', line 113 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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/architect4r/model/node.rb', line 62 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
103 104 105 106 107 108 109 110 111 |
# File 'lib/architect4r/model/node.rb', line 103 def destroy run_callbacks :destroy do if result = connection.delete_node(self.id) @_destroyed = true self.freeze end result end end |
#to_s ⇒ Object
54 55 56 57 |
# File 'lib/architect4r/model/node.rb', line 54 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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/architect4r/model/node.rb', line 84 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 |