Module: OrangeLib::Variables

Included in:
Rest
Defined in:
lib/orange_lib/vars.rb

Instance Method Summary collapse

Instance Method Details

#clear_variablesObject

Remove all of stored variables.

Examples:

clear_variables


23
24
25
# File 'lib/orange_lib/vars.rb', line 23

def clear_variables
  variables.clear
end

#interpret_string(input, params = {}) ⇒ Object

Interpret an input string or hash by using the variable that set before that or the params argument

Examples:

app.variable('app', 'bdd-lib')
model = { name: 'Chris', friends: [{fname: 'Hung'}, {fname: 'Aurora'}] }
template = 'Hello %{name} from %{app}, your friends are %{friends[0].fname} and %{friends[1].fname}'
output = app.interpret_string(template, model) # -> Hello Chris from bdd-lib, your friends are Hung and Aurora


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/orange_lib/vars.rb', line 36

def interpret_string(input, params = {})
  # Merge optional parameters with global variables
  params = variables.merge(params)
  params = flatten_hash(params)

  if input.class == Hash
    input.each { |k, v| input[k] = interpret_string(v) }
  elsif input.class == String && input =~ /(\%\{.+\})+/
    input % params
  else
    input
  end
end

#variable(name, value = nil) ⇒ Object

Create variable with input name and value. If input value is not passed, method will return value of variable (This function is used if we would like to store a value to a variable in cucumber)

Examples:

variable('REPORT_ID', 'c73f5f2a-d098-4d84-8831-35c704773747') => store report id as variable REPORT_ID
variable('REPORT_ID') => return value of variable REPORT_ID

Parameters:

  • name (String)

    variable name

  • value (Object) (defaults to: nil)

    value of variable

Returns:

  • (Object)

    value of variable if input value is not passed.



12
13
14
15
16
17
# File 'lib/orange_lib/vars.rb', line 12

def variable(name, value = nil)
  name = name.to_sym
  return variables[name] if value.nil?

  variables[name] = value
end