Class: Agilibox::Serializers::Base
- Inherits:
-
Object
- Object
- Agilibox::Serializers::Base
- Defined in:
- app/serializers/agilibox/serializers/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .format(value) ⇒ Object
- .format_boolean(value) ⇒ Object
- .format_datetime(value) ⇒ Object
- .format_decimal(value) ⇒ Object
- .format_default(value) ⇒ Object
- .format_integer(value) ⇒ Object
- .formatter_for(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(data, options = {}) ⇒ Base
constructor
A new instance of Base.
- #render_file(_file_path) ⇒ Object
- #render_inline ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ Base
Returns a new instance of Base.
4 5 6 7 |
# File 'app/serializers/agilibox/serializers/base.rb', line 4 def initialize(data, = {}) @data = data @options = end |
Class Attribute Details
.decimals_precision ⇒ Object
18 19 20 |
# File 'app/serializers/agilibox/serializers/base.rb', line 18 def decimals_precision @decimals_precision ||= 2 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'app/serializers/agilibox/serializers/base.rb', line 2 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'app/serializers/agilibox/serializers/base.rb', line 2 def @options end |
Class Method Details
.format(value) ⇒ Object
52 53 54 55 |
# File 'app/serializers/agilibox/serializers/base.rb', line 52 def format(value) formatter = "format_" + formatter_for(value).to_s public_send(formatter, value) end |
.format_boolean(value) ⇒ Object
32 33 34 |
# File 'app/serializers/agilibox/serializers/base.rb', line 32 def format_boolean(value) I18n.t(value.to_s) end |
.format_datetime(value) ⇒ Object
36 37 38 |
# File 'app/serializers/agilibox/serializers/base.rb', line 36 def format_datetime(value) Agilibox::AllHelpers.date(value) end |
.format_decimal(value) ⇒ Object
28 29 30 |
# File 'app/serializers/agilibox/serializers/base.rb', line 28 def format_decimal(value) value.to_f.round(decimals_precision) # Fix BigDecimal and 0.1 + 0.2 end |
.format_default(value) ⇒ Object
40 41 42 |
# File 'app/serializers/agilibox/serializers/base.rb', line 40 def format_default(value) value.to_s end |
.format_integer(value) ⇒ Object
24 25 26 |
# File 'app/serializers/agilibox/serializers/base.rb', line 24 def format_integer(value) value end |
.formatter_for(value) ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/serializers/agilibox/serializers/base.rb', line 44 def formatter_for(value) return :integer if value.is_a?(Integer) return :decimal if value.is_a?(Numeric) return :boolean if value.is_a?(TrueClass) || value.is_a?(FalseClass) return :datetime if value.is_a?(Date) || value.is_a?(Time) return :default end |
Instance Method Details
#render_file(_file_path) ⇒ Object
13 14 15 |
# File 'app/serializers/agilibox/serializers/base.rb', line 13 def render_file(_file_path) raise NotImplementedError end |
#render_inline ⇒ Object
9 10 11 |
# File 'app/serializers/agilibox/serializers/base.rb', line 9 def render_inline raise NotImplementedError end |