class RailsBase::Configuration::Base
Constants
- ALLOWED_TYPES
Public Class Methods
_allow_write_block?()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 8 def self._allow_write_block? true end
_unset_allow_write!()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 12 def self._unset_allow_write! define_singleton_method('_allow_write_block?') do false end end
new()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 35 def initialize override_methods! assign_default_values! def_convenience_methods end
Public Instance Methods
assign_default_values!()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 60 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| ... }
click to toggle source
on any object you Base
inherited object yoyu can call `.dig(:chain1, :chain2, :chain3)
# File lib/rails_base/configuration/base.rb, line 43 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!()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 68 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!()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 96 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
Private Instance Methods
_name()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 197 def _name self.class.name.demodulize end
custom_validations()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 119 def custom_validations end
def_convenience_methods()
click to toggle source
# File lib/rails_base/configuration/base.rb, line 122 def def_convenience_methods self.class::DEFAULT_VALUES.each do |key, object| if object[:type] == :boolean self.class.define_method("#{key}?") do return false unless dependents_true?(key) public_send(key) end elsif object[:type] == :proc self.class.define_method("#{key}?") do |current_user| return false unless dependents_true?(key) public_send(key).call(current_user) end elsif object[:type] == :string_proc self.class.define_method("#{key}") do |*args| return false unless dependents_true?(key) if instance_variable_get("@#{key}".to_sym).is_a? Proc instance_variable_get("@#{key}".to_sym).call(args[0]) else instance_variable_get("@#{key}".to_sym) end end else self.class.define_method("#{key}") do return false unless dependents_true?(key) instance_variable_get("@#{key}".to_sym) end end end end
dependents_true?(key)
click to toggle source
# File lib/rails_base/configuration/base.rb, line 156 def dependents_true?(key) dependents = self.class::DEFAULT_VALUES[key][:dependents] || [] dependents.all? { |s| s.call(self) } end
validate_custom_rule!(var:, custom:, key:, msg:)
click to toggle source
# File lib/rails_base/configuration/base.rb, line 201 def validate_custom_rule!(var:, custom:, key:, msg:) return if custom.nil? return if custom.call(var) raise InvalidCustomConfiguration, msg end
validate_klass_type!(var:, type:, key:, klass_type:)
click to toggle source
# File lib/rails_base/configuration/base.rb, line 169 def validate_klass_type!(var:, type:, key:, klass_type:) return if klass_type.nil? boolean = if var.is_a? Array var.all? { |s| klass_type.include?(s.class) } else klass_type.include?(var.class) end return if boolean raise InvalidConfiguration, "#{_name}.#{key} expects all members to be a #{klass_type}. Received: [#{var.class}]" end
validate_values_included!(key:, var:, type:, expect_values:)
click to toggle source
# File lib/rails_base/configuration/base.rb, line 183 def validate_values_included!(key:, var:, type:, expect_values:) return if expect_values.nil? values = if expect_values.is_a? Proc expect_values.call(self) else expect_values end return if values.include?(var) raise InvalidConfiguration, "#{_name}.#{key} expects value to be included in [#{values}]. Received: [#{var}]" end
validate_var!(var:, type:, key:)
click to toggle source
# File lib/rails_base/configuration/base.rb, line 162 def validate_var!(var:, type:, key:) proc = ALLOWED_TYPES.fetch(type) return if proc.call(var) raise InvalidConfiguration, "#{_name}.#{key} expects a #{type}." end