Sha256: e72ff11488f7f065cc62e461dfd1852faf1239c99f6ccbf6b295f58489b757c2

Contents?: true

Size: 1.02 KB

Versions: 14

Compression:

Stored size: 1.02 KB

Contents

class Capricorn::SystemContext

  require 'rush'
  require 'mustache'

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

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

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

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

  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

  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

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

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
capricorn-2.0.11 lib/capricorn/system_context.rb
capricorn-2.0.10 lib/capricorn/system_context.rb
capricorn-2.0.9 lib/capricorn/system_context.rb
capricorn-2.0.8 lib/capricorn/system_context.rb
capricorn-2.0.7 lib/capricorn/system_context.rb
capricorn-2.0.6 lib/capricorn/system_context.rb
capricorn-2.0.5 lib/capricorn/system_context.rb
capricorn-2.0.4 lib/capricorn/system_context.rb
capricorn-2.0.3 lib/capricorn/system_context.rb
capricorn-2.0.3.pre2 lib/capricorn/system_context.rb
capricorn-2.0.3.pre lib/capricorn/system_context.rb
capricorn-2.0.2 lib/capricorn/system_context.rb
capricorn-2.0.1 lib/capricorn/system_context.rb
capricorn-2.0.0 lib/capricorn/system_context.rb