Class: Rseed::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/rseed/converter.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.converter_attributesObject

Returns the value of attribute converter_attributes.


4
5
6
# File 'lib/rseed/converter.rb', line 4

def converter_attributes
  @converter_attributes
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.


8
9
10
# File 'lib/rseed/converter.rb', line 8

def error
  @error
end

#loggerObject


21
22
23
# File 'lib/rseed/converter.rb', line 21

def logger
  @logger.nil? ? Rseed.logger : @logger
end

#optionsObject


25
26
27
28
# File 'lib/rseed/converter.rb', line 25

def options
  @options ||= {}
  @options
end

Class Method Details

.attribute(name, options) ⇒ Object

Used to define an attribute when creating a converter.


31
32
33
34
# File 'lib/rseed/converter.rb', line 31

def self.attribute name, options
  @converter_attributes ||= []
  converter_attributes << Attribute.new(name, options)
end

.mandatory_attributesObject


43
44
45
# File 'lib/rseed/converter.rb', line 43

def self.mandatory_attributes
  converter_attributes.reject { |a| a.options[:optional] }
end

.nameObject


11
12
13
14
15
# File 'lib/rseed/converter.rb', line 11

def self.name
  class_name = self.to_s
  m = /^(?<name>.*)Converter$/.match(class_name)
  m ? m[:name] : class_name
end

Instance Method Details

#after_deserializeObject


40
41
# File 'lib/rseed/converter.rb', line 40

def after_deserialize
end

#before_deserializeObject


36
37
38
# File 'lib/rseed/converter.rb', line 36

def before_deserialize
  true
end

#deserialize(values) ⇒ Object

Dummy convert function


59
60
61
# File 'lib/rseed/converter.rb', line 59

def deserialize values
  logger.debug values
end

#deserialize_raw(values) ⇒ Object

Takes the raw values coming out of an adapter and converts them based on the attribute definitions in the converter.


49
50
51
52
53
54
55
56
# File 'lib/rseed/converter.rb', line 49

def deserialize_raw values
  converted_values = HashWithIndifferentAccess.new
  self.class.converter_attributes.each do |attribute|
    converted_values[attribute.name] = attribute.deserialize(values, converter: self)
  end

  deserialize converted_values
end

#fail_with_error(e) ⇒ Object


72
73
74
75
# File 'lib/rseed/converter.rb', line 72

def fail_with_error e
  @error = e
  false
end

#nameObject


17
18
19
# File 'lib/rseed/converter.rb', line 17

def name
  self.class.name
end

#remove_blank_from(values) ⇒ Object


68
69
70
# File 'lib/rseed/converter.rb', line 68

def remove_blank_from values
  values.delete_if { |k, v| v.to_s.blank? }
end

#remove_nil_from(values) ⇒ Object

Helpers for converters


64
65
66
# File 'lib/rseed/converter.rb', line 64

def remove_nil_from values
  values.delete_if { |k, v| v.nil? }
end