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.
16 17 18 19 20 |
# File 'lib/cigale/macro_context.rb', line 16 def initialize () @library = [:library] @params = [:params] @had_expansions = false end |
Instance Method Details
#expand(entity) ⇒ Object
walk tree, expand macros we find
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cigale/macro_context.rb', line 30 def (entity) case entity when Array # list of things, need to expand each individually array = [] entity.each do |x| case child = (x) when Splat array += child.elems else array << child end end array when Hash pairs = [] entity.each do |k, v| case k when /^\.(.*)$/ # macro call if # keep child macro as-is for later expansion pairs << [k, (v)] else splat = false mname = $1 # cf. https://github.com/itchio/cigale/issues/2 for 'spec' if mname =~ /^\./ mname = mname[1..-1] splat = true end params = v mdef = lookup(mname) # cf. https://github.com/itchio/cigale/issues/3 case mdef when Hash if defaults = mdef.delete(:defaults) params = defaults.merge(toh(params)) end when Array case first = mdef.first when Hash if defaults = first.delete(:defaults) params = defaults.merge(toh(params)) mdef.shift end end end res = self.with_params(params).(mdef) case res when Hash if splat raise "Unnecessary use of splat: #{entity.inspect}" end res.each_pair { |p| pairs << p } when Array if splat if entity.size > 1 raise "Invalid array splat (needs to be single-pair hash): #{entity.inspect}" end return Splat.new(res) else return res end else if splat raise "Invalid macro expansion result for splat: #{res.inspect}" end return res end end else pairs << [k, (v)] end end pairs.to_h else # not a list, not a hash interpolate(entity) end end |
#expanding? ⇒ Boolean
161 162 163 |
# File 'lib/cigale/macro_context.rb', line 161 def not @params.nil? end |
#get_param(spec) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/cigale/macro_context.rb', line 138 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
157 158 159 |
# File 'lib/cigale/macro_context.rb', line 157 def had_expansions? @had_expansions end |
#interpolate(entity) ⇒ Object
given “name”
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cigale/macro_context.rb', line 121 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
23 24 25 26 27 |
# File 'lib/cigale/macro_context.rb', line 23 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
151 152 153 154 155 |
# File 'lib/cigale/macro_context.rb', line 151 def with_params (params) raise "Can't expand while expanding" if @had_expansions = true MacroContext.new(:library => @library, :params => params) end |