Class: Prosereflect::Schema::MarkType
- Inherits:
-
Object
- Object
- Prosereflect::Schema::MarkType
- Defined in:
- lib/prosereflect/schema/mark_type.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#excluded ⇒ Object
Returns the value of attribute excluded.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Class Method Summary collapse
Instance Method Summary collapse
- #check_attrs(attrs) ⇒ Object
- #create(attrs = nil) ⇒ Object
- #excludes?(other_mark_type) ⇒ Boolean
- #inclusive? ⇒ Boolean
-
#initialize(name:, attrs: {}, rank: 0, schema: nil, spec: nil, inclusive: true) ⇒ MarkType
constructor
A new instance of MarkType.
- #instance ⇒ Object
- #is_in_set?(mark_set) ⇒ Boolean
- #remove_from_set(mark_set) ⇒ Object
Constructor Details
#initialize(name:, attrs: {}, rank: 0, schema: nil, spec: nil, inclusive: true) ⇒ MarkType
Returns a new instance of MarkType.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/prosereflect/schema/mark_type.rb', line 9 def initialize(name:, attrs: {}, rank: 0, schema: nil, spec: nil, inclusive: true) @name = name @attrs = attrs @rank = rank @schema = schema @spec = spec @inclusive = inclusive @excluded = [] end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
6 7 8 |
# File 'lib/prosereflect/schema/mark_type.rb', line 6 def attrs @attrs end |
#excluded ⇒ Object
Returns the value of attribute excluded.
7 8 9 |
# File 'lib/prosereflect/schema/mark_type.rb', line 7 def excluded @excluded end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/prosereflect/schema/mark_type.rb', line 6 def name @name end |
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
6 7 8 |
# File 'lib/prosereflect/schema/mark_type.rb', line 6 def rank @rank end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
6 7 8 |
# File 'lib/prosereflect/schema/mark_type.rb', line 6 def schema @schema end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
6 7 8 |
# File 'lib/prosereflect/schema/mark_type.rb', line 6 def spec @spec end |
Class Method Details
.from_spec(name, rank, schema, spec) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 |
# File 'lib/prosereflect/schema/mark_type.rb', line 20 def self.from_spec(name, rank, schema, spec) attrs = {} if spec.respond_to?(:attrs) spec_attrs = spec.attrs attrs = if spec_attrs.is_a?(Hash) # New format: {"attr_name" => {default: value}} spec_attrs.each_with_object({}) do |(attr_name, attr_spec), hash| if attr_spec.respond_to?(:name) hash[attr_name] = Attribute.new(name: attr_spec.name, default: attr_spec.default, validate: nil) else # attr_spec is a Hash attr_name = attr_spec[:name] || attr_spec["name"] || attr_name default = attr_spec[:default] || attr_spec["default"] hash[attr_name] = Attribute.new(name: attr_name, default: default, validate: nil) end end else # Old format: values are attribute objects spec_attrs.transform_values do |a| Attribute.new(name: a.name, default: a.default, validate: nil) end end elsif spec.is_a?(Hash) spec_attrs = spec[:attrs] || spec["attrs"] || {} attrs = spec_attrs.transform_values do |v| Attribute.new(name: v[:name] || v["name"], default: v[:default] || v["default"]) end end new( name: name, attrs: attrs, rank: rank, schema: schema, spec: spec, inclusive: if spec.respond_to?(:inclusive) spec.inclusive elsif spec.key?(:inclusive) spec[:inclusive] elsif spec.key?("inclusive") spec["inclusive"] else true end, ) end |
Instance Method Details
#check_attrs(attrs) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/prosereflect/schema/mark_type.rb', line 87 def check_attrs(attrs) attrs ||= {} attrs.each do |attr_name, value| unless @attrs.key?(attr_name) raise Prosereflect::SchemaErrors::ValidationError, "Unsupported attribute #{attr_name} for mark #{@name}" end attr_def = @attrs[attr_name] attr_def&.validate_value(value) end end |
#create(attrs = nil) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/prosereflect/schema/mark_type.rb', line 72 def create(attrs = nil) return instance if attrs.nil? && @instance computed_attrs = compute_attrs(attrs) Mark.new(type: self, attrs: computed_attrs) end |
#excludes?(other_mark_type) ⇒ Boolean
100 101 102 |
# File 'lib/prosereflect/schema/mark_type.rb', line 100 def excludes?(other_mark_type) @excluded.include?(other_mark_type) end |
#inclusive? ⇒ Boolean
104 105 106 |
# File 'lib/prosereflect/schema/mark_type.rb', line 104 def inclusive? @inclusive end |
#instance ⇒ Object
108 109 110 |
# File 'lib/prosereflect/schema/mark_type.rb', line 108 def instance @instance ||= create(nil) end |
#is_in_set?(mark_set) ⇒ Boolean
83 84 85 |
# File 'lib/prosereflect/schema/mark_type.rb', line 83 def is_in_set?(mark_set) mark_set.any? { |m| m.type == self } end |
#remove_from_set(mark_set) ⇒ Object
79 80 81 |
# File 'lib/prosereflect/schema/mark_type.rb', line 79 def remove_from_set(mark_set) mark_set.reject { |m| m.type == self } end |