Class: TinCanApi::ActivityDefinition

Inherits:
Object
  • Object
show all
Includes:
InteractionType
Defined in:
lib/tin_can_api/activity_definition.rb

Overview

Activity Definition model class

Constant Summary

Constants included from InteractionType

InteractionType::CHOICE, InteractionType::FILL_IN, InteractionType::LIKERT, InteractionType::MATCHING, InteractionType::NUMERIC, InteractionType::OTHER, InteractionType::PERFORMANCE, InteractionType::SEQUENCING, InteractionType::TRUE_FALSE

Constants included from Enum

Enum::CONVERT_PROC

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InteractionType

#type_by_string

Methods included from Enum

extended

Constructor Details

#initialize(options = {}, &block) ⇒ ActivityDefinition

Returns a new instance of ActivityDefinition.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tin_can_api/activity_definition.rb', line 11

def initialize(options={}, &block)
  json = options.fetch(:json, nil)
  if json
    attributes = JSON.parse(json)
    self.type = attributes['type'] if attributes['type']
    self.more_info = attributes['moreInfo'] if attributes['moreInfo']
    self.name = attributes['name'] if attributes['name']
    self.description = attributes['description'] if attributes['description']
    self.extensions = attributes['extensions'] if attributes['extensions']
    self.interaction_type = attributes['interactionType'] if attributes['interactionType']

    if attributes['correctResponsesPattern']
      self.correct_responses_pattern = []
      attributes['correctResponsesPattern'].each do |pattern|
        correct_responses_pattern << pattern
      end
    end

    if attributes['choices']
      self.choices = []
      attributes['choices'].each do |choice|
        choices << InteractionComponent.new(choice.to_json)
      end
    end

    if attributes['scale']
      self.scale = []
      attributes['scale'].each do |element|
        scale << InteractionComponent.new(element.to_json)
      end
    end

    if attributes['source']
      self.source = []
      attributes['source'].each do |element|
        source << InteractionComponent.new(element.to_json)
      end
    end

    if attributes['target']
      self.target = []
      attributes['target'].each do |element|
        target << InteractionComponent.new(element.to_json)
      end
    end

    if attributes['steps']
      self.steps = []
      attributes['steps'].each do |element|
        steps << InteractionComponent.new(element.to_json)
      end
    end

  else
    self.name = options.fetch(:name, nil)
    self.description = options.fetch(:description, nil)
    self.extensions = options.fetch(:extensions, nil)
    self.interaction_type = options.fetch(:interaction_type, nil)
    self.correct_responses_pattern = options.fetch(:correct_responses_pattern, nil)
    self.choices = options.fetch(:choices, nil)
    self.scale = options.fetch(:scale, nil)
    self.source = options.fetch(:source, nil)
    self.target = options.fetch(:target, nil)
    self.type = options.fetch(:type, nil)
    self.more_info = options.fetch(:more_info, nil)

    if block_given?
      block[self]
    end
  end
end

Instance Attribute Details

#choicesObject

Returns the value of attribute choices.



8
9
10
# File 'lib/tin_can_api/activity_definition.rb', line 8

def choices
  @choices
end

#correct_responses_patternObject

Returns the value of attribute correct_responses_pattern.



7
8
9
# File 'lib/tin_can_api/activity_definition.rb', line 7

def correct_responses_pattern
  @correct_responses_pattern
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/tin_can_api/activity_definition.rb', line 7

def description
  @description
end

#extensionsObject

Returns the value of attribute extensions.



7
8
9
# File 'lib/tin_can_api/activity_definition.rb', line 7

def extensions
  @extensions
end

#interaction_typeObject

Returns the value of attribute interaction_type.



7
8
9
# File 'lib/tin_can_api/activity_definition.rb', line 7

def interaction_type
  @interaction_type
end

#more_infoObject

Returns the value of attribute more_info.



9
10
11
# File 'lib/tin_can_api/activity_definition.rb', line 9

def more_info
  @more_info
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/tin_can_api/activity_definition.rb', line 7

def name
  @name
end

#scaleObject

Returns the value of attribute scale.



8
9
10
# File 'lib/tin_can_api/activity_definition.rb', line 8

def scale
  @scale
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/tin_can_api/activity_definition.rb', line 8

def source
  @source
end

#stepsObject

Returns the value of attribute steps.



8
9
10
# File 'lib/tin_can_api/activity_definition.rb', line 8

def steps
  @steps
end

#targetObject

Returns the value of attribute target.



8
9
10
# File 'lib/tin_can_api/activity_definition.rb', line 8

def target
  @target
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/tin_can_api/activity_definition.rb', line 9

def type
  @type
end

Instance Method Details

#serialize(version) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/tin_can_api/activity_definition.rb', line 99

def serialize(version)
  node = {}
  node['name'] = name if name
  node['description'] = description if description
  node['type'] = type.to_s if type
  node['moreInfo'] = more_info.to_s if more_info
  node['extensions'] = extensions if extensions
  if interaction_type
    node['interactionType'] = interaction_type.to_s
    case interaction_type
      when TinCanApi::InteractionType::CHOICE.to_s, TinCanApi::InteractionType::SEQUENCING.to_s
        if choices && choices.any?
          node['choices'] = choices.map {|element| element.serialize(version)}
        end
      when TinCanApi::InteractionType::LIKERT.to_s
        if scale && scale.any?
          node['scale'] = scale.map {|element| element.serialize(version)}
        end
      when TinCanApi::InteractionType::MATCHING.to_s
        if source && source.any?
          node['source'] = source.map {|element| element.serialize(version)}
        end
        if target && target.any?
          node['target'] = target.map {|element| element.serialize(version)}
        end
      when TinCanApi::InteractionType::PERFORMANCE.to_s
        if steps && steps.any?
          node['steps'] = steps.map {|element| element.serialize(version)}
        end
    end
  end

  if correct_responses_pattern && correct_responses_pattern.any?
    node['correctResponsesPattern'] = correct_responses_pattern.map {|element| element}
  end

  node
end