Class: Estructural

Inherits:
Object
  • Object
show all
Defined in:
lib/estructural.rb

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.new(*attributes, **defaults, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/estructural.rb', line 4

def self.new(*attributes, **defaults, &block)
  Class.new do
    (attributes + defaults.keys).each do |attr|
      define_method(attr) { instance_variable_get("@#{attr}") }
      define_method("#{attr}=") {|value| instance_variable_set("@#{attr}", value) }
    end

    define_method(:initialize) do |args = {}|
      defaults.each { |attribute, value| send("#{attribute}=", value.dup) }
      args.each { |attribute, value| send "#{attribute}=", value }
    end

    class_eval(&block) if block
  end
end