Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented `foreign_keys` in Model and BelongsTo association

  • Loading branch information...
commit 78612f32078a1fe96d70f2bf8193d12a64ca9403 1 parent dd5c135
@serebryakov serebryakov authored
Showing with 22 additions and 0 deletions.
  1. +6 −0 lib/lib/associations/belongs_to.rb
  2. +16 −0 lib/lib/model/base.rb
View
6 lib/lib/associations/belongs_to.rb
@@ -43,6 +43,12 @@ def replace(value)
end
end
+ ##
+ # Returns the 'foreign key' (i.e. association URI).
+ ##
+ def keys
+ @key || value.id
+ end
private
View
16 lib/lib/model/base.rb
@@ -65,6 +65,8 @@ def namespace(value = nil, options = {})
#
# Person.ns #=> 'example'
# Person.to_xml #=> '<my:Person></my:Person>'
+ #
+ # @return [String]
##
def ns
@ns.to_s || 'myrdf'
@@ -510,6 +512,20 @@ def properties(*args)
end
##
+ # Returns a hash of all foreign keys (i.e. URIs) for belongs_to
+ # type of associations.
+ #
+ # @return [Hash] all foreign keys of an instance (name => uri)
+ ##
+ def foreign_keys
+ Hash[self.class.associations.select do |name, assoc|
+ assoc.belongs_to?
+ end.map do |name, assoc|
+ [ name, @attributes[name].keys ]
+ end]
+ end
+
+ ##
# Returns a hash of all the attributes with their names as keys and
# the attributes' values as values.
#
Please sign in to comment.
Something went wrong with that request. Please try again.