Class: RailsXapi::Object

Inherits:
ApplicationRecord show all
Defined in:
app/models/rails_xapi/object.rb

Overview

The Object defines the thing that was acted on. See: github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#244-object The Object of a Statement can be an Activity, Agent/Group, SubStatement, or Statement Reference.

Constant Summary collapse

OBJECT_TYPES =
%w[Activity Agent Group SubStatement StatementRef]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def actor
  @actor
end

#contextObject

Returns the value of attribute context.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def context
  @context
end

#objectObject

Returns the value of attribute object.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def object
  @object
end

#objectTypeObject

Returns the value of attribute objectType.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def objectType
  @objectType
end

#resultObject

Returns the value of attribute result.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def result
  @result
end

#timestampObject

Returns the value of attribute timestamp.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def timestamp
  @timestamp
end

#verbObject

Returns the value of attribute verb.



9
10
11
# File 'app/models/rails_xapi/object.rb', line 9

def verb
  @verb
end

Class Method Details

.find_or_create(attributes) ⇒ RailsXapi::Object

Find an Object by its id or create a new one.

Parameters:

  • attributes (Hash)

    The attributes of the requested object.

Returns:



46
47
48
# File 'app/models/rails_xapi/object.rb', line 46

def self.find_or_create(attributes)
  find_by(id: attributes[:id]) || create(attributes)
end

Instance Method Details

#activity?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'app/models/rails_xapi/object.rb', line 59

def activity?
  return true if object_type == "Activity"

  false
end

#as_jsonObject



65
66
67
68
69
# File 'app/models/rails_xapi/object.rb', line 65

def as_json
  { id: id, objectType: object_type }.tap do |hash|
    hash[:definition] = definition.as_json if definition.present?
  end
end

#definition=(definition_hash) ⇒ Object



34
35
36
37
38
39
40
# File 'app/models/rails_xapi/object.rb', line 34

def definition=(definition_hash)
  return unless definition_hash.present?

  # Build or create the associated object.
  build_definition if definition.nil?
  definition.assign_from_json_definition(definition_hash)
end

#update_definition(definition_data) ⇒ Object

Update the ActivityDefinition if it’s existing.



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

def update_definition(definition_data)
  return unless definition_data.present?

  definition = self.definition || create_definition
  definition.assign_from_json_definition(definition_data)
  definition.save!
end