Class: ActiveLrs::Xapi::InteractionComponent

Inherits:
Object
  • Object
show all
Includes:
LocalizationHelper
Defined in:
lib/active_lrs/xapi/interaction_component.rb

Overview

Represents an xAPI Interaction Component.

Interaction components are used in certain ActivityDefinitions where an interactionType is specified (e.g. multiple choice, sequencing, matching, etc.). They describe the options or elements involved (choices, scale steps, source/target, etc.). Each component must have an id, and may include a description map.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LocalizationHelper

#get_localized_value

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new InteractionComponent instance with optional attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    a hash of attributes for the interaction component

Options Hash (attributes):

  • "id" (String)

    The identifier for the component (required).

  • "description" (Hash{String => String})

    A language map for display text.



32
33
34
35
# File 'lib/active_lrs/xapi/interaction_component.rb', line 32

def initialize(attributes = {})
  self.id = attributes["id"] if attributes["id"]
  self.description = attributes["description"] if attributes["description"]
end

Instance Attribute Details

#descriptionHash{String => String}?

Returns A language map providing a human-readable description of the component (optional).

Returns:

  • (Hash{String => String}, nil)

    A language map providing a human-readable description of the component (optional).



23
24
25
# File 'lib/active_lrs/xapi/interaction_component.rb', line 23

def description
  @description
end

#idString

Returns The identifier for this component (required).

Returns:

  • (String)

    The identifier for this component (required).



19
20
21
# File 'lib/active_lrs/xapi/interaction_component.rb', line 19

def id
  @id
end

Instance Method Details

#localize_description(locale: nil) ⇒ String

Returns the localized description of the interaction component.

Parameters:

  • locale (String, Symbol, nil) (defaults to: nil)

    Optional locale to use. Defaults to nil (will use configured defaults).

Returns:

  • (String)

    The localized description, or “undefined” if not available.



60
61
62
# File 'lib/active_lrs/xapi/interaction_component.rb', line 60

def localize_description(locale: nil)
  get_localized_value(description, locale)
end

#to_hHash{String => (String, Hash)}

Converts the InteractionComponent into a hash form suitable for inclusion in an ActivityDefinition interactionType component list.

Examples:

component = ActiveLrs::Xapi::InteractionComponent.new(
  "id" => "choice_A",
  "description" => { "en-US" => "Option A" }
)
component.to_h
# => { "id" => "choice_A", "description" => { "en-US" => "Option A" } }

Returns:

  • (Hash{String => (String, Hash)})

    a hash with id and optionally description



49
50
51
52
53
54
# File 'lib/active_lrs/xapi/interaction_component.rb', line 49

def to_h
  node = {}
  node["id"] = id if id
  node["description"] = description if description
  node
end