Class: Shaf::Profile

Inherits:
Object
  • Object
show all
Extended by:
NormalizeName
Includes:
NormalizeName, UriHelper
Defined in:
lib/shaf/profile.rb,
lib/shaf/profile/relation.rb,
lib/shaf/profile/attribute.rb,
lib/shaf/profile/evaluator.rb,
lib/shaf/profile/unique_id.rb

Defined Under Namespace

Modules: NormalizeName, UniqueId Classes: Attribute, Evaluator, Relation

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UriHelper

base_uri, included

Methods included from UriHelperMethods

add_path_helpers, eval_method, path_helpers, #path_helpers, path_helpers_for, register, remove_all

Class Method Details

.attribute(*args, **kwargs, &block) ⇒ Object



58
59
60
# File 'lib/shaf/profile.rb', line 58

def attribute(*args, **kwargs, &block)
  evaluator.attribute(*args, **kwargs, &block)
end

.attributesObject



46
47
48
# File 'lib/shaf/profile.rb', line 46

def attributes
  @attributes ||= []
end

.descriptor(id) ⇒ Object



67
68
69
# File 'lib/shaf/profile.rb', line 67

def descriptor(id)
  find_attribute(id) || find_relation(id)
end

.doc(str = nil) ⇒ Object



28
29
30
31
# File 'lib/shaf/profile.rb', line 28

def doc(str = nil)
  @doc = str if str
  @doc if defined? @doc
end

.example(str) ⇒ Object



38
39
40
# File 'lib/shaf/profile.rb', line 38

def example(str)
  examples << str
end

.examplesObject



54
55
56
# File 'lib/shaf/profile.rb', line 54

def examples
  @examples ||= []
end

.find_attribute(id) ⇒ Object



71
72
73
# File 'lib/shaf/profile.rb', line 71

def find_attribute(id)
  attributes.find { |attr| attr.id.to_sym == id.to_sym }
end

.find_relation(id) ⇒ Object



75
76
77
# File 'lib/shaf/profile.rb', line 75

def find_relation(id)
  relations.find { |rel| rel.id.to_sym == id.to_sym }
end

.inherited(child) ⇒ Object



19
20
21
# File 'lib/shaf/profile.rb', line 19

def inherited(child)
  Profiles.register child
end

.match?(str) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/shaf/profile.rb', line 42

def match?(str)
  normalize(name) == normalize(str)
end

.name(str = nil) ⇒ Object



23
24
25
26
# File 'lib/shaf/profile.rb', line 23

def name(str = nil)
  @name = str if str
  @name if defined? @name
end

.relation(*args, **kwargs, &block) ⇒ Object Also known as: rel



62
63
64
# File 'lib/shaf/profile.rb', line 62

def relation(*args, **kwargs, &block)
  evaluator.rel(*args, **kwargs, &block)
end

.relationsObject



50
51
52
# File 'lib/shaf/profile.rb', line 50

def relations
  @relations ||= []
end

.urn(value = nil) ⇒ Object



33
34
35
36
# File 'lib/shaf/profile.rb', line 33

def urn(value = nil)
  @urn = value if value
  @urn if defined? @urn
end

.use(*descriptors, from:, doc: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/shaf/profile.rb', line 79

def use(*descriptors, from:, doc: nil)
  descriptors.each do |id|
    desc = from.descriptor(id)
    href = profile_path(from.name, fragment_id: id)

    case desc
    when Relation
      kwargs = {
        doc: doc || desc&.doc,
        href: href,
        http_methods: desc.http_methods,
        payload_type: desc.payload_type,
        content_type: desc.content_type,
      }
      relation(id, **kwargs)
    when Attribute
      attribute(id, href: href, doc: doc)
    when NilClass
      raise "#{from.name} does not have a descriptor with id #{id}"
    else
      raise Errors::ServerError, "Unsupported descriptor: #{desc}"
    end
  end
end

Instance Method Details

#nameObject



111
112
113
# File 'lib/shaf/profile.rb', line 111

def name
  normalize(self.class.name)
end