module Scheme::Parser::Methods

Constants

CONTEXT_KEYS
FIELD_KEYS
PURE_CONTEXT_KEYS

Public Instance Methods

context(name) { || ... } click to toggle source
# File lib/scheme/parser/methods.rb, line 12
def context name, &block
   current_context << name.to_s
   yield
   current_context.pop
end
current_context() click to toggle source
# File lib/scheme/parser/methods.rb, line 79
def current_context
   @current_context ||= []
end
current_options() click to toggle source
# File lib/scheme/parser/methods.rb, line 91
def current_options
   current_scheme[nil] ||= {}
end
current_scheme() click to toggle source
# File lib/scheme/parser/methods.rb, line 95
def current_scheme
   schemes[current_scheme_path.last] ||= {}
end
current_scheme_path() click to toggle source
# File lib/scheme/parser/methods.rb, line 83
def current_scheme_path
   @current_scheme_path ||= []
end
filter_hashes(hashes, by) click to toggle source
# File lib/scheme/parser/methods.rb, line 60
def filter_hashes hashes, by
   hashes.flatten.map do |hash|
      (hash.keys & by).map { |x| [ x, hash[x] ] }.to_h
   end.select do |hash|
      hash.any?
   end
end
has_field(name, *args) click to toggle source
# File lib/scheme/parser/methods.rb, line 26
def has_field name, *args
   current_scheme[scheme_name(name)] = make_options(:field, args)
end
has_reference(name, *args) click to toggle source
# File lib/scheme/parser/methods.rb, line 38
def has_reference name, *args
   current_scheme[scheme_name(name)] = make_options(:reference, args)
end
has_scheme(name, *args) click to toggle source
# File lib/scheme/parser/methods.rb, line 30
def has_scheme name, *args
   current_scheme[scheme_name(name)] = make_options(:scheme, args)
end
has_schemes(name, *args) click to toggle source
# File lib/scheme/parser/methods.rb, line 34
def has_schemes name, *args
   current_scheme[scheme_name(name)] = make_options(:scheme, args, true)
end
make_contexts(contexts) click to toggle source
# File lib/scheme/parser/methods.rb, line 68
def make_contexts contexts
   (contexts.empty? && [{}] || contexts).map do |options|
      ctxs = [ options[:context] || "" ].flatten
      options[:context] = ctxs.map do |ctx|
         [ current_context, ctx ].flatten.compact.join('/')
      end

      options
   end
end
make_options(type, args, multiple = false) click to toggle source

TODO move to protected

# File lib/scheme/parser/methods.rb, line 54
def make_options type, args, multiple = false
   contexts = make_contexts(filter_hashes(args, CONTEXT_KEYS))
   local = filter_hashes(args, FIELD_KEYS).reduce({}) { |r, x| r.merge(x) }
   { type: type, multiple: multiple, contexts: contexts }.merge(local)
end
scheme(name) { || ... } click to toggle source
# File lib/scheme/parser/methods.rb, line 6
def scheme name, &block
   current_scheme_path << name.to_s
   yield
   current_scheme_path.pop
end
scheme_name(name) click to toggle source
# File lib/scheme/parser/methods.rb, line 18
def scheme_name name
   name.to_s.singularize
end
schemes() click to toggle source
# File lib/scheme/parser/methods.rb, line 87
def schemes
   @schemes ||= {}
end
use_default_key(name) click to toggle source
# File lib/scheme/parser/methods.rb, line 22
def use_default_key name
   current_options[:key] = name
end