Sha256: 10e0f03854455fbc56970ae77c58ec208ad545f663d65e1b631b525b03d13697

Contents?: true

Size: 543 Bytes

Versions: 16

Compression:

Stored size: 543 Bytes

Contents

class Hash

  # Constructs a Proc object from a hash such
  # that the parameter of the Proc is assigned
  # the hash keys as attributes.
  #
  #   h = { :a => 1 }
  #   p = h.to_proc
  #   o = OpenStruct.new
  #   p.call(o)
  #   o.a  #=> 1
  #

  def to_proc
    lambda do |o|
      self.each do |k,v|
        ke = "#{k}="
        o.__send__(ke, v)
      end
    end
  end

  def to_proc_with_reponse
    lambda do |o|
      self.each do |k,v|
        ke = "#{k}="
        o.__send__(ke, v) if respond_to?(ke)
      end
    end
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
facets-1.4.0 lib/facets/core/hash/to_proc.rb
facets-1.4.2 lib/facets/core/hash/to_proc.rb
facets-1.4.1 lib/facets/core/hash/to_proc.rb
facets-1.4.3 lib/facets/core/hash/to_proc.rb
facets-1.4.5 lib/facets/core/hash/to_proc.rb
facets-1.4.4 lib/facets/core/hash/to_proc.rb
facets-1.7.30 lib/facets/core/hash/to_proc.rb
facets-1.7.38 lib/facets/core/hash/to_proc.rb
facets-1.7.0 lib/facets/core/hash/to_proc.rb
facets-1.7.46 lib/facets/core/hash/to_proc.rb
facets-1.8.0 lib/facets/core/hash/to_proc.rb
facets-1.8.49 lib/facets/core/hash/to_proc.rb
facets-1.8.51 lib/facets/core/hash/to_proc.rb
facets-1.8.20 lib/facets/core/hash/to_proc.rb
facets-1.8.8 lib/facets/core/hash/to_proc.rb
facets-1.8.54 lib/facets/core/hash/to_proc.rb