Class: Rmk::Vars

Inherits:
Hash
  • Object
show all
Defined in:
lib/rmk/vars.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rmk, upstream) ⇒ Vars

Returns a new instance of Vars.

Parameters:

  • upstream (Rmk::Vars, nil)

    upstream vars for lookup var which current obj not include



7
# File 'lib/rmk/vars.rb', line 7

def initialize(rmk, upstream) @rmk, @upstream = rmk, upstream end

Instance Attribute Details

#rmkObject (readonly)

Returns the value of attribute rmk.



8
9
10
# File 'lib/rmk/vars.rb', line 8

def rmk
  @rmk
end

#upstreamObject (readonly)

Returns the value of attribute upstream.



8
9
10
# File 'lib/rmk/vars.rb', line 8

def upstream
  @upstream
end

Instance Method Details

#[](name) ⇒ Object



16
# File 'lib/rmk/vars.rb', line 16

def [](name) @rmk[name] ||  fetch(name) end

#[]=(name, append = false, value) ⇒ Object



18
19
20
21
# File 'lib/rmk/vars.rb', line 18

def []=(name, append = false, value)
	value = interpolate_str value.to_s
	super name, append ? self[name].to_s + value : value
end

#downstream_new(clone_rmk: false) ⇒ Object

create new downstream vars which will lookup first, when not found then lookup current obj

Parameters:

  • clone_rmk (Boolean) (defaults to: false)

    dup a new rmk Hash or not, usually dup when need add reserved vars



12
# File 'lib/rmk/vars.rb', line 12

def downstream_new(clone_rmk:false) Rmk::Vars.new clone_rmk ? @rmk.clone(freeze:false) : @rmk, self end

#include?(name, inherit = true) ⇒ Boolean

Returns:

  • (Boolean)


25
# File 'lib/rmk/vars.rb', line 25

def include?(name, inherit = true) inherit ? @rmk.include?(name) || member?(name) : super(name) end

#interpolate_str(str) ⇒ Object

preprocess str, and then unescape the result



40
# File 'lib/rmk/vars.rb', line 40

def interpolate_str(str) unescape_str preprocess_str str end

#keys(inherit = true) ⇒ Object



31
# File 'lib/rmk/vars.rb', line 31

def keys(inherit = true) inherit ? @rmk.keys + get_keys : super() end

#org_keysObject



27
# File 'lib/rmk/vars.rb', line 27

alias_method :org_keys, :keys

#preprocess_str(str) ⇒ Object

only do #w+ interpolate



34
# File 'lib/rmk/vars.rb', line 34

def preprocess_str(str) str.gsub(/\$((?:\$\$)*){(\w+)}/){"#{$1}#{self[$2]}"} end

#split_str(str) ⇒ Array<String>

preprocess str,and then split use white spaces, and then unescape each result, typically used for split file list

Parameters:

  • str (String)

    str to split

Returns:

  • (Array<String>)


45
46
47
48
49
50
51
52
53
# File 'lib/rmk/vars.rb', line 45

def split_str(str)
	str = preprocess_str str
	result = []
	until str.empty?
		head, _, str = str.partition /(?<!\$)(?:\$\$)*\K\s+/
		result << unescape_str(head) unless head.empty?
	end
	result
end

#unescape_str(str) ⇒ Object

do all ‘$’ prefix escape str interpolate



37
# File 'lib/rmk/vars.rb', line 37

def unescape_str(str) str.gsub(/\$(?:([\$\s>&])|(\w+))/){$1 || self[$2]} end