Class: Cigale::MacroContext

Inherits:
Exts
  • Object
show all
Defined in:
lib/cigale/macro_context.rb

Instance Method Summary collapse

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 (options)
  @library = options[:library]
  @params = options[: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
119
120
121
# File 'lib/cigale/macro_context.rb', line 30

def expand (entity)
  case entity
  when Array
    # list of things, need to expand each individually
    array = []

    entity.each do |x|
      case child = expand(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 expanding?
          # keep child macro as-is for later expansion
          pairs << [k, expand(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[:defaults]
              params = defaults.merge(toh(params))
              mdef = mdef.dup
              mdef.delete(:defaults)
            end
          when Array
            case first = mdef.first
            when Hash
              if defaults = first[:defaults]
                params = defaults.merge(toh(params))
                mdef = mdef.dup
                mdef.shift
              end
            end
          end

          res = self.with_params(params).expand(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, expand(v)]
      end
    end

    pairs.to_h
  else
    # not a list, not a hash
    interpolate(entity)
  end
end

#expanding?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/cigale/macro_context.rb', line 164

def expanding?
  not @params.nil?
end

#get_param(spec) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cigale/macro_context.rb', line 141

def get_param (spec)
  raise "Found param {#{spec}} outside expansion" unless expanding?

  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

Returns:

  • (Boolean)


160
161
162
# File 'lib/cigale/macro_context.rb', line 160

def had_expansions?
  @had_expansions
end

#interpolate(entity) ⇒ Object

given “name”



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cigale/macro_context.rb', line 124

def interpolate (entity)
  return entity unless expanding?

  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



154
155
156
157
158
# File 'lib/cigale/macro_context.rb', line 154

def with_params (params)
  raise "Can't expand while expanding" if expanding?
  @had_expansions = true
  MacroContext.new(:library => @library, :params => params)
end