Class: Prosereflect::Schema
- Inherits:
-
Object
- Object
- Prosereflect::Schema
- Defined in:
- lib/prosereflect/schema.rb,
lib/prosereflect/schema/mark.rb,
lib/prosereflect/schema/node.rb,
lib/prosereflect/schema/spec.rb,
lib/prosereflect/schema/fragment.rb,
lib/prosereflect/schema/attribute.rb,
lib/prosereflect/schema/mark_type.rb,
lib/prosereflect/schema/node_type.rb,
lib/prosereflect/schema/schema_main.rb,
lib/prosereflect/schema/content_match.rb
Defined Under Namespace
Classes: Attribute, ContentMatch, Fragment, Mark, MarkSpec, MarkType, MatchEdge, Node, NodeSpec, NodeType, SchemaSpec, TextNode
Constant Summary collapse
- ContentMatchError =
Alias ContentMatchError for backwards compatibility
Prosereflect::SchemaErrors::ContentMatchError
- Error =
Alias Error for backwards compatibility
Prosereflect::SchemaErrors::Error
- ValidationError =
Alias ValidationError for backwards compatibility
Prosereflect::SchemaErrors::ValidationError
Class Attribute Summary collapse
-
.mark_types ⇒ Object
Returns the value of attribute mark_types.
-
.node_types ⇒ Object
Returns the value of attribute node_types.
Instance Attribute Summary collapse
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
-
#initialize(nodes_spec:, marks_spec: {}, top_node: nil) ⇒ Schema
constructor
A new instance of Schema.
-
#mark(type, attrs = nil) ⇒ Object
Create a mark.
-
#mark_from_json(json_data) ⇒ Object
Deserialize mark from JSON.
- #mark_type(name) ⇒ Object
-
#node(type, attrs = nil, content = nil, marks = nil) ⇒ Object
Create a node.
-
#node_from_json(json_data) ⇒ Object
Deserialize node from JSON.
- #node_type(name) ⇒ Object
-
#text(text, marks = nil) ⇒ Object
Create a text node.
- #top_node_type ⇒ Object
Constructor Details
#initialize(nodes_spec:, marks_spec: {}, top_node: nil) ⇒ Schema
Returns a new instance of Schema.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/prosereflect/schema/schema_main.rb', line 11 def initialize(nodes_spec:, marks_spec: {}, top_node: nil) @spec = SchemaSpec.from_hashes(nodes_spec: nodes_spec, marks_spec: marks_spec, top_node: top_node) @nodes = {} # name -> NodeType @marks = {} # name -> MarkType # Build NodeTypes @spec.nodes.each do |name, node_spec| @nodes[name] = NodeType.from_spec(name, self, node_spec) end # Build MarkTypes rank = 0 @spec.marks.each do |name, mark_spec| @marks[name] = MarkType.from_spec(name, rank, self, mark_spec) rank += 1 end # Validate schema validate_schema # Build content expressions build_content_matches # Build mark sets build_mark_sets # Build mark exclusions build_mark_exclusions @top_node_type = @nodes[@spec.top_node] end |
Class Attribute Details
.mark_types ⇒ Object
Returns the value of attribute mark_types.
18 19 20 |
# File 'lib/prosereflect/schema.rb', line 18 def mark_types @mark_types end |
.node_types ⇒ Object
Returns the value of attribute node_types.
18 19 20 |
# File 'lib/prosereflect/schema.rb', line 18 def node_types @node_types end |
Instance Attribute Details
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
9 10 11 |
# File 'lib/prosereflect/schema/schema_main.rb', line 9 def marks @marks end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
9 10 11 |
# File 'lib/prosereflect/schema/schema_main.rb', line 9 def nodes @nodes end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
9 10 11 |
# File 'lib/prosereflect/schema/schema_main.rb', line 9 def spec @spec end |
Instance Method Details
#mark(type, attrs = nil) ⇒ Object
Create a mark
80 81 82 83 |
# File 'lib/prosereflect/schema/schema_main.rb', line 80 def mark(type, attrs = nil) type_obj = type.is_a?(String) ? mark_type(type) : type type_obj.create(attrs) end |
#mark_from_json(json_data) ⇒ Object
Deserialize mark from JSON
91 92 93 94 95 |
# File 'lib/prosereflect/schema/schema_main.rb', line 91 def mark_from_json(json_data) type = mark_type(json_data["type"]) attrs = json_data["attrs"] || {} type.create(attrs) end |
#mark_type(name) ⇒ Object
53 54 55 56 |
# File 'lib/prosereflect/schema/schema_main.rb', line 53 def mark_type(name) @marks[name] || raise(::Prosereflect::SchemaErrors::ValidationError, "Unknown mark type: #{name}") end |
#node(type, attrs = nil, content = nil, marks = nil) ⇒ Object
Create a node
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/prosereflect/schema/schema_main.rb', line 59 def node(type, attrs = nil, content = nil, marks = nil) type_obj = type.is_a?(String) ? node_type(type) : type unless type_obj.is_a?(NodeType) raise ::Prosereflect::SchemaErrors::Error, "Invalid node type: #{type}" end if type_obj.schema != self raise ::Prosereflect::SchemaErrors::Error, "Node type from different schema used (#{type_obj.name})" end type_obj.create_checked(attrs, content, marks) end |
#node_from_json(json_data) ⇒ Object
Deserialize node from JSON
86 87 88 |
# File 'lib/prosereflect/schema/schema_main.rb', line 86 def node_from_json(json_data) Node.from_json(self, json_data) end |
#node_type(name) ⇒ Object
48 49 50 51 |
# File 'lib/prosereflect/schema/schema_main.rb', line 48 def node_type(name) @nodes[name] || raise(::Prosereflect::SchemaErrors::ValidationError, "Unknown node type: #{name}") end |
#text(text, marks = nil) ⇒ Object
Create a text node
74 75 76 77 |
# File 'lib/prosereflect/schema/schema_main.rb', line 74 def text(text, marks = nil) type = node_type("text") TextNode.new(type: type, attrs: {}, text: text, marks: marks || []) end |
#top_node_type ⇒ Object
44 45 46 |
# File 'lib/prosereflect/schema/schema_main.rb', line 44 def top_node_type @top_node_type end |