Sha256: e38a930d465f2dde606a40d64b226ea8663b8db684f6f6c6de823990994cfff1
Contents?: true
Size: 1.61 KB
Versions: 4
Compression:
Stored size: 1.61 KB
Contents
require 'path_string' require 'core_extensions' require 'matcher' class HashDealer attr_accessor :parent # cattr_accessor def self.hashes @hashes ||= {} end # define a method of the request factory def self.define(name, opts = {}, &block) self.hashes[name] = self.new(opts, &block) end def self.roll(name, *args) raise Exception.new("No HashDealer called #{name}") unless self.hashes[name] self.hashes[name].attributes(*args) end # initializer just calls the block from within our DSL def initialize(opts = {}, &block) @parent = opts[:parent] instance_eval(&block) end # set the value as the root element for attributes def root(value) @attributes = value end # get the stored attributes for this HashDealer def attributes(*args) # allows us to set a root value return @attributes unless @attributes.is_a?(Hash) att = @parent ? HashDealer.roll(@parent.to_sym) : Hash.new @attributes.each do |k,v| att[k] = v.is_a?(Proc) ? v.call(*args) : v end # if we have a hash as the first arg, it would override the attributes if args.first.is_a?(Hash) args.first.each_pair do |k,v| att[k.to_sym] = v if att.has_key?(k.to_sym) end end att end protected # method missing def method_missing(meth, *args, &block) raise Exception.new("Please provide either a String or a block to #{meth}") unless (args.length == 1 || (args.empty? && block_given?)) @attributes ||= Hash.new if block_given? @attributes[meth.to_sym] = block else @attributes[meth.to_sym] = args.first end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
hash_dealer-1.2.1 | lib/hash_dealer.rb |
hash_dealer-1.2.0 | lib/hash_dealer.rb |
hash_dealer-1.1.3 | lib/hash_dealer.rb |
hash_dealer-1.1.2 | lib/hash_dealer.rb |