lib/mida/vocabulary.rb in mida-0.3.1 vs lib/mida/vocabulary.rb in mida-0.3.2

- old
+ new

@@ -6,19 +6,15 @@ # To specify a vocabulary use the following methods: # +itemtype+, +has_one+, +has_many+ 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 def self.register(vocabulary) @vocabularies << vocabulary @@ -39,17 +35,43 @@ 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 <tt>vocabularies</tt>. + # This is the correct way to inherit properties from another vocabulary, + # rather than subclassing. + def self.include_vocabulary(*vocabularies) + vocabularies.each do |vocabulary| + included_vocabularies.merge(vocabulary.included_vocabularies) + included_vocabularies << vocabulary + properties.merge!(vocabulary.properties) + end + end + + # As per the standard <tt>kind_of?</tt>, but also checks to see if vocabulary has + # been included by self + def self.kind_of?(vocabulary) + return true if self == vocabulary + return true if self.ancestors.include?(vocabulary) + included_vocabularies.include?(vocabulary) + end + # Sets the regular expression to match against the +itemtype+ # or returns the current regular expression - def self.itemtype(regexp_arg=nil) - if regexp_arg - @itemtype = regexp_arg - else - @itemtype - end + def self.itemtype(regexp=nil) + return @itemtype unless regexp + @itemtype = regexp end # Defines the properties as only containing one value # If want to say any property name, then use +:any+ as a name