Module: ClassComposer::Generator::InstanceMethods

Defined in:
lib/class_composer/generator/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#class_composer_assign_defaults!(children: false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/class_composer/generator/instance_methods.rb', line 31

def class_composer_assign_defaults!(children: false)
  self.class.composer_mapping.each do |key, |
    assigned_value = method(:"#{key}").call
    method(:"#{key}=").call(assigned_value)

    if children && [:children]
      method(:"#{key}").call().class_composer_assign_defaults!(children: children)
    end
  end

  nil
end

#class_composer_freeze_objects!(behavior: nil, children: false, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/class_composer/generator/instance_methods.rb', line 44

def class_composer_freeze_objects!(behavior: nil, children: false, &block)
  if behavior && block
    raise ArgumentError, "`behavior` and `block` can not both be present. Choose one"
  end

  if behavior.nil? && block.nil?
    raise ArgumentError, "`behavior` or `block` must be present."
  end

  if block
    @class_composer_frozen = block
  else
    if !FROZEN_TYPES.include?(behavior)
      raise Error, "Unknown behavior [#{behavior}]. Expected one of #{FROZEN_TYPES}."
    end
    @class_composer_frozen = behavior
  end

  # If children is set, iterate the children, otherwise exit early
  return if children == false

  self.class.composer_mapping.each do |key, |
    next unless [:children]

    method(:"#{key}").call().class_composer_freeze_objects!(behavior:, children:, &block)
  end
end

#class_composer_frozen!(key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/class_composer/generator/instance_methods.rb', line 6

def class_composer_frozen!(key)
  # when nil, we allow changes to the instance methods
  return if @class_composer_frozen.nil?

  # When frozen is a proc, we let the user decide how to handle
  # The return value decides if the value can be changed or not
  if Proc === @class_composer_frozen
    return @class_composer_frozen.(self, key)
  end

  msg = "#{self.class} instance methods are frozen. Attempted to change variable [#{key}]."
  case @class_composer_frozen
  when FROZEN_LOG_AND_ALLOW
    msg += " This operation will proceed."
    Kernel.warn(msg)
    return true
  when FROZEN_LOG_AND_SKIP
    msg += " This operation will NOT proceed."
    Kernel.warn(msg)
    return false
  when FROZEN_RAISE
    raise Error, msg
  end
end