class DMatch::Env

Public Instance Methods

[](identifier) click to toggle source
# File lib/destructure/env.rb, line 12
def [](identifier)
  raise 'identifier must be a Var or symbol' unless (identifier.is_a? Var) || (identifier.is_a? Symbol)
  if identifier.is_a? Symbol
    identifier = env.keys.select{|k| k.name == identifier}.first || identifier
  end
  v = env[identifier]
  raise "Identifier '#{identifier}' is not bound." if v.nil?
  v.is_a?(EnvNil) ? nil : v
end
[]=(identifier, value)
Alias for: bind
bind(identifier, value) click to toggle source
# File lib/destructure/env.rb, line 22
def bind(identifier, value)
  raise 'identifier must be a Var' unless identifier.is_a? Var
  value_to_store = value.nil? ? EnvNil.new : value
  existing_key = env.keys.select{|k| k == identifier || (k.name.is_a?(Symbol) && k.name == identifier.name)}.first
  return nil if existing_key &&
      (DMatch.match(env[existing_key], value_to_store).nil? ||
      DMatch.match(value_to_store, env[existing_key]).nil?)
  env[existing_key || identifier] = value_to_store
  self
end
Also aliased as: []=
env() click to toggle source
# File lib/destructure/env.rb, line 8
def env
  @env ||= {}
end
keys() click to toggle source
# File lib/destructure/env.rb, line 35
def keys
  env.keys
end
merge!(other_env) click to toggle source
# File lib/destructure/env.rb, line 43
def merge!(other_env)
  other_env.keys.any?{|k| bind(k, other_env[k]).nil?} ? nil : self
end
to_openstruct() click to toggle source
# File lib/destructure/env.rb, line 39
def to_openstruct
  OpenStruct.new(Hash[env.map{|kv| [kv.first.name, kv.last]}])
end