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:   [:has_one, :has_many, :has_and_belongs_to_many]
}

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Config

Returns a new instance of Config.



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

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

Instance Method Details

#clone(value = nil) ⇒ Object



142
143
144
145
# File 'lib/amoeba/config.rb', line 142

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

#disableObject



62
63
64
# File 'lib/amoeba/config.rb', line 62

def disable
  @config[:enabled] = false
end

#enableObject



58
59
60
# File 'lib/amoeba/config.rb', line 58

def enable
  @config[:enabled] = true
end

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



126
127
128
129
130
# File 'lib/amoeba/config.rb', line 126

def exclude_association(value = nil, options = {})
  enable
  @config[:includes] = {}
  push_value_to_hash({ value => options }, :excludes)
end

#exclude_associations(*values) ⇒ Object



132
133
134
# File 'lib/amoeba/config.rb', line 132

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

#exclude_field(value = nil) ⇒ Object

TODO: remove this method in v3.0.0



137
138
139
140
# File 'lib/amoeba/config.rb', line 137

def exclude_field(value = nil)
  warn 'exclude_field is deprecated and will be removed in version 3.0.0; please use exclude_association instead'
  exclude_association(value)
end

#fill_hash_value_for(config_key, key, val) ⇒ Object



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

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



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

def include_association(value = nil, options = {})
  enable
  @config[:excludes] = {}
  push_value_to_hash({ value => options }, :includes)
end

#include_associations(*values) ⇒ Object



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

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

#include_field(value = nil) ⇒ Object

TODO: remove this method in v3.0.0



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

def include_field(value = nil)
  warn 'include_field is deprecated and will be removed in version 3.0.0; please use include_association instead'
  include_association(value)
end

#propagate(style = :submissive) ⇒ Object



70
71
72
73
# File 'lib/amoeba/config.rb', line 70

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

#push_array_value_to_hash(value, config_key) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/amoeba/config.rb', line 85

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



75
76
77
78
79
80
81
82
83
# File 'lib/amoeba/config.rb', line 75

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



95
96
97
98
99
100
101
102
103
104
# File 'lib/amoeba/config.rb', line 95

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



66
67
68
# File 'lib/amoeba/config.rb', line 66

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

#recognize(value = nil) ⇒ Object



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

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

#remapper(value) ⇒ Object



176
177
178
# File 'lib/amoeba/config.rb', line 176

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

#through(value) ⇒ Object



172
173
174
# File 'lib/amoeba/config.rb', line 172

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

#upbringingObject



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

alias_method :upbringing, :raised