Module: MetaHelper

Included in:
DSLCompanion::Features::Basic
Defined in:
lib/dsl_companion/meta_helper.rb

Instance Method Summary collapse

Instance Method Details

#class_def(name, &block) ⇒ Object

Defines an instance method within a class



19
20
21
# File 'lib/dsl_companion/meta_helper.rb', line 19

def class_def( name, &block )
  class_eval { define_method name, &block }
end

#inject_variable(name, value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dsl_companion/meta_helper.rb', line 23

def inject_variable(name, value)
  # Inject instance variable in the current context
  injected_accessor = name.to_s.to_sym
  injected_instance_variable = "@#{injected_accessor}"
  already_defined = self.instance_variable_defined? injected_instance_variable
  logger("DSL Interpreter overriding existing variable '#{injected_instance_variable}'", :warn) if already_defined
  self.instance_variable_set injected_instance_variable, value

  # Defines the method that returns the instance variable and inject into the interpreter's context
  meta_def "#{injected_accessor}" do
    self.instance_variable_get injected_instance_variable
  end

end

#meta_def(name, &block) ⇒ Object

Adds method to metaclass



14
15
16
# File 'lib/dsl_companion/meta_helper.rb', line 14

def meta_def( name, &block )
  meta_eval { define_method name, &block }
end

#meta_eval(&block) ⇒ Object



9
10
11
# File 'lib/dsl_companion/meta_helper.rb', line 9

def meta_eval &block
  metaclass.instance_eval &block
end

#metaclassObject



3
4
5
6
7
# File 'lib/dsl_companion/meta_helper.rb', line 3

def metaclass
  class << self
    self
  end
end