Class: IMS::LTI::Models::LTIModel

Inherits:
Object
  • Object
show all
Defined in:
lib/ims/lti/models/lti_model.rb

Constant Summary collapse

LTI_VERSION_2P0 =
'LTI-2p0'.freeze
LTI_VERSION_2P1 =
'LTI-2p1'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ LTIModel

Returns a new instance of LTIModel.



6
7
8
9
10
# File 'lib/ims/lti/models/lti_model.rb', line 6

def initialize(attributes = {})
  @ext_attributes = {}
  @unknown_attributes = {}
  self.attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/ims/lti/models/lti_model.rb', line 102

def method_missing(meth, *args, &block)
  proc = ->(attr, hash) {meth =~ /=$/ ? hash[attr] = args[0] : hash[attr] }
  if (match = /^ext_([^=$]*)/.match(meth))
    proc.call(match.to_s.to_sym, @ext_attributes)
  elsif (match = /([^=$]*)/.match(meth))
    proc.call(match.to_s.to_sym, @unknown_attributes)
  else
    super
  end
end

Class Method Details

.add_attribute(attribute, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ims/lti/models/lti_model.rb', line 21

def self.add_attribute(attribute, options = {})
  @serialization_options ||= {}
  keys = @serialization_options.keys + options.keys
  keys.each do |k|
    @serialization_options[k] ||= {}
    options.has_key?(k) ? @serialization_options[k][attribute] = options[k] : @serialization_options[k].delete(attribute)
  end
  unless self.attributes.include? attribute
    self.attributes += [attribute]
    attr_accessor(attribute)
  end
end

.add_attributes(attribute, *attrs) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ims/lti/models/lti_model.rb', line 12

def self.add_attributes(attribute, *attrs)
  attrs.unshift(attribute)
  attrs -= self.attributes
  if attrs.size > 0
    self.attributes += attrs
    attr_accessor(attrs.shift, *attrs)
  end
end

.from_json(json) ⇒ Object



44
45
46
# File 'lib/ims/lti/models/lti_model.rb', line 44

def self.from_json(json)
  new.from_json(json)
end

.inherit_attributes(attrs) ⇒ Object



34
35
36
37
# File 'lib/ims/lti/models/lti_model.rb', line 34

def self.inherit_attributes(attrs)
  attributes ||= []
  self.attributes += attrs
end

.inherited(subclass) ⇒ Object



39
40
41
42
# File 'lib/ims/lti/models/lti_model.rb', line 39

def self.inherited(subclass)
  subclass.inherit_attributes(self.attributes)
  super
end

Instance Method Details

#as_json(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ims/lti/models/lti_model.rb', line 70

def as_json(options = {})
  json_hash = attributes
  serialization_attrs_for(:json_key).each { |attr| json_hash.delete(attr.to_s) }
  serialization_attrs_for(:relation).each do |attr|
    val = attributes[attr.to_s]
    if val && val.is_a?(Array)
      json_hash[json_key(attr)] = val.map { |v| v.as_json }
    elsif val
      json_hash[json_key(attr)] = val.as_json
    end
  end
  json_hash = @unknown_attributes.merge(json_hash)
  json_hash = @ext_attributes.merge(json_hash)
  json_hash.merge! to_json_conversions
  json_hash.merge! to_json_keys
  json_hash
end

#attributesObject



48
49
50
51
52
53
54
55
# File 'lib/ims/lti/models/lti_model.rb', line 48

def attributes
  attrs = {}
  self.class.attributes.each do |a|
    value = instance_variable_get("@#{a.to_s}")
    attrs[a.to_s] = value unless value == nil
  end
  attrs
end

#attributes=(attrs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ims/lti/models/lti_model.rb', line 57

def attributes=(attrs)
  attrs.each do |k, v|
    if self.class.attributes.include?(k.to_sym)
      send(("#{k}=").to_sym, v)
    elsif k.to_s =~ /^ext_/
      @ext_attributes[k.to_sym] = v
    else
      warn("Unknown attribute '#{k}'")
      @unknown_attributes[k.to_sym] = v
    end
  end
end

#from_json(json) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/ims/lti/models/lti_model.rb', line 92

def from_json(json)
  # JSON.parse(json.to_json) is a quick and dirty way to clone the json object passed in
  data = json.is_a?(String) ? JSON.parse(json) : JSON.parse(json.to_json)
  if data.is_a? Array
    data.map { |hash| self.class.from_json(hash.to_json) }
  else
    process_json_hash(data)
  end
end

#to_jsonObject



88
89
90
# File 'lib/ims/lti/models/lti_model.rb', line 88

def to_json
  self.as_json.to_json
end