Class: Prosereflect::Schema::Mark
- Inherits:
-
Object
- Object
- Prosereflect::Schema::Mark
- Defined in:
- lib/prosereflect/schema/mark.rb
Overview
Lightweight Mark class for schema validation
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .none ⇒ Object
-
.same_set(a, b) ⇒ Object
Class method for comparing two mark sets Returns true if both sets contain the same marks in the same order.
- .set_from(marks) ⇒ Object
- .sort_marks(marks) ⇒ Object
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#add_to_set(mark_set) ⇒ Object
Add this mark to a set, respecting exclusion rules and rank ordering Follows prosemirror-py Mark.add_to_set logic.
- #eq?(other) ⇒ Boolean
-
#initialize(type:, attrs: {}) ⇒ Mark
constructor
A new instance of Mark.
- #is_in_set?(mark_set) ⇒ Boolean
- #length ⇒ Object
- #remove_from_set(mark_set) ⇒ Object
- #same_set?(other_marks) ⇒ Boolean
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(type:, attrs: {}) ⇒ Mark
Returns a new instance of Mark.
9 10 11 12 |
# File 'lib/prosereflect/schema/mark.rb', line 9 def initialize(type:, attrs: {}) @type = type @attrs = attrs || {} end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
7 8 9 |
# File 'lib/prosereflect/schema/mark.rb', line 7 def attrs @attrs end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/prosereflect/schema/mark.rb', line 7 def type @type end |
Class Method Details
.none ⇒ Object
94 95 96 |
# File 'lib/prosereflect/schema/mark.rb', line 94 def none @none ||= [] end |
.same_set(a, b) ⇒ Object
Class method for comparing two mark sets Returns true if both sets contain the same marks in the same order
104 105 106 107 108 109 110 111 |
# File 'lib/prosereflect/schema/mark.rb', line 104 def same_set(a, b) return true if a == b return false unless a.length == b.length a.each_with_index.all? do |mark_a, i| mark_a.eq?(b[i]) end end |
.set_from(marks) ⇒ Object
88 89 90 91 92 |
# File 'lib/prosereflect/schema/mark.rb', line 88 def set_from(marks) return none if marks.nil? || marks.empty? marks.filter_map { |m| m.is_a?(Mark) ? m : m } end |
.sort_marks(marks) ⇒ Object
98 99 100 |
# File 'lib/prosereflect/schema/mark.rb', line 98 def sort_marks(marks) marks.sort_by { |m| m.type.rank } end |
Instance Method Details
#[](index) ⇒ Object
26 27 28 |
# File 'lib/prosereflect/schema/mark.rb', line 26 def [](index) @type.is_a?(Array) ? @type[index] : self end |
#add_to_set(mark_set) ⇒ Object
Add this mark to a set, respecting exclusion rules and rank ordering Follows prosemirror-py Mark.add_to_set logic
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 |
# File 'lib/prosereflect/schema/mark.rb', line 40 def add_to_set(mark_set) copy = nil placed = false mark_set.each_with_index do |other, i| if eq?(other) # Self already in set, return unchanged return mark_set end if @type.excludes?(other.type) # This mark's type excludes the other's type # Remove the other from the result by starting copy copy ||= mark_set[0...i] elsif other.type.excludes?(@type) # Other's type excludes this mark's type - don't add return mark_set else # No exclusion - insert before if other's rank is higher and not yet placed if !placed && other.type.rank > @type.rank copy ||= mark_set[0...i] copy << self placed = true end copy << other if copy end end copy ||= mark_set.dup copy << self unless placed sort_marks(copy) end |
#eq?(other) ⇒ Boolean
14 15 16 17 18 |
# File 'lib/prosereflect/schema/mark.rb', line 14 def eq?(other) return false unless other.is_a?(Mark) @type == other.type && @attrs == other.attrs end |
#is_in_set?(mark_set) ⇒ Boolean
34 35 36 |
# File 'lib/prosereflect/schema/mark.rb', line 34 def is_in_set?(mark_set) mark_set.any? { |m| m.type == @type } end |
#length ⇒ Object
30 31 32 |
# File 'lib/prosereflect/schema/mark.rb', line 30 def length 1 end |
#remove_from_set(mark_set) ⇒ Object
73 74 75 |
# File 'lib/prosereflect/schema/mark.rb', line 73 def remove_from_set(mark_set) mark_set.reject { |m| m.type == @type } end |
#same_set?(other_marks) ⇒ Boolean
20 21 22 23 24 |
# File 'lib/prosereflect/schema/mark.rb', line 20 def same_set?(other_marks) return false unless other_marks.length == length other_marks.each_with_index.all? { |m, i| self[i].eq?(m) } end |
#to_h ⇒ Object
77 78 79 80 81 |
# File 'lib/prosereflect/schema/mark.rb', line 77 def to_h result = { "type" => @type.name } result["attrs"] = @attrs unless @attrs.empty? result end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/prosereflect/schema/mark.rb', line 83 def to_s "<Mark #{@type.name}>" end |