class Crate::Crate
Constants
- FILTERS
- REJECT_METHODS
Attributes
_arrays[R]
_crate_klasses[R]
_crates[R]
_hooks[R]
_names[R]
_options[R]
_options[R]
_raw[R]
Public Class Methods
_basename()
click to toggle source
# File lib/crate/crate.rb, line 114 def _basename _options[:basename] end
_class(**options, &initializer)
click to toggle source
# File lib/crate/crate.rb, line 21 def _class(**options, &initializer) Class.new(self) do _initialize(**options) class_eval(&initializer) if block_given? end end
_declare_impl(name)
click to toggle source
# File lib/crate/crate.rb, line 74 def _declare_impl(name) _names.push(name) define_method(name) { _raw[name] } end
_initialize(**options)
click to toggle source
# File lib/crate/crate.rb, line 8 def _initialize(**options) @_names = [] @_crates = {} @_arrays = {} @_hooks = [] @_options = { logger: Logger.new(nil), **options } @_crate_klasses = [] end
_invalid_filter?(**options)
click to toggle source
# File lib/crate/crate.rb, line 61 def _invalid_filter?(**options) !(options.keys - FILTERS).empty? end
_make_args(hook_name, args)
click to toggle source
# File lib/crate/crate.rb, line 79 def _make_args(hook_name, args) # rubocop:disable Metrics/MethodLength args = [] if args == true args = Array(args) args, block = if args[-1].respond_to?(:to_proc) [args[0...-1], args[-1]] else [args, nil] end args.push({}) unless args[-1].is_a?(Hash) args[-1] = { tag: hook_name, **args[-1] } [args, block] end
_make_hooks(name, **options)
click to toggle source
# File lib/crate/crate.rb, line 93 def _make_hooks(name, **options) options.each do |hook_name, args| args, block = _make_args(hook_name, args) hook { method(:"_hook_#{hook_name}").call(name, *args, &block) } end end
array(*args, **options, &initializer)
click to toggle source
# File lib/crate/crate.rb, line 37 def array(*args, **options, &initializer) name, klass, = args declare(name) @_arrays[name] = klass || _class( **_options, basename: name.to_s, **options, &initializer ) end
check_undefined_keys(**options)
click to toggle source
# File lib/crate/crate.rb, line 104 def check_undefined_keys(**options) hook do method(:_hook_check_undefined_keys) \ .call(tag: 'check_undefined_keys', **options) end end
crate(*args, **options, &initializer)
click to toggle source
# File lib/crate/crate.rb, line 28 def crate(*args, **options, &initializer) name, klass, = args declare(name) @_crates[name] = klass || _class( **_options, basename: name.to_s, **options, &initializer ) end
declare(name, **options)
click to toggle source
# File lib/crate/crate.rb, line 65 def declare(name, **options) raise if _invalid_filter?(**options) return if self == ::Crate::Crate || \ name.nil? || method_defined?(name) _declare_impl(name) _make_hooks(name, **options) end
hook(&hook_)
click to toggle source
# File lib/crate/crate.rb, line 100 def hook(&hook_) _hooks.push(hook_) end
inherited(subclass)
click to toggle source
# File lib/crate/crate.rb, line 17 def inherited(subclass) subclass._initialize(**_options) end
new(raw, **options)
click to toggle source
# File lib/crate/crate.rb, line 142 def initialize(raw, **options) @_options = { **self.class._options, fullname: self.class._basename, **options } @_raw = Raw.new(raw, **_options).to_hash _new_crates _new_arrays _run_hooks _select_klass_if_exist(**options) end
version(*args_, &block)
click to toggle source
# File lib/crate/crate.rb, line 50 def version(*args_, &block) key, value, = *args_ block ||= proc { |raw| raw[key] == value } (class << self; self end) \ .send(:define_method, :_valid_crate?) do |*args| block.call(*args) end end
versions(*crate_klasses)
click to toggle source
# File lib/crate/crate.rb, line 46 def versions(*crate_klasses) @_crate_klasses = crate_klasses end
Public Instance Methods
_array_name(name, index)
click to toggle source
# File lib/crate/crate.rb, line 136 def _array_name(name, index) "#{_name(name)}[#{index}]" end
_crate_serialize()
click to toggle source
# File lib/crate/crate.rb, line 208 def _crate_serialize Hash[_raw.map { |key, value| [key, value._crate_serialize] }] end
_exit_if_no_klass(klass)
click to toggle source
# File lib/crate/crate.rb, line 187 def _exit_if_no_klass(klass) return unless klass.nil? _log(**_options, name: _fullname, tag: 'version', severity: Logger::FATAL) do 'must find valid version but did not' end exit 1 end
_find_valid_klass(*args)
click to toggle source
# File lib/crate/crate.rb, line 179 def _find_valid_klass(*args) self.class._crate_klasses.find do |klass| klass._valid_crate?(*args) end end
_fullname()
click to toggle source
# File lib/crate/crate.rb, line 128 def _fullname _options[:fullname] end
_log(**options)
click to toggle source
# File lib/crate/crate.rb, line 121 def _log(**options) options[:logger].add(options[:severity]) do "#{options[:name]}: [#{options[:tag]}]" + \ (block_given? ? " #{yield}" : '') end end
_name(name)
click to toggle source
# File lib/crate/crate.rb, line 132 def _name(name) "#{_fullname}[#{name.inspect}]" end
_new_arrays()
click to toggle source
# File lib/crate/crate.rb, line 160 def _new_arrays self.class._arrays \ .select { |name, _klass| _raw.key?(name) } \ .each do |name, klass| _raw[name].map!.with_index do |raw, index| klass.new(raw, **_options, fullname: _array_name(name, index)) end end end
_new_crates()
click to toggle source
# File lib/crate/crate.rb, line 152 def _new_crates self.class._crates \ .select { |name, _klass| _raw.key?(name) } \ .each do |name, klass| _raw[name] = klass.new(_raw[name], **_options, fullname: _name(name)) end end
_run_hooks()
click to toggle source
# File lib/crate/crate.rb, line 170 def _run_hooks self.class._hooks.each { |hook| instance_eval(&hook) } end
_select_klass(klass)
click to toggle source
# File lib/crate/crate.rb, line 196 def _select_klass(klass) _exit_if_no_klass(klass) @_crate = klass.new(_raw, **_options) (methods - REJECT_METHODS).each do |method_name| instance_eval("undef #{method_name.inspect}") end (class << self; self end) \ .send(:define_method, :method_missing) do |*args, &block| @_crate.send(*args, &block) end end
_select_klass_if_exist(**options)
click to toggle source
# File lib/crate/crate.rb, line 174 def _select_klass_if_exist(**options) return if self.class._crate_klasses.empty? _select_klass(_find_valid_klass(_raw, **options)) end
to_json()
click to toggle source
# File lib/crate/crate.rb, line 212 def to_json _crate_serialize.to_json end
to_pretty_json()
click to toggle source
# File lib/crate/crate.rb, line 216 def to_pretty_json JSON.pretty_generate(_crate_serialize) end