Class: RailsXapi::Context

Inherits:
ApplicationRecord show all
Includes:
Serializable
Defined in:
app/models/rails_xapi/context.rb

Overview

Constant Summary

Constants included from Serializable

Serializable::LATIN_LETTERS, Serializable::LATIN_LETTERS_REGEX

Instance Method Summary collapse

Instance Method Details

#as_jsonObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/rails_xapi/context.rb', line 86

def as_json
  context_attributes = {}
  context_attributes[:registration] = registration if registration.present?
  context_attributes[:instructor] = instructor if instructor.present?
  context_attributes[:team] = team if team.present?

  if context_activities.present?
    grouped = context_activities.group_by(&:activity_type)
    # convert string keys to symbols
    context_attributes[:contextActivities] = grouped
      .transform_keys(&:to_sym)
      .transform_values { |activities| activities.map(&:as_json) }
  end

  context_attributes[
    :statement
  ] = statement_ref.id if statement_ref&.id.present?

  context_attributes
end

#contextActivities=(context_activities_hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/rails_xapi/context.rb', line 20

def contextActivities=(context_activities_hash)
  context_activities_hash.each do |activity_type, activities|
    activities.each do |activity|
      # Create the object and update it if necessary.
      object =
        RailsXapi::Object.find_or_create(activity) do
          object.activity_definition = activity[:definition] if activity[
            :definition
          ].present?
        end

      object.update(activity)
      # Create the ContextActivity object.
      context_activity =
        RailsXapi::ContextActivity.new(
          activity_type: activity_type.to_s,
          object: object
        )
      context_activities << context_activity
    end
  end
end

#extensions=(extensions_data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/rails_xapi/context.rb', line 72

def extensions=(extensions_data)
  unless extensions_data.is_a?(Hash)
    raise RailsXapi::Errors::XapiError,
          I18n.t(
            "rails_xapi.errors.attribute_must_be_a_hash",
            name: "extensions"
          )
  end

  extensions_data.each do |iri, data|
    extensions.build(iri: iri, value: serialized_value(data))
  end
end

#instructor=(value) ⇒ Object

Set the instructor value and create the actor if provided.



44
45
46
47
48
49
# File 'app/models/rails_xapi/context.rb', line 44

def instructor=(value)
  return if value.blank?

  actor = (value)
  super(actor) if actor.present?
end

#statement=(value) ⇒ Object

Set the statement_ref value if provided. RailsXapi::Context needs a setter to save the “statement” data. However, it also belongs to a RailsXapi::Statement. Therefore, we use the attribute :statement_ref.



62
63
64
65
66
67
68
69
70
# File 'app/models/rails_xapi/context.rb', line 62

def statement=(value)
  return unless value.is_a?(Hash) && value[:objectType] == "StatementRef"

  id = value[:id]
  return if id.nil?

  statement_row = RailsXapi::Statement.find_by(id: id)
  self[:statement_ref] = statement_row.id if statement_row&.id.present?
end

#team=(value) ⇒ Object

Set the team value and create the actor if provided.



52
53
54
55
56
57
# File 'app/models/rails_xapi/context.rb', line 52

def team=(value)
  return if value.blank?

  actor = (value, "Group")
  super(actor) if actor.present?
end