Class: Amoeba::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/amoeba/config.rb

Constant Summary collapse

DEFAULTS =
{
  enabled: false,
  inherit: false,
  do_preproc: false,
  parenting: false,
  raised: false,
  dup_method: :dup,
  remap_method: nil,
  includes: {},
  excludes: {},
  clones: [],
  customizations: [],
  overrides: [],
  null_fields: [],
  coercions: {},
  prefixes: {},
  suffixes: {},
  regexes: {},
  known_macros: %i[has_one has_many has_and_belongs_to_many]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Config

Returns a new instance of Config.



35
36
37
38
# File 'lib/amoeba/config.rb', line 35

def initialize(klass)
  @klass  = klass
  @config = self.class::DEFAULTS.deep_dup
end

Instance Method Details

#clone(value = nil) ⇒ Object



116
117
118
119
# File 'lib/amoeba/config.rb', line 116

def clone(value = nil)
  enable
  push_value_to_array(value, :clones)
end

#disableObject



46
47
48
# File 'lib/amoeba/config.rb', line 46

def disable
  @config[:enabled] = false
end

#enableObject



42
43
44
# File 'lib/amoeba/config.rb', line 42

def enable
  @config[:enabled] = true
end

#exclude_association(value = nil, options = {}) ⇒ Object



105
106
107
108
109
110
# File 'lib/amoeba/config.rb', line 105

def exclude_association(value = nil, options = {})
  enable
  @config[:includes] = {}
  value = value.is_a?(Array) ? value.map! { |v| [v, options] }.to_h : { value => options }
  push_value_to_hash(value, :excludes)
end

#exclude_associations(*values) ⇒ Object



112
113
114
# File 'lib/amoeba/config.rb', line 112

def exclude_associations(*values)
  values.flatten.each { |v| exclude_association(v) }
end

#fill_hash_value_for(config_key, key, val) ⇒ Object



90
91
92
# File 'lib/amoeba/config.rb', line 90

def fill_hash_value_for(config_key, key, val)
  @config[config_key][key] = val if val || (!val.nil? && config_key == :coercions)
end

#include_association(value = nil, options = {}) ⇒ Object



94
95
96
97
98
99
# File 'lib/amoeba/config.rb', line 94

def include_association(value = nil, options = {})
  enable
  @config[:excludes] = {}
  value = value.is_a?(Array) ? value.map! { |v| [v, options] }.to_h : { value => options }
  push_value_to_hash(value, :includes)
end

#include_associations(*values) ⇒ Object



101
102
103
# File 'lib/amoeba/config.rb', line 101

def include_associations(*values)
  values.flatten.each { |v| include_association(v) }
end

#propagate(style = :submissive) ⇒ Object



54
55
56
57
# File 'lib/amoeba/config.rb', line 54

def propagate(style = :submissive)
  @config[:parenting] ||= style
  @config[:inherit] = true
end

#push_array_value_to_hash(value, config_key) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/amoeba/config.rb', line 69

def push_array_value_to_hash(value, config_key)
  @config[config_key] = {}

  value.each do |definition|
    definition.each do |key, val|
      fill_hash_value_for(config_key, key, val)
    end
  end
end

#push_value_to_array(value, key) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/amoeba/config.rb', line 59

def push_value_to_array(value, key)
  res = @config[key]
  if value.is_a?(::Array)
    res = value
  elsif value
    res << value
  end
  @config[key] = res.uniq
end

#push_value_to_hash(value, config_key) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/amoeba/config.rb', line 79

def push_value_to_hash(value, config_key)
  if value.is_a?(Array)
    push_array_value_to_hash(value, config_key)
  else
    value.each do |key, val|
      fill_hash_value_for(config_key, key, val)
    end
  end
  @config[config_key]
end

#raised(style = :submissive) ⇒ Object



50
51
52
# File 'lib/amoeba/config.rb', line 50

def raised(style = :submissive)
  @config[:raised] = style
end

#recognize(value = nil) ⇒ Object



121
122
123
124
# File 'lib/amoeba/config.rb', line 121

def recognize(value = nil)
  enable
  push_value_to_array(value, :known_macros)
end

#remapper(value) ⇒ Object



150
151
152
# File 'lib/amoeba/config.rb', line 150

def remapper(value)
  @config[:remap_method] = value.to_sym
end

#through(value) ⇒ Object



146
147
148
# File 'lib/amoeba/config.rb', line 146

def through(value)
  @config[:dup_method] = value.to_sym
end

#upbringingObject



40
# File 'lib/amoeba/config.rb', line 40

alias upbringing raised