class Environmentor::Schema

Attributes

attrs[R]
children[R]
defined_at[RW]
name[R]
opts[R]
parent[R]

Public Class Methods

new(mappers, parent = nil, name = nil, **opts, &block) click to toggle source
# File lib/environmentor/schema.rb, line 9
def initialize(mappers, parent = nil, name = nil, **opts, &block)
  @parent = parent
  @name = name
  if parent && !name
    raise ArgumentError, "Namespace defined without name!"
  end

  @namespace_chain = build_namespace_chain
  @mappers = mappers
  @opts = opts
  @attrs = {}
  @children = []
  @defined_at = opts[:defined_at] || caller.first

  if block_given?
    instance_exec(&block)
    validate!
  end
end

Public Instance Methods

absent() click to toggle source
# File lib/environmentor/schema.rb, line 91
def absent
  ::Environmentor::Attribute::Absent
end
attr_config(name, **opts) click to toggle source
# File lib/environmentor/schema.rb, line 29
def attr_config(name, **opts)
  attrs[name] = Attribute.new(name, @namespace_chain, **opts)
  nil
end
get_attr(attr) click to toggle source
# File lib/environmentor/schema.rb, line 87
def get_attr(attr)
  attr.get_from_mappers(@mappers)
end
map_to(mod) click to toggle source
# File lib/environmentor/schema.rb, line 60
def map_to(mod)
  schema = self
  attrs.each do |name, attr|
    mod.__send__(:define_method, name) do
      schema.get_attr attr
    end

    mod.__send__(:define_singleton_method, name) do
      schema.get_attr attr
    end
  end

  @children.each do |s|
    next unless s.name
    ns_mod = Module.new
    s.map_to ns_mod

    mod.__send__(:define_method, s.name) do
      ns_mod
    end

    mod.__send__(:define_singleton_method, s.name) do
      ns_mod
    end
  end
end
namespace(name, &block) click to toggle source
# File lib/environmentor/schema.rb, line 34
def namespace(name, &block)
  opts = @opts.merge(defined_at: caller.first)
  @children << Schema.new(@mappers, self, name, **opts).tap { |s|
    s.instance_exec(&block)
  }
  nil
end
validate() click to toggle source
# File lib/environmentor/schema.rb, line 42
def validate
  Attribute::ValidationErrors.new.tap do |errors|
    attrs.each do |_, attr|
      errors.concat validate_attr(attr)
    end

    @children.each do |schema|
      errors.concat schema.validate
    end
  end
end
validate!() click to toggle source
# File lib/environmentor/schema.rb, line 54
def validate!
  validate.tap do |errors|
    handle_attr_errors(errors) unless errors.success?
  end
end

Protected Instance Methods

handle_attr_errors(errors) click to toggle source

:nocov:

# File lib/environmentor/schema.rb, line 102
def handle_attr_errors(errors)
  # TODO: maybe make customisable somehow. At least this is overridable
  # in isolation.
  puts "#{$0}: fatal: failed to validate external config defined at"
  puts defined_at << ":"
  errors.each do |e|
    puts "* " << e.message
  end
  exit 1
end
validate_attr(attr) click to toggle source
# File lib/environmentor/schema.rb, line 97
def validate_attr(attr)
  attr.validate_in_mappers(@mappers)
end

Private Instance Methods

build_namespace_chain() click to toggle source

:nocov:

# File lib/environmentor/schema.rb, line 116
def build_namespace_chain
  s = self
  [].tap do |out|
    while s
      out.unshift s
      s = s.parent
    end
  end
end