Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow vocabularies to be included into others

  • Loading branch information...
commit a13be43b889445171367b246a16dd506eb2dc72c 1 parent b25bc8f
@LawrenceWoodman authored
Showing with 68 additions and 4 deletions.
  1. +19 −4 lib/mida/vocabulary.rb
  2. +49 −0 spec/vocabulary_spec.rb
View
23 lib/mida/vocabulary.rb
@@ -8,15 +8,11 @@ module Mida
class Vocabulary
class << self
- # Return the properties specification
- attr_reader :properties
-
# Return the registered vocabularies
attr_reader :vocabularies
end
@vocabularies = Set.new
- @properties = {}
# Register a vocabulary that can be used when parsing,
# later vocabularies are given precedence over earlier ones
@@ -41,6 +37,25 @@ def self.inherited(subclass)
register(subclass)
end
+ # Return the properties specification
+ def self.properties
+ @properties ||= {}
+ end
+
+ # Return the included vocabularies
+ def self.included_vocabularies
+ @included_vocabularies ||= Set.new
+ end
+
+ # Include the properties from the specified vocabulary.
+ # 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)
+ end
+
# Sets the regular expression to match against the +itemtype+
# or returns the current regular expression
def self.itemtype(regexp_arg=nil)
View
49 spec/vocabulary_spec.rb
@@ -126,6 +126,10 @@ class Person < Mida::Vocabulary
Mida::Vocabulary.vocabularies.should include(Person)
end
+ it '#included_vocabularies should be empty' do
+ Person.included_vocabularies.empty?.should be_true
+ end
+
end
describe Mida::Vocabulary, 'when subclassed and has no properties' do
@@ -146,3 +150,48 @@ class Empty < Mida::Vocabulary
end
end
+
+describe Mida::Vocabulary, 'when subclassed and using #include_vocabulary' do
+ before do
+ class Thing < Mida::Vocabulary
+ itemtype %r{http://example\.com.*?thing$}i
+ has_one 'description'
+ end
+
+ class Product < Mida::Vocabulary
+ include_vocabulary Thing
+ itemtype %r{http://example\.com.*?product$}i
+ has_one 'make', 'model'
+ has_many 'addons'
+ end
+
+ class Car < Mida::Vocabulary
+ include_vocabulary Product
+ itemtype %r{http://example\.com.*?car$}i
+ has_one 'colour', 'engine'
+ has_many 'stickers'
+ end
+ end
+
+ it '#itemtype should return the new regexp' do
+ Car.itemtype.should == %r{http://example\.com.*?car$}i
+ end
+
+ it "should contain included vocabularies' properties" do
+ ['description', 'make','model'].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['stickers'][:num].should == :many
+ end
+
+ it '#included_vocabularies should return the included vocabularies' do
+ [Thing, Product].each do |vocab|
+ Car.included_vocabularies.should include(vocab)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.