Sha256: 676d75657ecd9a6cb6cb046244cef96fbece8ccbedb0b0aa0606c9a3521d4a1e

Contents?: true

Size: 838 Bytes

Versions: 3

Compression:

Stored size: 838 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: Is there a better name for `data` --perhaps `open`?
  #
  # TODO: Is this method really worth having?
  #
  # Returns [Functor].

  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

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/hash/data.rb
facets-3.1.0 lib/core/facets/hash/data.rb
facets-3.0.0 lib/core/facets/hash/data.rb