Class: Mumukit::Service::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/service/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Document

Returns a new instance of Document.



6
7
8
# File 'lib/mumukit/service/document.rb', line 6

def initialize(json)
  @raw = json.to_h.symbolize_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/mumukit/service/document.rb', line 14

def method_missing(name, *args)
  if args.size == 0
    self[name]
  elsif args.size == 1
    self[name[0..-2]] = args.first
  else
    super
  end
end

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



3
4
5
# File 'lib/mumukit/service/document.rb', line 3

def raw
  @raw
end

Instance Method Details

#[]=(key, value) ⇒ Object



24
25
26
27
# File 'lib/mumukit/service/document.rb', line 24

def []=(key, value)
  @raw[key.to_sym] = value
  @json = nil
end

#as_json(options = {}) ⇒ Object



10
11
12
# File 'lib/mumukit/service/document.rb', line 10

def as_json(options={})
  json.as_json(options)
end

#defaultsObject



33
34
35
# File 'lib/mumukit/service/document.rb', line 33

def defaults
  {}
end

#errorsObject



37
38
39
# File 'lib/mumukit/service/document.rb', line 37

def errors
  []
end

#jsonObject



46
47
48
49
50
# File 'lib/mumukit/service/document.rb', line 46

def json
  @json ||= defaults.
      merge(@raw).
      merge(transforms(@raw))
end

#transforms(original) ⇒ Object



29
30
31
# File 'lib/mumukit/service/document.rb', line 29

def transforms(original)
  {}
end

#validate!Object



41
42
43
44
# File 'lib/mumukit/service/document.rb', line 41

def validate!
  e = errors
  raise DocumentValidationError, e.join(', ') unless e.empty?
end