Class: Rseed::Converter
- Inherits:
-
Object
- Object
- Rseed::Converter
- Defined in:
- lib/rseed/converter.rb
Class Attribute Summary collapse
-
.converter_attributes ⇒ Object
Returns the value of attribute converter_attributes.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
- #logger ⇒ Object
- #options ⇒ Object
Class Method Summary collapse
-
.attribute(name, options) ⇒ Object
Used to define an attribute when creating a converter.
- .mandatory_attributes ⇒ Object
- .name ⇒ Object
Instance Method Summary collapse
- #after_deserialize ⇒ Object
- #before_deserialize ⇒ Object
-
#deserialize(values) ⇒ Object
Dummy convert function.
-
#deserialize_raw(values) ⇒ Object
Takes the raw values coming out of an adapter and converts them based on the attribute definitions in the converter.
- #fail_with_error(e) ⇒ Object
- #name ⇒ Object
- #remove_blank_from(values) ⇒ Object
-
#remove_nil_from(values) ⇒ Object
Helpers for converters.
Class Attribute Details
.converter_attributes ⇒ Object
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
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'lib/rseed/converter.rb', line 8 def error @error end |
#logger ⇒ Object
21 22 23 |
# File 'lib/rseed/converter.rb', line 21 def logger @logger.nil? ? Rseed.logger : @logger end |
#options ⇒ Object
25 26 27 28 |
# File 'lib/rseed/converter.rb', line 25 def @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, @converter_attributes ||= [] converter_attributes << Attribute.new(name, ) end |
.mandatory_attributes ⇒ Object
43 44 45 |
# File 'lib/rseed/converter.rb', line 43 def self.mandatory_attributes converter_attributes.reject { |a| a.[:optional] } end |
.name ⇒ Object
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_deserialize ⇒ Object
40 41 |
# File 'lib/rseed/converter.rb', line 40 def after_deserialize end |
#before_deserialize ⇒ Object
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 |
#name ⇒ Object
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 |