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
25
# 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]

  hash
end

#fieldsObject



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

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

#formsObject



47
48
49
50
51
# File 'lib/simple_ams/adapters/ams.rb', line 47

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


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

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

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

#metasObject



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

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

#relationsObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/simple_ams/adapters/ams.rb', line 53

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

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