Class: Prosereflect::Schema::MarkType

Inherits:
Object
  • Object
show all
Defined in:
lib/prosereflect/schema/mark_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attrsObject (readonly)

Returns the value of attribute attrs.



6
7
8
# File 'lib/prosereflect/schema/mark_type.rb', line 6

def attrs
  @attrs
end

#excludedObject

Returns the value of attribute excluded.



7
8
9
# File 'lib/prosereflect/schema/mark_type.rb', line 7

def excluded
  @excluded
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/prosereflect/schema/mark_type.rb', line 6

def name
  @name
end

#rankObject (readonly)

Returns the value of attribute rank.



6
7
8
# File 'lib/prosereflect/schema/mark_type.rb', line 6

def rank
  @rank
end

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/prosereflect/schema/mark_type.rb', line 6

def schema
  @schema
end

#specObject (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

Returns:

  • (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

Returns:

  • (Boolean)


104
105
106
# File 'lib/prosereflect/schema/mark_type.rb', line 104

def inclusive?
  @inclusive
end

#instanceObject



108
109
110
# File 'lib/prosereflect/schema/mark_type.rb', line 108

def instance
  @instance ||= create(nil)
end

#is_in_set?(mark_set) ⇒ Boolean

Returns:

  • (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