Class: Jsoning::Mapper
- Inherits:
-
Object
- Object
- Jsoning::Mapper
- Defined in:
- lib/jsoning/foundations/mapper.rb
Overview
takes care of translating/fetching values from the object
Instance Attribute Summary collapse
-
#custom_block ⇒ Object
Returns the value of attribute custom_block.
-
#default_value ⇒ Object
Returns the value of attribute default_value.
- #name ⇒ Object
- #nullable ⇒ Object
-
#parallel_variable ⇒ Object
what variable in the object will be used to obtain the value.
Instance Method Summary collapse
-
#extract(object, target_hash) ⇒ Object
map this very specific object’s field to target_hash.
-
#initialize ⇒ Mapper
constructor
A new instance of Mapper.
Constructor Details
#initialize ⇒ Mapper
Returns a new instance of Mapper.
12 13 14 15 16 |
# File 'lib/jsoning/foundations/mapper.rb', line 12 def initialize self.parallel_variable = nil self.default_value = nil self.nullable = true end |
Instance Attribute Details
#custom_block ⇒ Object
Returns the value of attribute custom_block.
10 11 12 |
# File 'lib/jsoning/foundations/mapper.rb', line 10 def custom_block @custom_block end |
#default_value ⇒ Object
Returns the value of attribute default_value.
6 7 8 |
# File 'lib/jsoning/foundations/mapper.rb', line 6 def default_value @default_value end |
#name ⇒ Object
27 28 29 30 |
# File 'lib/jsoning/foundations/mapper.rb', line 27 def name @name_as_string = @name.to_s if @name_as_string.nil? @name_as_string end |
#nullable ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/jsoning/foundations/mapper.rb', line 18 def nullable if @nullable.is_a?(TrueClass) || @nullable.is_a?(FalseClass) return @nullable else # by default, allow every mapped things to be nil true end end |
#parallel_variable ⇒ Object
what variable in the object will be used to obtain the value
9 10 11 |
# File 'lib/jsoning/foundations/mapper.rb', line 9 def parallel_variable @parallel_variable end |
Instance Method Details
#extract(object, target_hash) ⇒ Object
map this very specific object’s field to target_hash
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jsoning/foundations/mapper.rb', line 33 def extract(object, target_hash) if object.respond_to?(parallel_variable) parallel_val = object.send(parallel_variable) parallel_val = default_value if parallel_val.nil? target_hash[name] = deep_parse(parallel_val) else if default_value target_hash[name] = deep_parse(default_value) else unless nullable raise Jsoning::Error, "Null value given for '#{name}' when serializing #{object}" end end end end |