Module: Callable

Overview

def call

  do some stuff in here
end

end

Instance Method Summary collapse

Instance Method Details

#initialize(kwargs) ⇒ Object

OVERRIDING VALUES IN INITIALIZE

If you want to override values in initialize then you can put this in your calling class and it will let you transform one of the values:

def initialize(args)

super

@quantity = args[:quantity].to_i

end

Its not super clean, but there might be times it needs to be done.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/callable.rb', line 109

def initialize(kwargs)
  # Set each of them as an instance var, i.e.
  # @something = something
  readable_attributes = self.class.readable_attributes(kwargs)

  readable_attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end

  @context = Hashie::Mash.new(kwargs)

  # Then we set them as attr_readers so we can call them
  # i.e. something
  class_eval do
    private

    attr_reader(*readable_attributes.keys.map(&:to_sym))
    attr_reader(:context)
  end
end