Class: Textus::Protocol::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/protocol/schema.rb,
lib/textus/protocol/schema/tools.rb,
lib/textus/protocol/schema/registry.rb

Defined Under Namespace

Modules: Tools Classes: Registry

Constant Summary collapse

TYPE_VALIDATORS =
{
  "string" => :validate_string,
  "number" => :validate_number,
  "boolean" => :validate_boolean,
  "enum" => :validate_enum,
  "array" => :validate_array,
  "object" => :validate_object,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Schema

Returns a new instance of Schema.



13
14
15
16
17
18
19
# File 'lib/textus/protocol/schema.rb', line 13

def initialize(raw)
  @raw = raw || {}
  @name = @raw["name"]
  @required = Array(@raw["required"])
  @optional = Array(@raw["optional"])
  @fields = @raw["fields"] || {}
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/textus/protocol/schema.rb', line 6

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/textus/protocol/schema.rb', line 6

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



6
7
8
# File 'lib/textus/protocol/schema.rb', line 6

def optional
  @optional
end

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/textus/protocol/schema.rb', line 6

def raw
  @raw
end

#requiredObject (readonly)

Returns the value of attribute required.



6
7
8
# File 'lib/textus/protocol/schema.rb', line 6

def required
  @required
end

Class Method Details

.load(path) ⇒ Object



8
9
10
11
# File 'lib/textus/protocol/schema.rb', line 8

def self.load(path)
  raw = YAML.safe_load_file(path, permitted_classes: [Symbol, Date, Time], aliases: false)
  new(raw)
end

Instance Method Details

#evolutionObject



38
39
40
41
42
43
# File 'lib/textus/protocol/schema.rb', line 38

def evolution
  raw = @raw["evolution"] || {}
  raw.each_with_object({}) do |(k, v), h|
    h[k] = v.is_a?(Date) || v.is_a?(Time) ? v.to_s : v
  end
end

#maintained_by(field) ⇒ Object



25
26
27
28
# File 'lib/textus/protocol/schema.rb', line 25

def maintained_by(field)
  meta = @fields[field] or return nil
  meta["maintained_by"]
end

#to_hObject



21
22
23
# File 'lib/textus/protocol/schema.rb', line 21

def to_h
  @raw
end

#unowned_fieldsObject



30
31
32
33
34
35
36
# File 'lib/textus/protocol/schema.rb', line 30

def unowned_fields
  @fields.each_with_object([]) do |(name, spec), acc|
    next unless spec.is_a?(Hash)

    acc << name if spec["maintained_by"].nil?
  end
end

#validate!(frontmatter) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/textus/protocol/schema.rb', line 45

def validate!(frontmatter)
  missing = @required - frontmatter.keys
  raise Textus::SchemaViolation.new("missing" => missing) unless missing.empty?

  known = (@required + @optional).uniq
  frontmatter.each do |k, v|
    next unless @fields.key?(k)

    check_type!(k, v, @fields[k])
  end

  warnings = frontmatter.keys - known
  warnings.map { |w| "unknown field: #{w}" }
end