Class: Riddler::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/riddler/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = nil) ⇒ Context

Returns a new instance of Context.



6
7
8
9
10
11
12
13
14
15
# File 'lib/riddler/context.rb', line 6

def initialize input = nil
  input ||= {}
  @variables = {
    "ids" => {}
  }
  input.each do |key, value|
    next if value.nil?
    assign key, value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



45
46
47
48
# File 'lib/riddler/context.rb', line 45

def method_missing method_name, *_args
  return super unless variables.key? method_name.to_s
  variable method_name
end

Instance Attribute Details

#variablesObject (readonly)

Returns the value of attribute variables.



4
5
6
# File 'lib/riddler/context.rb', line 4

def variables
  @variables
end

Instance Method Details

#add_id(name, value) ⇒ Object



30
31
32
# File 'lib/riddler/context.rb', line 30

def add_id name, value
  variables["ids"][name.to_s] = value
end

#assign(name, value) ⇒ Object



17
18
19
# File 'lib/riddler/context.rb', line 17

def assign name, value
  variables[name.to_s] = drop_for value
end

#render(string) ⇒ Object



25
26
27
28
# File 'lib/riddler/context.rb', line 25

def render string
  template = ::Liquid::Template.parse string
  template.render variables
end

#to_hashObject



38
39
40
41
42
43
# File 'lib/riddler/context.rb', line 38

def to_hash
  hash_array = variables.map do |key, value|
    [key, value.to_hash]
  end
  Hash[hash_array]
end

#to_liquidObject



34
35
36
# File 'lib/riddler/context.rb', line 34

def to_liquid
  Liquid::Context.new [variables]
end

#variable(name) ⇒ Object



21
22
23
# File 'lib/riddler/context.rb', line 21

def variable name
  variables[name.to_s]
end