Class: JSI::Schema::Dialect

Inherits:
Object
  • Object
show all
Defined in:
lib/jsi/schema/dialect.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, vocabularies:, **conf) ⇒ Dialect

Returns a new instance of Dialect.

Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jsi/schema/dialect.rb', line 35

def initialize(id: nil, vocabularies: , **conf)
  @id = Util.uri(id, nnil: false, yabs: true, ynorm: true)
  @vocabularies = Set.new(vocabularies).freeze
  @conf = conf.freeze

  elements = vocabularies.map(&:elements).inject(Set.new, &:merge)

  # key element depends on each element of its value
  dependencies = Hash.new { |h, k| h[k] = Set[] }
  elements.each do |element|
    element.select_elements_self_is_required_before(elements).each do |required_before_element|
      # element will be invoked before required_before_element
      dependencies[required_before_element] << element
    end

    element.select_elements_self_depends_on(elements).each do |depends_on_element|
      # element will be invoked after depends_on_element
      dependencies[element] << depends_on_element
    end
  end

  @elements = []

  until elements.empty?
    sort_element = elements.detect do |element|
      dependencies[element].all? { |req_el| @elements.include?(req_el) }
    end || fail(Bug)
    @elements.push(sort_element)
    elements.delete(sort_element)
  end

  @elements.freeze

  @actions = Hash.new(Util::EMPTY_ARY)
  action_names = @elements.map { |e| e.actions.each_key }.inject(Set.new, &:merge).freeze
  action_names.each do |action_name|
    @actions[action_name] = @elements.map { |e| e.actions[action_name] }.inject([], &:concat).freeze
  end
  @actions.freeze

  # Bootstrap schemas are memoized in nested hashes.
  # The outer hash is keyed by document, compared by identity, because hashing the document
  # is expensive and there aren't typically multiple instances of the same document
  # (and if there are, it is no problem for them to map to different bootstrap schemas).
  # The inner hash is keyed by other keyword params to MetaSchemaNode::BootstrapSchema#initialize,
  # not by identity, as those use different instances but are cheaper to hash.
  @bootstrap_schema_map = Hash.new do |dochash, document|
    dochash[document] = Hash.new do |paramhash, kw|
      #chkbug fail if kw[:dialect]
      paramhash[kw] = MetaSchemaNode::BootstrapSchema.new(dialect: self, jsi_document: document, **kw)
    end
  end
  @bootstrap_schema_map.compare_by_identity

  freeze
end

Instance Attribute Details

#confHash (readonly)

Returns:

  • (Hash)


99
100
101
# File 'lib/jsi/schema/dialect.rb', line 99

def conf
  @conf
end

#elementsArray<Schema::Element> (readonly)

Returns:



102
103
104
# File 'lib/jsi/schema/dialect.rb', line 102

def elements
  @elements
end

#idURI? (readonly)

Returns:

  • (URI, nil)


93
94
95
# File 'lib/jsi/schema/dialect.rb', line 93

def id
  @id
end

#vocabulariesSet<Schema::Vocabulary> (readonly)

Returns:



96
97
98
# File 'lib/jsi/schema/dialect.rb', line 96

def vocabularies
  @vocabularies
end

Class Method Details

.from_xvocabulary(xvocabulary, id: nil, registry: JSI.registry, register: !!id,, **conf) ⇒ Schema::Dialect

Parameters:

  • xvocabulary (#to_hash)

    $vocabulary keyword value

  • id (#to_str, nil) (defaults to: nil)
  • registry (Registry) (defaults to: JSI.registry)
  • register (Boolean) (defaults to: !!id,)

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jsi/schema/dialect.rb', line 12

def from_xvocabulary(
    xvocabulary,
    id: nil,
    registry: JSI.registry,
    register: !!id,
    **conf
)
  vocabularies = []
  xvocabulary.each do |vocabulary_uri, required|
    if required || registry.vocabulary_registered?(vocabulary_uri)
      vocabularies << registry.find_vocabulary(vocabulary_uri)
    end
  end
  dialect = Schema::Dialect.new(id: id, vocabularies: vocabularies, **conf)
  registry.register_dialect(dialect) if register
  dialect
end

Instance Method Details

#bootstrap_schema(jsi_document:, **kw) ⇒ MetaSchemaNode::BootstrapSchema

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



106
107
108
# File 'lib/jsi/schema/dialect.rb', line 106

def bootstrap_schema(jsi_document: , **kw)
  @bootstrap_schema_map[jsi_document][kw]
end

#invoke(action_name, cxt) ⇒ Object

Invoke the indicated action of each Element on the given context

Parameters:

  • action_name (Symbol)
  • cxt (Schema::Cxt)

    the self of the action

Returns:

  • given cxt



114
115
116
117
118
119
120
121
# File 'lib/jsi/schema/dialect.rb', line 114

def invoke(action_name, cxt)
  @actions[action_name].each do |action|
      cxt.instance_exec(&action)
      return(cxt) if cxt.abort
  end

  cxt
end

#pretty_print(q) ⇒ Object

Parameters:

  • q


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jsi/schema/dialect.rb', line 124

def pretty_print(q)
  if id
    jsi_pp_object_group(q, [self.class.name, -"id: <#{id}>"])
  else
    jsi_pp_object_group(q) do
      q.seplist(vocabularies) do |v|
        q.pp(v)
      end
    end
  end
end