Class: Schema::AttributeNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/schema/attribute_normalizer.rb

Instance Method Summary collapse

Constructor Details

#initializeAttributeNormalizer

Returns a new instance of AttributeNormalizer.



5
6
7
# File 'lib/schema/attribute_normalizer.rb', line 5

def initialize
  @normalizations = []
end

Instance Method Details

#add(method, options = {}) ⇒ Object



9
10
11
# File 'lib/schema/attribute_normalizer.rb', line 9

def add(method, options = {})
  @normalizations << options.merge(method: method)
end

#normalize(model, value) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/schema/attribute_normalizer.rb', line 18

def normalize(model, value)
  return nil if value.nil?

  @normalizations.each do |normalization|
    value = apply_normalization(model, value, normalization)
  end
  value
end

#normalize_model_attribute(model, attribute) ⇒ Object



13
14
15
16
# File 'lib/schema/attribute_normalizer.rb', line 13

def normalize_model_attribute(model, attribute)
  value = normalize(model, model.public_send(attribute))
  model.public_send("#{attribute}=", value)
end