Sha256: a4dcc81a1c50531aa280e4abdc0b7a5a244408dc810054bb1df13468b3f95763

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

class Polly::Env < Hash
  include Polly::Common

  def initialize(env = {})
    Math.singleton_methods.each do |m|
      self[m.to_sym] = lambda { |*args| Math.send(m, *args) }
    end

    env.each { |name, expr| self[name] = Sexpr.build(expr, self) }
  end

  def clean
    Env[self.select { |name, expr| expr.is_a?(Sexpr) }]
  end

  def values
    clean.inject({}) { |h, (name, expr)| h[name] = expr.value; h }
  end

  def values!
    clean.inject({}) { |h, (name, expr)| h[name] = expr.value!; h }
  end

  def atomic_variables
    clean.select { |name, expr| expr.atomic? }.keys
  end

  def defined_variables
    clean.select { |name, expr| expr.atomic? && expr.defined? }.keys
  end

  def undefined_variables
    clean.select { |name, expr| expr.atomic? && !expr.defined? }.keys
  end

# printing  and conversion

  def print(opts = {}); puts to_s(opts) end
  def to_s(opts = {}); clean.map { |(k,v)| "#{k.inspect} => #{v.to_s(opts)}" }.join("\n") end
  def to_yaml(*args); dump.to_yaml(*args) end

  def ==(env)
    env.is_a?(Hash) ? Hash[self.clean] == Hash[env.clean] : false
  end

private

  def dump
    clean.inject({}) { |h, (name, expr)| h[name] = expr.to_ary; h }
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
polly-0.0.6 lib/polly/env.rb
polly-0.0.5 lib/polly/env.rb
polly-0.0.4 lib/polly/env.rb