Module: AutomationObject::Reflection

Included in:
Composite
Defined in:
lib/automation_object/helpers/reflection.rb

Overview

Helper module for adding attributes/alias to classes dynamically Reflection/MetaProgramming

Instance Method Summary collapse

Instance Method Details

#add_alias(alias_name, attribute_name) ⇒ Object

Parameters:

  • alias_name (Symbol, String)

    name of alias to add

  • attribute_name (Symbol, String)

    attribute to link to



21
22
23
24
25
26
27
28
# File 'lib/automation_object/helpers/reflection.rb', line 21

def add_alias(alias_name, attribute_name)
  singleton_class = class << self
    self
  end
  singleton_class.send(:define_method, alias_name) do
    instance_variable_get("@#{attribute_name}")
  end
end

#add_attribute(name, value) ⇒ Object

Parameters:

  • name (String, Symbol)

    name of attribute to add

  • value (Object)

    value of attribute



9
10
11
12
13
14
15
16
17
# File 'lib/automation_object/helpers/reflection.rb', line 9

def add_attribute(name, value)
  name = name.to_s
  alias_name = name + '?' if name.gsub!(/\?$/, '')

  self.class.send(:attr_accessor, name) unless self.class.method_defined? name
  instance_variable_set("@#{name}", value)

  add_alias(alias_name, name) if alias_name
end