Class: Textus::Schema
- Inherits:
-
Object
- Object
- Textus::Schema
- Defined in:
- lib/textus/schema.rb,
lib/textus/schema/tools.rb
Defined Under Namespace
Modules: Tools
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Class Method Summary collapse
Instance Method Summary collapse
- #evolution ⇒ Object
-
#initialize(raw) ⇒ Schema
constructor
A new instance of Schema.
- #maintained_by(field) ⇒ Object
- #to_h ⇒ Object
-
#validate!(frontmatter) ⇒ Object
Returns nil on success; raises SchemaViolation on hard failure.
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
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/textus/schema.rb', line 5 def fields @fields end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/textus/schema.rb', line 5 def name @name end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
5 6 7 |
# File 'lib/textus/schema.rb', line 5 def optional @optional end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
5 6 7 |
# File 'lib/textus/schema.rb', line 5 def raw @raw end |
#required ⇒ Object (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
#evolution ⇒ Object
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) = @fields[field] or return nil ["maintained_by"] end |
#to_h ⇒ Object
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.
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 |