Sha256: ed01e556716eaf3971640e092bd8fffc4cbb7248648186288b4c64e135475f25

Contents?: true

Size: 486 Bytes

Versions: 11

Compression:

Stored size: 486 Bytes

Contents

class Hash
  class Builder
    
    def initialize
      @hash = {}
      @scopes = []
    end

    def build
      yield self
      @hash
    end

    def method_missing(method, *args, &block)
      if block
        @scopes.push method
        block.call
        @scopes.pop
      else
        current_scope = @scopes.inject(@hash) { |h,s| h[s] ||= {} }
        current_scope[method] = args.first
      end
    end

    def self.build(&block)
      new.build(&block)
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hash_ext-0.6.0 lib/hash_ext/builder.rb
hash_ext-0.5.0 lib/hash_ext/builder.rb
hash_ext-0.4.0 lib/hash_ext/builder.rb
hash_ext-0.3.1 lib/hash_ext/builder.rb
hash_ext-0.3.0 lib/hash_ext/builder.rb
hash_ext-0.2.3 lib/hash_ext/builder.rb
hash_ext-0.2.2 lib/hash_ext/builder.rb
hash_ext-0.2.1 lib/hash_ext/builder.rb
hash_ext-0.2.0 lib/hash_ext/builder.rb
hash_ext-0.1.1 lib/hash_ext/builder.rb
hash_ext-0.1.0 lib/hash_ext/builder.rb