Class: Sapluuna::Context

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

Defined Under Namespace

Classes: VariableMissing

Constant Summary collapse

RootDirectory =
'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
# File 'lib/sapluuna/context.rb', line 9

def initialize opts
  @opts                 = opts.dup
  @discover_variables   = opts.delete :discover_variables
  @variables            = (opts.delete(:variables) or {})
  @root_directory       = (opts.delete(:root_directory) or RootDirectory)
  @output               = ''
  @discovered_variables = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sapluuna/context.rb', line 73

def method_missing method, *args
  if Array === args.first
    value = args.first.last
    case args.first.first
    when :is
      @variables[method] ||= value
    when :are
      @variables[method] ||= value.to_s.strip.split(/\s+/)
    end
    ""
  elsif @variables[method]
    @variables[method]
  else
    if @discover_variables
      args ||= ['']
      @discovered_variables[method] = args[0] unless @discovered_variables.has_key? method
      ""
    else
      raise VariableMissing, "variable '#{method}' required, but not given"
    end
  end
end

Instance Attribute Details

#discovered_variablesObject (readonly)

Returns the value of attribute discovered_variables.



3
4
5
# File 'lib/sapluuna/context.rb', line 3

def discovered_variables
  @discovered_variables
end

#variablesObject (readonly)

Returns the value of attribute variables.



3
4
5
# File 'lib/sapluuna/context.rb', line 3

def variables
  @variables
end

Instance Method Details

#are(value) ⇒ Object



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

def are value
  [:are, value]
end

#cfg(value) ⇒ Object



18
19
20
# File 'lib/sapluuna/context.rb', line 18

def cfg value
  @output << value
end

#code(value) ⇒ Object



22
23
24
# File 'lib/sapluuna/context.rb', line 22

def code value
  @output << eval(value).to_s
end

#is(value) ⇒ Object



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

def is value
  [:is, value]
end

#silent(*args) ⇒ Object



38
39
40
# File 'lib/sapluuna/context.rb', line 38

def silent *args
  ""
end

#strObject



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

def str
  @output
end