Class: Textus::Schema

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

Defined Under Namespace

Modules: Tools

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Schema

Returns a new instance of Schema.



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

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.



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

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



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

def optional
  @optional
end

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

#requiredObject (readonly)

Returns the value of attribute required.



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

def required
  @required
end

Class Method Details

.load(path) ⇒ Object



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

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

Instance Method Details

#evolutionObject



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

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



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

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

#to_hObject



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

def to_h
  @raw
end

#validate!(frontmatter) ⇒ Object

Returns nil on success; raises SchemaViolation on hard failure. Unknown fields produce warnings, returned as a String[] alongside.

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/textus/schema.rb', line 38

def validate!(frontmatter)
  missing = @required - frontmatter.keys
  raise 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