Sha256: cbbbdea03ac985f563801c1e6c0993e81a9866e91b8acc47ba0c4a06d621d869

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class Object
  # Takes a hash and creates (singleton) attr_readers for each key.
  #
  #   require 'facet/object/with_accessor'
  #
  #   with_reader { :x => 1, :y => 2 }
  #   @x       #=> 1
  #   @y       #=> 2
  #   self.x   #=> 1
  #   self.y   #=> 2
  #
  def with_reader(h)
    (class << self ; self ; end).send( :attr_reader, *h.keys )
    h.each { |k,v| instance_variable_set("@#{k}", v) }
  end

  # Takes a hash and creates (singleton) attr_writers for each key.
  #
  #   require 'facet/object/with_accessor'
  #
  #   with_writer { :x => 1, :y => 2 }
  #   @x           #=> 1
  #   @y           #=> 2
  #   self.x = 3
  #   self.y = 4
  #   @x           #=> 3
  #   @y           #=> 4
  #
  def with_writer(h)
    (class << self ; self ; end).send( :attr_writer, *h.keys )
    h.each { |k,v| instance_variable_set("@#{k}", v) }
  end

  # Takes a hash and creates (singleton) attr_accessors for each key .
  #
  #   require 'facet/object/with_accessor'
  #
  #   with_accessor { :x => 1, :y => 2 }
  #   @x           #=> 1
  #   @y           #=> 2
  #   self.x = 3
  #   self.y = 4
  #   self.x       #=> 3
  #   self.y       #=> 4
  #
  def with_accessor(h)
    (class << self ; self ; end).send( :attr_accessor, *h.keys )
    h.each { |k,v| instance_variable_set("@#{k}", v) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/object/with_accessor.rb