Module: EG

Defined in:
lib/eg.rb,
lib/eg/version.rb

Overview

EG object creation functionality

Constant Summary collapse

RE_CONST =
/^[A-Z]/.freeze
RE_ATTR =
/^@(.+)$/.freeze
VERSION =
'1.0'

Class Method Summary collapse

Class Method Details

.call(hash) ⇒ Module

Creates an object from its prototype hash

Parameters:

  • hash (Hash)

    prototype hash

Returns:

  • (Module)

    created object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eg.rb', line 15

def self.call(hash)
  Module.new.tap do |m|
    s = m.singleton_class
    hash.each do |k, v|
      if k =~ RE_CONST
        m.const_set(k, v)
      elsif k =~ RE_ATTR
        m.instance_variable_set(k, v)
      elsif v.respond_to?(:to_proc)
        s.define_method(k) { |*args| instance_exec(*args, &v) }
      else
        s.define_method(k) { v }
      end
    end
  end
end

.to_procObject



32
33
34
# File 'lib/eg.rb', line 32

def self.to_proc
  ->(hash) { EG.call(hash) }
end