Class: Modernize::StructContext

Inherits:
Object
  • Object
show all
Defined in:
lib/modernizer.rb

Overview

This class is used to make the context key/values available as instance variables to the map methods.

Instance Method Summary collapse

Constructor Details

#initialize(context, hash) ⇒ StructContext

Returns a new instance of StructContext.



94
95
96
97
98
99
# File 'lib/modernizer.rb', line 94

def initialize(context, hash)
  create_getter(:hash, hash)
  context.each do |key, value|
    create_getter(key, value)
  end
end

Instance Method Details

#create_getter(name, value) ⇒ Object

Creates getters for each instance variable and sets the initial value.



110
111
112
113
114
115
# File 'lib/modernizer.rb', line 110

def create_getter(name, value)
  instance_variable_set(:"@#{name}", value)
  create_method(name.to_sym) do
    instance_variable_get(:"@#{name}")
  end
end

#create_method(name, &block) ⇒ Object

Helper method which wraps define_method.



103
104
105
# File 'lib/modernizer.rb', line 103

def create_method(name, &block)
  self.class.send(:define_method, name, &block)
end