class Occi::Core::ActionInstance

Attributes

action[RW]
attributes[RW]
model[RW]

Public Class Methods

new(action = self.action, attributes=self.attributes) click to toggle source
# File lib/occi/core/action_instance.rb, line 19
def initialize(action = self.action, attributes=self.attributes)
  raise ArgumentError, 'action cannot be nil' unless action

  if action.kind_of? String
    scheme, term = action.split '#'
    action = Occi::Core::Action.new(scheme, term)
  end
  @action = action

  if attributes.kind_of? Occi::Core::Attributes
    @attributes = attributes.convert
  else
    @attributes = Occi::Core::Attributes.new(attributes || {})
  end
end

Public Instance Methods

as_json(options={}) click to toggle source

@param [Hash] options @return [Hashie::Mash] json representation

# File lib/occi/core/action_instance.rb, line 37
def as_json(options={})
  action = Hashie::Mash.new
  action.action = @action.to_s if @action
  action.attributes = @attributes.any? ? @attributes.as_json : Occi::Core::Attributes.new.as_json
  action
end
check(set_defaults = false) click to toggle source

@param [Boolean] set default values for all empty attributes @return [Boolean] Result of the validation process

# File lib/occi/core/action_instance.rb, line 82
def check(set_defaults = false)
  raise ArgumentError, 'No model has been assigned to this action instance' unless @model

  action = @model.get_by_id(@action.type_identifier, true)
  raise Occi::Errors::CategoryNotDefinedError,
        "Action not found for action instance #{self.class.name}[#{self.to_s.inspect}]!" unless action

  definitions = Occi::Core::Attributes.new
  definitions.merge! action.attributes

  @attributes.check!(definitions, set_defaults)
end
empty?() click to toggle source

@return [Boolean] Indicating whether this action instance is “empty”, i.e. required attributes are blank

# File lib/occi/core/action_instance.rb, line 76
def empty?
  action.nil? || action.empty?
end
to_header() click to toggle source

@return [Hash] hash containing the HTTP headers of the text/occi rendering

# File lib/occi/core/action_instance.rb, line 61
def to_header
  header = Hashie::Mash.new
  header['Category'] = @action.to_string_short

  attributes = []
  @attributes.names.each_pair do |name, value|
    value = value.to_s.inspect unless value && value.is_a?(Numeric)
    attributes << "#{name}=#{value}"
  end
  header['X-OCCI-Attribute'] = attributes.join(',') if attributes.any?

  header
end
to_json() click to toggle source

@return [String] JSON representation

# File lib/occi/core/action_instance.rb, line 56
def to_json
  as_json.to_json
end
to_text() click to toggle source

@return [String] text representation

# File lib/occi/core/action_instance.rb, line 45
def to_text
  text = "Category: #{@action.to_string_short}"
  @attributes.names.each_pair do |name, value|
    value = value.to_s.inspect unless value && value.is_a?(Numeric)
    text << "\nX-OCCI-Attribute: #{name}=#{value}"
  end

  text
end