class BlockIsHash

Public Class Methods

new(repeats, &block) click to toggle source
# File lib/block-is-hash/class.rb, line 2
def initialize repeats, &block
  @repeats = repeats
  @hash = {}
  instance_eval(&block)
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/block-is-hash/class.rb, line 8
def method_missing name, *args, &block
  if block
    args.push BlockIsHash.new(@repeats, &block).to_hash
  end

  value = args.length == 1 ? args[0] : args

  if @repeats.include? name and @hash.include? name
    @hash[name].push value
  elsif @repeats.include? name
    @hash[name] = [value]
  elsif @hash.include? name
    raise "#{name} entry is declared twice."
  else
    @hash[name] = value
  end
end
to_hash() click to toggle source
# File lib/block-is-hash/class.rb, line 26
def to_hash
  @hash
end