Class: Occi::Core::Renderers::Json::Attributes

Inherits:
Base
  • Object
show all
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.

Author:

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

Base::DELEGATED

Instance Attribute Summary

Attributes inherited from Base

#object, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Occi::Core::Renderers::Json::Base

Instance Method Details

#renderString

Renders the given object to `JSON`.

Returns:

  • (String)

    object rendering as JSON

Raises:


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_hashObject

: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

#typecasterObject

:nodoc:


42
43
44
# File 'lib/occi/core/renderers/json/attributes.rb', line 42

def typecaster
  Hash.new(DEFAULT_LAMBDA).merge(TYPECASTER_HASH)
end