Class: Skemata::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/skemata/node.rb

Constant Summary collapse

ALLOWED_OPTS =
%i[type root_object].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Node

Prepares internal data hash and assigns locals

Parameters:

  • opts (defaults to: {})

    = {} [Hash] See Skemata::DSL.draw for valid opts



12
13
14
15
16
# File 'lib/skemata/node.rb', line 12

def initialize(opts = {})
  ALLOWED_OPTS.each { |o| instance_variable_set("@#{o}", opts[o]) }
  @data = { '@type' => type }
  @data['@context'] = 'https://schema.org' if opts.fetch(:is_root, true)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



32
33
34
# File 'lib/skemata/node.rb', line 32

def data
  @data
end

Instance Method Details

#decorate(name, *args, &block) ⇒ Object

Decorate the node with a new property (as delegated by method_missing)

Parameters:

  • name (Symbol)

    Key name

  • *args (Array)

    Varargs for attributes describing key

  • &block (Proc)

    Body for a child node, if present



25
26
27
28
29
30
# File 'lib/skemata/node.rb', line 25

def decorate(name, *args, &block)
  # Draw another node
  return route_block(name, *args, &block) if block.present?
  # Or populate the hash
  data[attify_token(name)] = extract(args.first || name.to_sym)
end