diff --git a/lib/deja/node.rb b/lib/deja/node.rb index fa74207..0295ce9 100644 --- a/lib/deja/node.rb +++ b/lib/deja/node.rb @@ -159,17 +159,21 @@ def incoming_rel(type, cardinality="plural") end def create! - run_callbacks :create do - @id = Deja::Query.create_node(persisted_attributes) - raise Deja::Error::OperationFailed, "Failed to create node" unless @id + run_callbacks :save do + run_callbacks :create do + @id = Deja::Query.create_node(persisted_attributes) + raise Deja::Error::OperationFailed, "Failed to create node" unless @id + end end self end def update!(opts = {}) opts.each { |attribute, value| send("#{attribute}=", value) } - run_callbacks :update do - Deja::Query.update_node(@id, persisted_attributes) + run_callbacks :save do + run_callbacks :update do + Deja::Query.update_node(@id, persisted_attributes) + end end self end diff --git a/lib/deja/relationship.rb b/lib/deja/relationship.rb index b5a2361..0b1f786 100644 --- a/lib/deja/relationship.rb +++ b/lib/deja/relationship.rb @@ -79,17 +79,21 @@ def initialize(*args) end def create! - run_callbacks :create do - @id = Deja::Query.create_relationship(@start_node.id, @end_node.id, self.class.label, persisted_attributes) - raise Deja::Error::OperationFailed, "Failed to create relationship" unless @id + run_callbacks :save do + run_callbacks :create do + @id = Deja::Query.create_relationship(@start_node.id, @end_node.id, self.class.label, persisted_attributes) + raise Deja::Error::OperationFailed, "Failed to create relationship" unless @id + end end self end def update!(opts = {}) opts.each { |attribute, value| send("#{attribute}=", value) } - run_callbacks :update do - Deja::Query.update_relationship(@id, persisted_attributes) + run_callbacks :save do + run_callbacks :update do + Deja::Query.update_relationship(@id, persisted_attributes) + end end self end diff --git a/lib/deja/schema_generator.rb b/lib/deja/schema_generator.rb index 8cb0ae5..607cc16 100644 --- a/lib/deja/schema_generator.rb +++ b/lib/deja/schema_generator.rb @@ -21,10 +21,19 @@ def define_class_key def attribute(name, type, opts = {}) self.define_class_key @@all_attributes[self.name][name] = opts.merge(:type => type) - send(:attr_accessor, name) + attr_accessorize(name, opts) add_property_to_index(name) if opts[:index] end + def attr_accessorize(name, opts) + send(:attr_accessor, name) + define_attribute_methods name + define_method("#{name}=") do |new_value| + send("#{name}_will_change!") if (new_value != instance_variable_get("@#{name}") && !instance_variable_get("@#{name}").nil?) + instance_variable_set("@#{name}", new_value) + end + end + def indexed_attributes @@indexed_attributes end