Class: Cigale::MacroContext
Instance Method Summary collapse
-
#expand(entity) ⇒ Object
walk tree, expand macros we find.
- #expanding? ⇒ Boolean
- #get_param(spec) ⇒ Object
- #had_expansions? ⇒ Boolean
-
#initialize(options) ⇒ MacroContext
constructor
A new instance of MacroContext.
-
#interpolate(entity) ⇒ Object
given “name”.
-
#lookup(macro_name) ⇒ Object
get a macro.
- #with_params(params) ⇒ Object
Methods inherited from Exts
#asplode, #boolp, #dig, #first_pair, #toa, #toh
Constructor Details
#initialize(options) ⇒ MacroContext
Returns a new instance of MacroContext.
6 7 8 9 10 |
# File 'lib/cigale/macro_context.rb', line 6 def initialize () @library = [:library] @params = [:params] @had_expansions = false end |
Instance Method Details
#expand(entity) ⇒ Object
walk tree, expand macros we find
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cigale/macro_context.rb', line 20 def (entity) case entity when Array # list of things, need to expand each individually entity.map do |x| (x) end when Hash entity.map do |k, v| case k when /^\.(.*)$/ # macro call if # keep child macro as-is for later expansion [k, (v)] else mdef = lookup($1) res = self.with_params(v).(mdef) case res when Hash first_pair(res) when String, Array return res else raise "Invalid macro expansion result: #{res.inspect}" end end else [k, (v)] end end.to_h else # not a list, not a hash interpolate(entity) end end |
#expanding? ⇒ Boolean
99 100 101 |
# File 'lib/cigale/macro_context.rb', line 99 def not @params.nil? end |
#get_param(spec) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cigale/macro_context.rb', line 76 def get_param (spec) raise "Found param {#{spec}} outside expansion" unless name, type = spec.split(" ") if name == '*' return @params end res = dig(@params, name) raise "Unspecified param {#{name}}" if res.nil? res end |
#had_expansions? ⇒ Boolean
95 96 97 |
# File 'lib/cigale/macro_context.rb', line 95 def had_expansions? @had_expansions end |
#interpolate(entity) ⇒ Object
given “name”
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cigale/macro_context.rb', line 59 def interpolate (entity) return entity unless case entity when /^{([a-zA-Z0-9*\. -]*)}$/ # just paste param verbatim — could be a string, # could be a hash, a list, whatever, just jam it in there. get_param($1) when String entity.gsub /{([a-zA-Z0-9*\. -]*)}/ do |m| get_param($1) end else entity end end |
#lookup(macro_name) ⇒ Object
get a macro
13 14 15 16 17 |
# File 'lib/cigale/macro_context.rb', line 13 def lookup (macro_name) mdef = @library[macro_name] raise "Undefined macro: #{macro_name} — got #{@library.keys.inspect}" unless mdef mdef end |
#with_params(params) ⇒ Object
89 90 91 92 93 |
# File 'lib/cigale/macro_context.rb', line 89 def with_params (params) raise "Can't expand while expanding" if @had_expansions = true MacroContext.new(:library => @library, :params => params) end |