Method: Class#constructor
- Defined in:
- lib/constructor.rb
#constructor(*attrs, &block) ⇒ Object
Defines an initialize method for the class that expects a hash of named arguments
class Horse
construct :name, :breed, :weight
end
Horse.new :name => 'Ed', :breed => 'Mustang', :weight => 342
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/constructor.rb', line 23 def constructor(*attrs, &block) if superclass.constructor_keys.any? similar_keys = superclass.constructor_keys & attrs raise "Base class already has keys #{similar_keys.inspect}" unless similar_keys.empty? end # Look for embedded options in the listing: opts = attrs.find { |a| a.kind_of?(Hash) and attrs.delete(a) } do_accessors = opts.nil? ? false : opts[:accessors] == true do_readers = opts.nil? ? false : opts[:readers] == true _constructor_config[:require_args] = opts.nil? ? true : opts[:strict] != false _constructor_config[:super_args] = opts.nil? ? nil : opts[:super] if block_given? _constructor_config[:constructor_block] = block end attr_accessor(*attrs) if do_accessors attr_reader(*attrs) if do_readers # Remember all constructor keys @_ctor_keys = [attrs,superclass.constructor_keys].flatten # Build the new initialize method include Constructor end |