Class: Capricorn::SystemContext

Inherits:
Object
  • Object
show all
Defined in:
lib/capricorn/system_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ SystemContext

Returns a new instance of SystemContext.



10
11
12
13
14
15
16
17
18
# File 'lib/capricorn/system_context.rb', line 10

def initialize(attributes={})
  @binding = binding
  @attributes = {}

  attributes[:box] ||= Rush::Box.new('localhost')
  attributes.each do |name, value|
    set(name, value)
  end
end

Class Method Details

.run(script, attributes = {}) ⇒ Object



6
7
8
# File 'lib/capricorn/system_context.rb', line 6

def self.run(script, attributes={})
  new(attributes).load(script)
end

Instance Method Details

#load(script) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/capricorn/system_context.rb', line 24

def load(script)
  case script
  when IO then eval(script.read, @binding)
  when String then
    if File.file?(script)
      eval(File.read(script), @binding, script, 0)
    else
      eval(script, @binding)
    end
  end
  self
end

#render(tpl, context = {}) ⇒ Object



45
46
47
# File 'lib/capricorn/system_context.rb', line 45

def render(tpl, context={})
  Mustache.render(tpl, @attributes.merge(context))
end

#run(script, attributes = {}) ⇒ Object



20
21
22
# File 'lib/capricorn/system_context.rb', line 20

def run(script, attributes={})
  self.class.new(script, @attributes.merge(attributes))
end

#set(attribute, value) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/capricorn/system_context.rb', line 37

def set(attribute, value)
  unless self.respond_to?(attribute)
    mc = (class << self ; self ; end)
    mc.send(:define_method, attribute.to_sym) { @attributes[attribute.to_sym] }
  end
  @attributes[attribute.to_sym] = value
end