Sha256: ec3abb0bd362daee7f73778e6813058d7b794873efb2df718964d4a77e1f8e43

Contents?: true

Size: 806 Bytes

Versions: 7

Compression:

Stored size: 806 Bytes

Contents

require 'facets/functor'

class Hash

  # Access to a hash as if it were an OpenStruct.
  #
  #   h = {:a=>1, :b=>2}
  #
  #   h.data.a  #=> 1
  #   h.data.b  #=> 2
  #   h.data.c  #=> nil
  #
  #   h.data.c = 3
  #   h.data.c  #=> 3
  #
  #   h.data.a?  #=> true
  #   h.data.d?  #=> false
  #
  # TODO: Change name of Hash#data to something better?
  #
  # TODO: Is this really a method worth having?
  def data
    this = self
    Functor.new do |op, *a|
      case op.to_s
      when /\=$/
        op = op.to_s.chomp('=')
        this[op] = a.first
      when /\?$/
        op = op.to_s.chomp('?')
        this.key?(op.to_s) || this.key?(op.to_sym)
      when /\!$/
        op = op.to_s.chomp('!')
        this[op] # ???
      else
        this[op.to_s] || this[op.to_sym]
      end
    end
  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/hash/data.rb
facets-2.9.2 lib/core/facets/hash/data.rb
facets-2.9.2 src/core/facets/hash/data.rb
facets-2.9.1 lib/core/facets/hash/data.rb
facets-2.9.0 lib/core/facets/hash/data.rb
facets-2.9.0.pre.2 lib/core/facets/hash/data.rb
facets-2.9.0.pre.1 lib/core/facets/hash/data.rb