Sha256: a7ad57df309fec01bc66c0409bf582b3e3a56219e4074406eb90e7ebf8f424fa

Contents?: true

Size: 1.79 KB

Versions: 17

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8

require 'y_support/core_ext/class'

class Object
  # Assigns prescribed atrributes to the object and makes them accessible with
  # getter (reader) methods. Raises NameError should any of the getters shadow /
  # overwrite existing methods.
  # 
  def set_attr_with_readers hash
    hash.each_pair { |ß, value|
      fail NameError, "Method \##{ß} already defined!" if methods.include? ß
      set_attr_with_readers! ß => value
    }
  end

  # Assigns prescribed atrributes to the object and makes them accessible with
  # getter (reader) methods. Shadows / overwrites existing methods.
  # 
  def set_attr_with_readers! hash
    hash.each_pair { |symbol, value|
      instance_variable_set "@#{symbol}", value
      singleton_class.class_exec { attr_reader symbol }
    }
  end

  # Constructs parametrized subclasses of the supplied classes and makes them
  # available under specified getters. Expects a hash of pairs { reader_symbol:
  # class }, and a hash of parameters, with which the class(es) is (are)
  # parametrized. Raises NameError should any of the getters shadow / overwrite
  # existing methods.
  # 
  def param_class hash, with: (fail ArgumentError, "No parameters!")
    hash.each { |ß, ç|
      sub = ç.parametrize with
      set_attr_with_readers( ß => sub )
    }
    return nil
  end

  # Constructs parametrized subclasses of the supplied classes and makes them
  # available under specified getters. Expects a hash of pairs { reader_symbol:
  # class }, and a hash of parameters, with which the class(es) is (are)
  # parametrized. Shadows / overwrites existing methods.
  # 
  def param_class! hash, with: (fail ArgumentError, "No parameters!")
    hash.each { |ß, ç|
      sub = ç.parametrize with
      set_attr_with_readers!( ß => sub )
    }
    return nil
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
y_support-2.1.5 lib/y_support/core_ext/object/misc.rb
y_support-2.1.4 lib/y_support/core_ext/object/misc.rb
y_support-2.1.3 lib/y_support/core_ext/object/misc.rb
y_support-2.1.2 lib/y_support/core_ext/object/misc.rb
y_support-2.1.1 lib/y_support/core_ext/object/misc.rb
y_support-2.1.0 lib/y_support/core_ext/object/misc.rb
y_support-2.0.43 lib/y_support/core_ext/object/misc.rb
y_support-2.0.42 lib/y_support/core_ext/object/misc.rb
y_support-2.0.41 lib/y_support/core_ext/object/misc.rb
y_support-2.0.39 lib/y_support/core_ext/object/misc.rb
y_support-2.0.38 lib/y_support/core_ext/object/misc.rb
y_support-2.0.37 lib/y_support/core_ext/object/misc.rb
y_support-2.0.36 lib/y_support/core_ext/object/misc.rb
y_support-2.0.35 lib/y_support/core_ext/object/misc.rb
y_support-2.0.34 lib/y_support/core_ext/object/misc.rb
y_support-2.0.33 lib/y_support/core_ext/object/misc.rb
y_support-2.0.31 lib/y_support/core_ext/object/misc.rb