Sha256: 882d051265e982dd00fb1280b6c78a14dc2dd3b26c4973df32eed2a3e484a523

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Genio
  module Parser
    module Types
      class Base < Hash
        def initialize(options = {})
          options.each do |name, value|
            self[name] = value
          end
        end

        def merge(hash)
          dup.merge!(hash)
        end

        def merge!(hash)
          hash.each{|name, value| self[name] = value }
          self
        end

        def [](name)
          name = name.to_s if name.is_a?(Symbol)
          super(name)
        end

        def []=(name, value)
          name = name.to_s if name.is_a?(Symbol)
          super(name, convert(value))
        end

        def convert(value)
          value = Base.new(value) if !value.is_a? Base and value.is_a? Hash
          value
        end

        def method_missing(name, *values)
          if values.size == 0
            self[name]
          elsif values.size == 1 and name =~ /^(.*)=$/
            self[$1] = values.first
          else
            super
          end
        end

        class << self
          def attr_accessor(name)
            define_method name do
              self[name]
            end
            define_method "#{name}=" do |value|
              self[name] = value
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
genio-parser-0.0.1 lib/genio/parser/types/base.rb