Class: Shaf::ResourceDoc

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf/resource_doc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, properties = {}) ⇒ ResourceDoc

Returns a new instance of ResourceDoc.



32
33
34
35
36
37
38
# File 'lib/shaf/resource_doc.rb', line 32

def initialize(name, properties = {})
  @name       = name
  @attributes = properties.fetch('attributes', {})
  @links      = properties.fetch('links', {})
  @curies     = properties.fetch('curies', {})
  @embeds     = properties.fetch('embeds', {})
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



30
31
32
# File 'lib/shaf/resource_doc.rb', line 30

def attributes
  @attributes
end

#curiesObject (readonly)

Returns the value of attribute curies.



30
31
32
# File 'lib/shaf/resource_doc.rb', line 30

def curies
  @curies
end

#embedsObject (readonly)

Returns the value of attribute embeds.



30
31
32
# File 'lib/shaf/resource_doc.rb', line 30

def embeds
  @embeds
end

Returns the value of attribute links.



30
31
32
# File 'lib/shaf/resource_doc.rb', line 30

def links
  @links
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/shaf/resource_doc.rb', line 30

def name
  @name
end

Class Method Details

.find(name) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/shaf/resource_doc.rb', line 6

def find(name)
  unless docs[name]
    properties = load(name) or return
    docs[name] = new(name, properties)
  end
  docs[name]
end

.find!(name) ⇒ Object



14
15
16
# File 'lib/shaf/resource_doc.rb', line 14

def find!(name)
  find(name) or raise(Errors::NotFoundError, "No documentation for #{name}")
end

Instance Method Details

#attribute(attr) ⇒ Object



49
50
51
52
53
54
# File 'lib/shaf/resource_doc.rb', line 49

def attribute(attr)
  attr_doc = attributes[attr.to_s]
  return attr_doc if attr_doc
  raise Errors::NotFoundError,
    "No documentation for #{name} attribute '#{attr}'"
end

#embedded(name) ⇒ Object



63
64
65
66
67
68
# File 'lib/shaf/resource_doc.rb', line 63

def embedded(name)
  embed_doc = embeds[name.to_s]
  return embed_doc if embed_doc
  raise Errors::NotFoundError,
    "No documentation for #{name} embedded '#{name}'"
end


56
57
58
59
60
61
# File 'lib/shaf/resource_doc.rb', line 56

def link(rel)
  link_doc = links[rel.to_s]
  return link_doc if link_doc
  raise Errors::NotFoundError,
    "No documentation for #{name} link relation '#{rel}'"
end

#to_sObject



40
41
42
43
44
45
46
47
# File 'lib/shaf/resource_doc.rb', line 40

def to_s
  JSON.pretty_generate(
    attributes: attributes,
    links: links,
    curies: curies,
    embeds: embeds,
  )
end