Skip to content

Commit

Permalink
Merge pull request maxdemarzi#22 from blizkreeg/models
Browse files Browse the repository at this point in the history
added methods to get the outgoing/incoming aliases of a relationship
  • Loading branch information
kamranjon committed Aug 23, 2013
2 parents 5bf591a + 25227c1 commit 06d4a42
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/deja/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def define_alias_methods(rel, aliases)
instance_variable_get("@#{rel}")
end

define_method "#{aliases[:out_plural]}=" do |relationship|
current_rel = instance_variable_get("@#{rel}") || []
current_rel << [relationship.end_node, relationship]
instance_variable_set("@#{rel}", current_rel)
end

alias_method "#{aliases[:out_plural]}<<", "#{aliases[:out_plural]}="

define_method aliases[:out_singular] do |&b|
relation = send(aliases[:out_plural]).first
b.call(relation[0], relation[1]) if b
Expand All @@ -56,6 +64,14 @@ def define_alias_methods(rel, aliases)
instance_variable_get("@#{rel}")
end

define_method "#{aliases[:in_plural]}=" do |relationship|
current_rel = instance_variable_get("@#{rel}") || []
current_rel << [relationship.start_node, relationship]
instance_variable_set("@#{rel}", current_rel)
end

alias_method "#{aliases[:in_plural]}<<", "#{aliases[:in_plural]}="

define_method aliases[:in_singular] do |&b|
relation = send(aliases[:in_plural]).first
b.call(relation[0], relation[1]) if b
Expand All @@ -82,6 +98,16 @@ def relationships
end
end

def outgoing_rel(type, cardinality="plural")
selector = ("out_" + cardinality).to_sym
self.class.relationship_names[type.underscore.to_sym][selector]
end

def incoming_rel(type, cardinality="plural")
selector = ("in_" + cardinality).to_sym
self.class.relationship_names[type.underscore.to_sym][selector]
end

def count_relationships(type = :all)
if type == :all
Deja::Query.count_relationships(@id)
Expand Down Expand Up @@ -129,4 +155,3 @@ def persisted_attributes
end
end
end

0 comments on commit 06d4a42

Please sign in to comment.