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.



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

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/ims/lti/models/lti_model.rb', line 109

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



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

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



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

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



46
47
48
# File 'lib/ims/lti/models/lti_model.rb', line 46

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

.inherit_attributes(attrs) ⇒ Object



36
37
38
39
# File 'lib/ims/lti/models/lti_model.rb', line 36

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

.inherited(subclass) ⇒ Object



41
42
43
44
# File 'lib/ims/lti/models/lti_model.rb', line 41

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

Instance Method Details

#as_json(options = {}) ⇒ Object



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

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



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

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



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

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ims/lti/models/lti_model.rb', line 94

def from_json(json)
  json = json.to_json unless json.is_a?(String)
  begin
    data = JSON.parse(json)
  rescue
    data = JSON.parse(URI::DEFAULT_PARSER.unescape(json))
  end

  if data.is_a? Array
    data.map { |hash| self.class.from_json(hash.to_json) }
  else
    process_json_hash(data)
  end
end

#to_jsonObject



90
91
92
# File 'lib/ims/lti/models/lti_model.rb', line 90

def to_json
  self.as_json.to_json
end