Class: Shaf::Profile::Evaluator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent:, allowed: nil) ⇒ Evaluator

Returns a new instance of Evaluator.



11
12
13
14
# File 'lib/shaf/profile/evaluator.rb', line 11

def initialize(parent:, allowed: nil)
  @parent = parent
  @allowed = allowed && Array(allowed).map(&:to_sym)
end

Instance Attribute Details

#allowedObject (readonly)

Returns the value of attribute allowed.



9
10
11
# File 'lib/shaf/profile/evaluator.rb', line 9

def allowed
  @allowed
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/shaf/profile/evaluator.rb', line 9

def parent
  @parent
end

Instance Method Details

#attribute(name, doc:, type: :string, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/shaf/profile/evaluator.rb', line 16

def attribute(name, doc:, type: :string, &block)
  return unless allow? :attribute

  attr = Attribute.new(name, doc: doc, type: type, parent: parent)
  self.class.new(parent: attr, allowed: allowed).instance_exec(&block) if block
  parent.attributes << attr
end

#relation(name, **kwargs, &block) ⇒ Object Also known as: rel



24
25
26
27
28
29
30
# File 'lib/shaf/profile/evaluator.rb', line 24

def relation(name, **kwargs, &block)
  return unless allow? :rel

  rel = Relation.new(name, parent: parent, **kwargs)
  self.class.new(parent: rel, allowed: [:attribute]).instance_exec(&block) if block
  parent.relations << rel
end