Class: TinCanApi::ContextActivities

Inherits:
Object
  • Object
show all
Defined in:
lib/tin_can_api/context_activities.rb

Overview

ContextActivities Model class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ContextActivities.



8
9
10
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
# File 'lib/tin_can_api/context_activities.rb', line 8

def initialize(options={}, &block)
  json = options.fetch(:json, nil)
  if json
    attributes = JSON.parse(json)
    if attributes['parent']
      if attributes['parent'].is_a?(Array)
        self.parent = attributes['parent'].map {|element| Activity.new(json: element.to_json)}
      else
        self.parent = [Activity.new(json: attributes['parent'].to_json)]
      end
    end

    if attributes['grouping']
      if attributes['grouping'].is_a?(Array)
        self.grouping = attributes['grouping'].map {|element| Activity.new(json: element.to_json)}
      else
        self.grouping = [Activity.new(json: attributes['grouping'].to_json)]
      end
    end

    if attributes['other']
      if attributes['other'].is_a?(Array)
        self.other = attributes['other'].map {|element| Activity.new(json: element.to_json)}
      else
        self.other = [Activity.new(json: attributes['other'].to_json)]
      end
    end

    if attributes['category']
      if attributes['category'].is_a?(Array)
        self.category = attributes['category'].map {|element| Activity.new(json: element.to_json)}
      else
        self.category = [Activity.new(json: attributes['category'].to_json)]
      end
    end
  else
    self.parent = options.fetch(:parent, nil)
    self.grouping = options.fetch(:grouping, nil)
    self.other = options.fetch(:other, nil)
    self.category = options.fetch(:category, nil)

    if block_given?
      block[self]
    end
  end
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



6
7
8
# File 'lib/tin_can_api/context_activities.rb', line 6

def category
  @category
end

#groupingObject

Returns the value of attribute grouping.



6
7
8
# File 'lib/tin_can_api/context_activities.rb', line 6

def grouping
  @grouping
end

#otherObject

Returns the value of attribute other.



6
7
8
# File 'lib/tin_can_api/context_activities.rb', line 6

def other
  @other
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/tin_can_api/context_activities.rb', line 6

def parent
  @parent
end

Instance Method Details

#serialize(version) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tin_can_api/context_activities.rb', line 55

def serialize(version)
  node = {}
  if parent && parent.any?
    if version == TinCanApi::TCAPIVersion::V095 && parent.size > 1
      raise TinCanApi::Errors::IncompatibleTCAPIVersion, "Version #{version.to_s} doesn't support lists of activities (parent)"
    end
    if version == TinCanApi::TCAPIVersion::V095
      node['parent'] = parent.first.serialize(version)
    else
      node['parent'] = parent.map {|element| element.serialize(version)}
    end
  end

  if grouping && grouping.any?
    if version == TinCanApi::TCAPIVersion::V095 && grouping.size > 1
      raise TinCanApi::Errors::IncompatibleTCAPIVersion, "Version #{version.to_s} doesn't support lists of activities (grouping)"
    end
    if version == TinCanApi::TCAPIVersion::V095
      node['grouping'] = grouping.first.serialize(version)
    else
      node['grouping'] = grouping.map {|element| element.serialize(version)}
    end
  end

  if other && other.any?
    if version == TinCanApi::TCAPIVersion::V095 && other.size > 1
      raise TinCanApi::Errors::IncompatibleTCAPIVersion, "Version #{version.to_s} doesn't support lists of activities (other)"
    end
    if version == TinCanApi::TCAPIVersion::V095
      node['other'] = other.first.serialize(version)
    else
      node['other'] = other.map {|element| element.serialize(version)}
    end
  end

  if category && category.any?
    if version.ordinal <= TinCanApi::TCAPIVersion::V100.ordinal
      node['category'] = category.map {|element| element.serialize(version)}
    else
      raise TinCanApi::Errors::IncompatibleTCAPIVersion, "Version #{version.to_s} doesn't support the category context activity"
    end
  end

  node
end