Sha256: c7717d71f23c9f8db68fa319955ce7ebb4cc427f4f42fa3cb1b06f90aa9c4a4f

Contents?: true

Size: 1.07 KB

Versions: 25

Compression:

Stored size: 1.07 KB

Contents

module Compony
  # @api description
  # This class is intended for configs with predefined interfaces and should be used with instances of Hash:<br>
  # Example:<br>
  # ```ruby
  # instance_of_a_hash = Compony::MethodAccessibleHash.new
  # instance_of_a_hash.merge!({ foo: :bar })
  # instance_of_a_hash.foo --> :bar
  # instance_of_a_hash.roo --> nil
  # ```
  # See: https://gist.github.com/kalsan/87826048ea0ade92ab1be93c0919b405
  class MethodAccessibleHash < Hash
    # Takes an optional hash as argument and constructs a new
    # MethodAccessibleHash.
    def initialize(hash = {})
      super()

      hash.each do |key, value|
        self[key.to_sym] = value
      end
    end

    # @private
    def merge(hash)
      super(hash.symbolize_keys)
    end

    # @private
    def method_missing(method, *args, &)
      if method.end_with?('=')
        name = method.to_s.gsub(/=$/, '')
        self[name.to_sym] = args.first
      else
        self[method.to_sym]
      end
    end

    # @private
    def respond_to_missing?(_method, _include_private = false)
      true
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
compony-0.5.3 lib/compony/method_accessible_hash.rb
compony-0.5.2 lib/compony/method_accessible_hash.rb
compony-0.5.1 lib/compony/method_accessible_hash.rb
compony-0.5.0 lib/compony/method_accessible_hash.rb
compony-0.4.1 lib/compony/method_accessible_hash.rb
compony-0.4.0 lib/compony/method_accessible_hash.rb
compony-0.3.3 lib/compony/method_accessible_hash.rb
compony-0.3.2 lib/compony/method_accessible_hash.rb
compony-0.3.1 lib/compony/method_accessible_hash.rb
compony-0.3.0 lib/compony/method_accessible_hash.rb
compony-0.2.3 lib/compony/method_accessible_hash.rb
compony-0.2.2 lib/compony/method_accessible_hash.rb
compony-0.2.1 lib/compony/method_accessible_hash.rb
compony-0.2.0 lib/compony/method_accessible_hash.rb
compony-0.1.1 lib/compony/method_accessible_hash.rb
compony-0.1.0 lib/compony/method_accessible_hash.rb
compony-0.0.9 lib/compony/method_accessible_hash.rb
compony-0.0.8 lib/compony/method_accessible_hash.rb
compony-0.0.7 lib/compony/method_accessible_hash.rb
compony-0.0.6 lib/compony/method_accessible_hash.rb