Skip to content

Commit

Permalink
Spec #attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Oct 1, 2012
1 parent 4bf316e commit bd65e78
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
18 changes: 12 additions & 6 deletions lib/neography/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ module Neography
module Property

def [](key)
key = key.to_sym
return unless respond_to?(key)
@table[key]
end

def []=(key, value)
k = key.to_s
key = key.to_sym
k_str = key.to_s
if value.nil?
if self.is_a? Neography::Node
neo_server.remove_node_properties(self.neo_id, [k])
neo_server.remove_node_properties(self.neo_id, [k_str])
else
neo_server.remove_relationship_properties(self.neo_id, [k])
neo_server.remove_relationship_properties(self.neo_id, [k_str])
end
else
if self.is_a? Neography::Node
neo_server.set_node_properties(self.neo_id, {k => value})
neo_server.set_node_properties(self.neo_id, {k_str => value})
else
neo_server.set_relationship_properties(self.neo_id, {k => value})
neo_server.set_relationship_properties(self.neo_id, {k_str => value})
end
new_ostruct_member(k) unless self.respond_to?(key)
new_ostruct_member(key) unless self.respond_to?(key)
end
@table[key] = value
end
Expand Down Expand Up @@ -50,5 +52,9 @@ def method_missing(method_sym, *arguments, &block)
end
end

def attributes
@table.keys
end

end
end
5 changes: 0 additions & 5 deletions lib/neography/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ def exist?
end
end

def attributes
attrs = self.methods - OpenStruct.instance_methods - Neography::Relationship.instance_methods
attrs.values_at(*attrs.each_index.select {|i| i.even?})
end

def other_node(node)
if node == @start_node
@end_node
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ module Neography
node.key = "value2"
end

it "knows its attributes" do
@db.stub(:"set_node_properties")
node.key = "value"
node["key2"] = "value"
node.attributes.should =~ [ :key, :key2 ]
end

end

context "Relationship" do
Expand Down Expand Up @@ -117,6 +124,13 @@ module Neography
relationship.key = "value2"
end

it "knows its attributes" do
@db.stub(:"set_relationship_properties")
relationship.key = "value"
relationship["key2"] = "value"
relationship.attributes.should =~ [ :key, :key2 ]
end

end
end
end
11 changes: 1 addition & 10 deletions spec/unit/relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,8 @@ module Neography

describe "#other_node" do

let(:rel) do
{
"self" => "/1",
"start" => "/2",
"end" => "/3",
"data" => {}
}
end

before do
db.stub(:create_relationship) { rel }
db.stub(:create_relationship) { relationship_hash }
end

subject(:relationship) { Relationship.create("type", from, to, props) }
Expand Down

0 comments on commit bd65e78

Please sign in to comment.