Class: Occi::Core::Renderers::Json::Attributes
- Defined in:
- lib/occi/core/renderers/json/attributes.rb
Overview
Implements routines required to pre-render instance attributes and its subclasses to a JSON-based representation.
Constant Summary collapse
- DEFAULT_LAMBDA =
Typecasting lambdas
->(val) { val }
- TYPECASTER_HASH =
{ IPAddr => ->(val) { val.host? ? val.to_s : "#{val}/#{val.cidr_mask}" }, URI => ->(val) { val.to_s } }.freeze
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#render ⇒ String
Renders the given object to ‘JSON`.
-
#render_hash ⇒ Object
:nodoc:.
-
#typecast(value, type) ⇒ Object
:nodoc:.
-
#typecaster ⇒ Object
:nodoc:.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Occi::Core::Renderers::Json::Base
Instance Method Details
#render ⇒ String
Renders the given object to ‘JSON`.
22 23 24 |
# File 'lib/occi/core/renderers/json/attributes.rb', line 22 def render raise Occi::Core::Errors::RendererError, 'Cannot render a standalone instance attribute' end |
#render_hash ⇒ Object
:nodoc:
27 28 29 30 31 32 33 34 |
# File 'lib/occi/core/renderers/json/attributes.rb', line 27 def render_hash attrs = {} object.each_pair do |name, content| next unless content.value? attrs[name] = typecast(content.value, content.attribute_definition.type) end attrs end |
#typecast(value, type) ⇒ Object
:nodoc:
37 38 39 |
# File 'lib/occi/core/renderers/json/attributes.rb', line 37 def typecast(value, type) typecaster[type].call(value) end |
#typecaster ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/occi/core/renderers/json/attributes.rb', line 42 def typecaster Hash.new(DEFAULT_LAMBDA).merge(TYPECASTER_HASH) end |