Module: Scheme::Parser::Methods

Defined in:
lib/scheme/parser/methods.rb

Constant Summary collapse

CONTEXT_KEYS =
[ :by, :re, :from, :context, :field, :reset_context, :on_proceed, :scheme, :on_found ]
FIELD_KEYS =
[ :required, :as, :if, :update, :update_field, :on_complete, :map ]
PURE_CONTEXT_KEYS =
[ :from, :context, :reset_context ]

Instance Method Summary collapse

Instance Method Details

#context(name, &block) ⇒ Object



12
13
14
15
16
# File 'lib/scheme/parser/methods.rb', line 12

def context name, &block
   current_context << name.to_s
   yield
   current_context.pop
end

#current_contextObject



79
80
81
# File 'lib/scheme/parser/methods.rb', line 79

def current_context
   @current_context ||= []
end

#current_optionsObject



91
92
93
# File 'lib/scheme/parser/methods.rb', line 91

def current_options
   current_scheme[nil] ||= {}
end

#current_schemeObject



95
96
97
# File 'lib/scheme/parser/methods.rb', line 95

def current_scheme
   schemes[current_scheme_path.last] ||= {}
end

#current_scheme_pathObject



83
84
85
# File 'lib/scheme/parser/methods.rb', line 83

def current_scheme_path
   @current_scheme_path ||= []
end

#filter_hashes(hashes, by) ⇒ Object



60
61
62
63
64
65
66
# 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) ⇒ Object



26
27
28
# 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) ⇒ Object



38
39
40
# 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) ⇒ Object



30
31
32
# 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) ⇒ Object



34
35
36
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object

TODO move to protected



54
55
56
57
58
# 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, &block) ⇒ Object



6
7
8
9
10
# 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) ⇒ Object



18
19
20
# File 'lib/scheme/parser/methods.rb', line 18

def scheme_name name
   name.to_s.singularize
end

#schemesObject



87
88
89
# File 'lib/scheme/parser/methods.rb', line 87

def schemes
   @schemes ||= {}
end

#use_default_key(name) ⇒ Object



22
23
24
# File 'lib/scheme/parser/methods.rb', line 22

def use_default_key name
   current_options[:key] = name
end