Skip to content

Commit

Permalink
Allow include_vocabulary to take >1 vocabularies
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencewoodman committed Jul 7, 2011
1 parent ce69752 commit f91c4fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
12 changes: 7 additions & 5 deletions lib/mida/vocabulary.rb
Expand Up @@ -47,13 +47,15 @@ def self.included_vocabularies
@included_vocabularies ||= Set.new
end

# Include the properties from the specified vocabulary.
# Include the properties from the specified <tt>vocabularies</tt>.
# This is the correct way to inherit properties from another vocabulary,
# rather than subclassing.
def self.include_vocabulary(vocabulary)
included_vocabularies.merge(vocabulary.included_vocabularies)
included_vocabularies << vocabulary
properties.merge!(vocabulary.properties)
def self.include_vocabulary(*vocabularies)
vocabularies.each do |vocabulary|
included_vocabularies.merge(vocabulary.included_vocabularies)
included_vocabularies << vocabulary
properties.merge!(vocabulary.properties)
end
end

# Sets the regular expression to match against the +itemtype+
Expand Down
16 changes: 11 additions & 5 deletions spec/vocabulary_spec.rb
Expand Up @@ -165,10 +165,16 @@ class Product < Mida::Vocabulary
has_many 'addons'
end

class Vehicle < Mida::Vocabulary
itemtype %r{http://example\.com.*?thing$}i
include_vocabulary Thing
has_one 'colour'
end

class Car < Mida::Vocabulary
include_vocabulary Product
include_vocabulary Product, Vehicle
itemtype %r{http://example\.com.*?car$}i
has_one 'colour', 'engine'
has_one 'engine'
has_many 'stickers'
end
end
Expand All @@ -178,19 +184,19 @@ class Car < Mida::Vocabulary
end

it "should contain included vocabularies' properties" do
['description', 'make','model'].each do
['description', 'make','model', 'colour'].each do
|prop| Car.properties[prop][:num].should == :one
end
Car.properties['addons'][:num].should == :many
end

it "should contain new properties" do
['colour','engine'].each {|prop| Car.properties[prop][:num].should == :one}
Car.properties['engine'][:num].should == :one
Car.properties['stickers'][:num].should == :many
end

it '#included_vocabularies should return the included vocabularies' do
[Thing, Product].each do |vocab|
[Thing, Product, Vehicle].each do |vocab|
Car.included_vocabularies.should include(vocab)
end
end
Expand Down

0 comments on commit f91c4fb

Please sign in to comment.