Sha256: bb96f6c0b84b32a2aa101b5f0fb9e4ee3440df9c2c089be898733cf74514792f

Contents?: true

Size: 1.6 KB

Versions: 252

Compression:

Stored size: 1.6 KB

Contents

##
## $Release: 2.7.0 $
## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
##


module Erubis


  ##
  ## context object for Engine#evaluate
  ##
  ## ex.
  ##   template = <<'END'
  ##   Hello <%= @user %>!
  ##   <% for item in @list %>
  ##    - <%= item %>
  ##   <% end %>
  ##   END
  ##
  ##   context = Erubis::Context.new(:user=>'World', :list=>['a','b','c'])
  ##   # or
  ##   # context = Erubis::Context.new
  ##   # context[:user] = 'World'
  ##   # context[:list] = ['a', 'b', 'c']
  ##
  ##   eruby = Erubis::Eruby.new(template)
  ##   print eruby.evaluate(context)
  ##
  class Context
    include Enumerable

    def initialize(hash=nil)
      hash.each do |name, value|
        self[name] = value
      end if hash
    end

    def [](key)
      return instance_variable_get("@#{key}")
    end

    def []=(key, value)
      return instance_variable_set("@#{key}", value)
    end

    def keys
      return instance_variables.collect { |name| name[1..-1] }
    end

    def each
      instance_variables.each do |name|
        key = name[1..-1]
        value = instance_variable_get(name)
        yield(key, value)
      end
    end

    def to_hash
      hash = {}
      self.keys.each { |key| hash[key] = self[key] }
      return hash
    end

    def update(context_or_hash)
      arg = context_or_hash
      if arg.is_a?(Hash)
        arg.each do |key, val|
          self[key] = val
        end
      else
        arg.instance_variables.each do |varname|
          key = varname[1..-1]
          val = arg.instance_variable_get(varname)
          self[key] = val
        end
      end
    end

  end


end

Version data entries

252 entries across 212 versions & 42 rubygems

Version Path
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.3.1 bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.1.1.0 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.3.0 bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.0.4.0 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.0.3.0 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.2.1 bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.2.0 bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.1.1 bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.1.0 bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.0.1 bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb
brakeman-4.0.1.pre1 bundle/ruby/2.3.0/gems/erubis-2.7.0/lib/erubis/context.rb