Module: ParamsReady::Extensions::Freezer

Included in:
Parameter::AbstractDefinition, Parameter::AbstractParameter
Defined in:
lib/params_ready/extensions/freezer.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#freeze_variable(name, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/params_ready/extensions/freezer.rb', line 18

def freeze_variable(name, &block)
  ivar = :"@#{name}"
  if defined? @variables_to_freeze
    @variables_to_freeze << [ivar, block]
  else
    @variables_to_freeze = [[ivar, block]]
    define_method :freeze_variables do
      next if frozen?
      self.class.variables_to_freeze.each do |(ivar, block)|
        variable = instance_variable_get ivar
        next if Extensions::Undefined.value_indefinite?(variable)
        
        block.call(variable) unless block.nil?
        variable.freeze
      end
    end
  end
end

#freeze_variables(*names) ⇒ Object



37
38
39
40
41
# File 'lib/params_ready/extensions/freezer.rb', line 37

def freeze_variables(*names)
  names.each do |name|
    freeze_variable name
  end
end

#variables_to_freezeObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/params_ready/extensions/freezer.rb', line 4

def variables_to_freeze
  # This works on assumption classes
  # are not redefined during runtime
  @cached_variables_to_freeze ||= begin
    names = if defined? @variables_to_freeze
      @variables_to_freeze.dup
    else
      []
    end
    names += superclass.variables_to_freeze if superclass.respond_to? :variables_to_freeze
    names
  end
end