Module: OOP::Tools::ClassMethods

Defined in:
lib/oop/tools.rb

Instance Method Summary collapse

Instance Method Details

#konstructor(*parts, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/oop/tools.rb', line 12

def konstructor(*parts, &block)
  parts = parts.first if parts.first.is_a?(Hash)
  define_method(:initialize) do |opts|
    if block
      opts = block.call(opts) || raise("#{self} invalid with #{opts}")
    end

    @opts = {}
    opts.each do |k, v|
      k = k.to_sym
      if self.is_a? Value
        @opts[_rules.cast_to_value(k)] = _rules.cast_to_value(v)
      else
        @opts[k] = v
      end
    end
    @opts
  end
  parts.each do |part, defaultt|
    part = part.to_sym
    define_method(part) do
      @opts[part] ||= defaultt
    end
  end
end

#message(meth, &block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/oop/tools.rb', line 4

def message(meth, &block)
  define_method(meth) do |*args, &blk|
    args = args.map do |arg|
      _rules.cast_to_value(arg)
    end
    _rules.cast_to_value(self.instance_exec(*(args + [blk]), &block))
  end
end