Class: Architect4r::Model::Relationship
- Inherits:
-
Object
- Object
- Architect4r::Model::Relationship
- Extended by:
- Adapters::CarrierWave
- Includes:
- Callbacks, Connection, Persistency
- Defined in:
- lib/architect4r/model/relationship.rb,
lib/architect4r/adapters/carrier_wave.rb
Instance Attribute Summary collapse
-
#destination ⇒ Object
Virtual attributes.
-
#raw_data ⇒ Object
Virtual attributes.
-
#source ⇒ Object
Virtual attributes.
Class Method Summary collapse
Instance Method Summary collapse
-
#create(options = {}) ⇒ Object
Create the document.
- #destroy ⇒ Object
-
#initialize(*args) ⇒ Relationship
constructor
Instance methods.
-
#update(options = {}) ⇒ Object
Trigger the callbacks (before, after, around) only if the document isn’t new.
Methods included from Adapters::CarrierWave
Methods included from Persistency
#create!, #destroyed?, #id, #new?, #persisted?, #save, #save!, #update_attributes
Methods included from Connection
Constructor Details
#initialize(*args) ⇒ Relationship
Instance methods
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/architect4r/model/relationship.rb', line 36 def initialize(*args) # Detect source and destination if s = args[0].is_a?(Architect4r::Model::Node) && args.shift self.source = s if d = args[0].is_a?(Architect4r::Model::Node) && args.shift self.destination = d end end # Detect properties properties = args[0].is_a?(Hash) && args.shift properties ||= {} parse_properties(properties) end |
Instance Attribute Details
#destination ⇒ Object
Virtual attributes
30 31 32 |
# File 'lib/architect4r/model/relationship.rb', line 30 def destination @destination end |
#raw_data ⇒ Object
Virtual attributes
30 31 32 |
# File 'lib/architect4r/model/relationship.rb', line 30 def raw_data @raw_data end |
#source ⇒ Object
Virtual attributes
30 31 32 |
# File 'lib/architect4r/model/relationship.rb', line 30 def source @source end |
Class Method Details
.find(id) ⇒ Object
132 133 134 135 136 |
# File 'lib/architect4r/model/relationship.rb', line 132 def self.find(id) data = connection.execute_cypher("start r=relationship(#{id}) return r") data &&= data['data'] && data['data'].flatten.first self.build_from_database(data) end |
.find!(id) ⇒ Object
138 139 140 |
# File 'lib/architect4r/model/relationship.rb', line 138 def self.find!(id) self.find(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!")) end |
.inherited(subklass) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/architect4r/model/relationship.rb', line 13 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 # Validations validates :source, :presence => true validates :destination, :presence => true end 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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/architect4r/model/relationship.rb', line 55 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_relationship(self.source.id, self.destination.id, self.class.name, self._to_database_hash) self.raw_data = result end # if something goes wrong we receive a nil value and return false !result.nil? end end end |
#destroy ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/architect4r/model/relationship.rb', line 93 def destroy run_callbacks :destroy do if result = connection.delete_relationship(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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/architect4r/model/relationship.rb', line 74 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_relationship(self.id, self._to_database_hash) # if something goes wrong we receive a nil value and return false !result.nil? end end end |