Module: ShareWith::Placeholders

Included in:
Service
Defined in:
lib/share_with/placeholders.rb

Overview

It provides the methods to replace the placeholders

with the correct data pointed.

Instance Method Summary collapse

Instance Method Details

#expand_all(str) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/share_with/placeholders.rb', line 9

def expand_all(str)
  loop do
    # Conditional params, if null remove the string
    str = str.gsub(/\[([^ \]]+)\]/) do |x|
      res = expand_all(x.delete("[]"))
      @empty ? "" : res
    end

    str = str.gsub(%r{<([^ />]+)>}) do |x|
      res = inspect(x.delete("<>"))
      @empty = res.to_s.empty?
      res.nil? ? x : res
    end

    break unless str =~ %r{<[^ />]+>}
  end

  str
end

#inspect(var_path) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/share_with/placeholders.rb', line 29

def inspect(var_path)
  context, var_name, var_select, attr_key = var_path.split(".")
  var_select ||= :value
  # puts @data,var_name,var_select if context.to_sym == :https
  res = send(context.to_sym)

  raise InvalidContext, context if res.nil?

  return res.send(var_name.to_sym) if %i[keys values].include? var_name.to_sym

  raise InvalidParam, [context, var_name].join(".") unless res.key? var_name.to_sym

  res = res[var_name.to_sym]
  case context.to_sym
  when :params
    value_selection(res, attr_key, var_select)
  when :includes
    res[:cache]
  else
    res
  end
end