Class: RailsBase::Configuration::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_base/configuration/base.rb

Defined Under Namespace

Classes: ConfigurationAlreadyEstablished, InvalidConfiguration, InvalidCustomConfiguration

Constant Summary collapse

ALLOWED_TYPES =
{
  array: -> (val) { [Array].include?(val.class) },
  array_nil: -> (val) { [Array, NilClass].include?(val.class) },
  boolean: -> (val) { [TrueClass, FalseClass].include?(val.class) },
  duration: -> (val) { [ActiveSupport::Duration].include?(val.class) },
  hash: -> (val) { [Hash].include?(val.class) },
  integer: -> (val) { [Integer].include?(val.class) },
  klass: -> (_val) { true },
  path: -> (val) { [Pathname].include?(val.class) },
  proc: -> (val) { [Proc].include?(val.class) },
  string: -> (val) { [String].include?(val.class) },
  string_nil: -> (val) { [String, NilClass].include?(val.class) },
  string_proc: -> (val) { [String, Proc].include?(val.class) },
  symbol: -> (val) { [Symbol].include?(val.class) },
  symbol_class: -> (val) { [Symbol].include?(val.class) || val.superclass === ActiveJob::QueueAdapters },
  values: -> (_val) { true },
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



36
37
38
39
40
# File 'lib/rails_base/configuration/base.rb', line 36

def initialize
  override_methods!
  assign_default_values!
  def_convenience_methods
end

Class Method Details

._allow_write_block?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/rails_base/configuration/base.rb', line 8

def self._allow_write_block?
  true
end

._unset_allow_write!Object



12
13
14
15
16
# File 'lib/rails_base/configuration/base.rb', line 12

def self._unset_allow_write!
  define_singleton_method('_allow_write_block?') do
    false
  end
end

Instance Method Details

#assign_default_values!Object



61
62
63
64
65
66
67
# File 'lib/rails_base/configuration/base.rb', line 61

def assign_default_values!
  self.class::DEFAULT_VALUES.each do |key, object|
    val = object[:default_assign_on_boot] ? object[:default_assign_on_boot].call : object[:default]
    public_send(:"#{key}=", val)
  end
  true
end

#dig(*args, default: nil, &block) ⇒ Object

on any object you Base inherited object yoyu can call ‘.dig(:chain1, :chain2, :chain3)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_base/configuration/base.rb', line 44

def dig(*args, default: nil, &block)
  current = self.public_send(args[0])

  args[1..-1].each do |key|
    return current || default if current.nil?

    current = current.public_send(key)
  end
  current
rescue StandardError
  if block_given?
    yield block
  else
    default
  end
end

#override_methods!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rails_base/configuration/base.rb', line 69

def override_methods!
  self.class::DEFAULT_VALUES.each do |key, object|
    self.class.define_method(:"#{key}=") do |value|
      raise ConfigurationAlreadyEstablished, "Unable to assign [#{_name}.#{key}]. Assignment must happen on boot" unless self.class._allow_write_block?

      instance_variable_set(:"@#{key}", value)
    end
    if object[:type] == :array
      self.class.define_method(:"#{key}<<") do |value|
        raise ConfigurationAlreadyEstablished, "Unable to assign [#{_name}.#{key}]. Assignment must happen on boot" unless self.class._allow_write_block?

        curr = instance_variable_get(:"@#{key}")
        curr << value
        instance_variable_set(:"@#{key}", curr)
      end
    end
    if object[:type] == :hash
      self.class.define_method(:"#{key}.merge") do |value|
        raise ConfigurationAlreadyEstablished, "Unable to assign [#{_name}.#{key}]. Assignment must happen on boot" unless self.class._allow_write_block?

        curr = instance_variable_get(:"@#{key}")
        curr.merge(value)
        instance_variable_set(:"@#{key}", curr)
      end
    end
  end
end

#validate!Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails_base/configuration/base.rb', line 97

def validate!
  custom_validations
  self.class::DEFAULT_VALUES.each do |key, object|
    value = instance_variable_get("@#{key}".to_sym)
    validate_var!(key: key, var: value, type: object[:type])
    validate_custom_rule!(var: value, custom: object[:custom], key: key, msg: object[:msg])
    validate_klass_type!(key: key, var: value, type: object[:type], klass_type: object[:klass_type])
    validate_values_included!(key: key, var: value, type: object[:type], expect_values: object[:expect_values])
    if object[:on_assignment]
      if object[:on_assignment].is_a? Array
        object[:on_assignment].each do |elem|
          elem.call(value, self)
        end
      else
        object[:on_assignment].call(value, self)
      end
    end
  end
  true
end