Module: DSLCompanion::MetaHelper

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inject_variable(target, name, value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dsl_companion/meta_helper.rb', line 28

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

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

Instance Method Details

#class_def(name, &block) ⇒ Object

Defines an instance method within a class



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

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

#inject_variable(name, value) ⇒ Object



24
25
26
# File 'lib/dsl_companion/meta_helper.rb', line 24

def inject_variable(name, value)
  MetaHelper.inject_variable self, name, value
end

#meta_def(name, &block) ⇒ Object

Adds method to metaclass



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

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

#meta_eval(&block) ⇒ Object



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

def meta_eval(&block)
  metaclass.instance_eval &block
end

#metaclassObject



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

def metaclass
  class << self
    self
  end
end