Module: MetaMethods

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

Constant Summary collapse

VERSION =
"1.0.3"

Instance Method Summary collapse

Instance Method Details

#define_attribute(type, object, key, value, create_instance_variable = true) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/meta_methods/meta_methods.rb', line 14

def define_attribute(type, object, key, value, create_instance_variable=true)
  if create_instance_variable
    metaclass(object).send type, key.to_sym

    object.instance_variable_set("@#{key}".to_sym, value)
  else
    object.class.class_eval <<-CODE
      def #{key}
        "#{value}"
      end
    CODE
  end
end

#define_attributes(type, object, hash, create_instance_variable = true) ⇒ Object



8
9
10
11
12
# File 'lib/meta_methods/meta_methods.rb', line 8

def define_attributes(type, object, hash, create_instance_variable=true)
  hash.each_pair do |key, value|
    define_attribute(type, object, key, value, create_instance_variable)
  end
end

#evaluate_dsl(create_block, destroy_block, execute_block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/meta_methods/meta_methods.rb', line 36

def evaluate_dsl(create_block, destroy_block, execute_block)
  object = nil

  begin
    object = create_block.kind_of?(Proc) ? create_block.call : create_block
    object.instance_variable_set(:@execute_block, execute_block)

    def object.method_missing(sym, *args, &block)
      block_binding = @execute_block.binding
      caller = block_binding.eval 'self'

      caller.send sym, *args, &block
    end

    object.instance_eval(&execute_block)
  ensure
    destroy_block.call(object) if destroy_block && object
  end
end

#locals_to_hash(object, content) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/meta_methods/meta_methods.rb', line 28

def locals_to_hash object, content
  scope = object.instance_eval { binding }

  eval(content, scope)

  extract_values(defined_vars(scope), scope)
end

#metaclass(object) ⇒ Object



2
3
4
5
6
# File 'lib/meta_methods/meta_methods.rb', line 2

def metaclass object
  class << object
    self
  end
end