Class: SimpleAMS::Adapters::AMS

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_ams/adapters/ams.rb

Direct Known Subclasses

Collection

Defined Under Namespace

Classes: Collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ AMS

Returns a new instance of AMS.



6
7
8
9
# File 'lib/simple_ams/adapters/ams.rb', line 6

def initialize(document, options = {})
  @document = document
  @options = options
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



4
5
6
# File 'lib/simple_ams/adapters/ams.rb', line 4

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/simple_ams/adapters/ams.rb', line 4

def options
  @options
end

Instance Method Details

#as_jsonObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/simple_ams/adapters/ams.rb', line 11

def as_json
  return nil if document.resource.nil?

  hash = {}

  hash.merge!(fields)
  hash.merge!(relations) unless relations.empty?
  hash.merge!(links: links) unless links.empty?
  hash.merge!(metas: metas) unless metas.empty?
  hash.merge!(forms: forms) unless forms.empty?

  return {document.name => hash} if options[:root]
  return hash
end

#fieldsObject



26
27
28
29
30
31
# File 'lib/simple_ams/adapters/ams.rb', line 26

def fields
  @fields ||= document.fields.inject({}){ |hash, field|
    hash[field.key] = field.value
    hash
  }
end

#formsObject



49
50
51
52
53
54
# File 'lib/simple_ams/adapters/ams.rb', line 49

def forms
  @forms ||= document.forms.inject({}){ |hash, form|
    hash[form.name] = form.value
    hash
  }
end


33
34
35
36
37
38
39
40
# File 'lib/simple_ams/adapters/ams.rb', line 33

def links
  return @links ||= {} if document.links.empty?

  @links ||= document.links.inject({}){ |hash, link|
    hash[link.name] = link.value
    hash
  }
end

#metasObject



42
43
44
45
46
47
# File 'lib/simple_ams/adapters/ams.rb', line 42

def metas
  @metas ||= document.metas.inject({}){ |hash, meta|
    hash[meta.name] = meta.value
    hash
  }
end

#relationsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/simple_ams/adapters/ams.rb', line 56

def relations
  return @relations = {} if document.relations.available.empty?

  @relations ||= document.relations.available.inject({}){ |hash, relation|
    if relation.folder?
      value = relation.map{|doc| self.class.new(doc).as_json}
    else
      value = self.class.new(relation).as_json
    end
    hash[relation.name] = value

    hash
  }
end