Created
October 17, 2011 15:30
-
-
Save nmk/1292868 to your computer and use it in GitHub Desktop.
Polymorphic embedding in a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "mongoid" | |
require "minitest/autorun" | |
Mongoid.configure do |config| | |
config.master = Mongo::Connection.new.db('test_polymorphic_embedding') | |
end | |
class Condition; end | |
# A relation consists of a name and a list of arguments | |
# | |
# e.g. agent(e, x) | |
# The name is `agent' and the arguments are `e' and `x' | |
# | |
# Relations are one kind of condition. | |
class Relation < Condition | |
include Mongoid::Document | |
field :name | |
# need to change this, so that a relation can also be stored | |
# in the arguments array | |
embeds_many :arguments, class_name: 'StringArgument' | |
end | |
class StringArgument | |
include Mongoid::Document | |
field :name | |
end | |
class RelationModellingTest < MiniTest::Unit::TestCase | |
# agent(e, x) | |
def test_build_a_relation_with_string_arguments_from_a_hash | |
input = { '_type' => 'Relation', | |
'name' => 'agent', | |
'arguments' => [ | |
{ '_type' => 'StringArgument', | |
'name' => 'e' }, | |
{ '_type' => 'StringArgument', | |
'name' => 'x' } | |
] } | |
relation = Relation.new(input) | |
assert_kind_of StringArgument, relation.arguments.first | |
assert_kind_of StringArgument, relation.arguments.last | |
end | |
# copy(direction(x), direction(y)) | |
def test_create_a_nested_relation | |
first_arg = { '_type' => 'Relation', | |
'name' => 'direction', | |
'arguments' => [ | |
{ '_type' => 'StringArgument', | |
'name' => 'x' } | |
] } | |
second_arg = { '_type' => 'Relation', | |
'name' => 'direction', | |
'arguments' => [ | |
{ '_type' => 'StringArgument', | |
'name' => 'x' } | |
] } | |
input = { '_type' => 'Relation', | |
'name' => 'copy', | |
'arguments' => [first_arg, second_arg] } | |
relation = Relation.new(input) | |
assert_kind_of Relation, relation.arguments.first | |
assert_kind_of Relation, relation.arguments.last | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good,thank you