The Resource class models a table of project releated URIs. Each entry has a name and URI. The class is Enumerable so each entry can be iterated over, much like a hash.
The class also recognizes common entry names and aliases, which can be accessed via method calls.
How aliases work in this class is unique. When a recognized name is assigned an URI, all it’s aliases are assigned the URI as well. Therefore when iterating over the entries there will be duplicate URIs under the various names.
Resources.new(:home=>'http://foo.com').to_h #=> {:home=>'http://foo.com', :homepage=>'http://foo.com'}
Special accessors disperse access over multiple hash entries.
# File lib/pom/resources.rb, line 26 def self.attr_accessor(*names) code = [] names.each do |name| code << "def #{name}" code << " @table['#{name}']" code << "end" code << "def #{name}=(val)" names.each do |n| code << " @table['#{n}'] = val" end code << "end" end module_eval code.join("\n") end
New Resources object. The initializer can take a hash of name to URI settings.
# File lib/pom/resources.rb, line 43 def initialize(table={}) @table = {} table.each{ |k,v| __send__("#{k}=", v) } end
Iterate over each enty, including aliases.
# File lib/pom/resources.rb, line 101 def each(&block) @table.each(&block) end
If a method is missing and it is a setter method (ending in ‘=’) then a new entry by that name will be added to the table. If a plain method then the name will be looked for in the table.
# File lib/pom/resources.rb, line 114 def method_missing(sym, *args) meth = sym.to_s name = meth.chomp('=') case meth when /=$/ @table[name] = args.first else super(sym, *args) if block_given? or args.size > 0 nil end end
Generated with the Darkfish Rdoc Generator 2.