Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Let Scope#fetch take the dialect as second arg.

  • Loading branch information...
commit b7d444d763cf07b25a8770d91f86c35ef97836d2 1 parent e95cfa2
@blambeau authored
View
2  lib/wlang/dialect.rb
@@ -183,7 +183,7 @@ def evaluate(expr, *default, &bl)
case expr
when Symbol, String
catch(:fail) do
- return scope.evaluate(expr, *default, &bl)
+ return scope.evaluate(expr, self, *default, &bl)
end
raise NameError, "Unable to find `#{expr}`"
else
View
7 lib/wlang/scope.rb
@@ -43,7 +43,7 @@ def with(x)
yield(self.push(x))
end
- def evaluate(expr, *default, &bl)
+ def evaluate(expr, dialect, *default, &bl)
expr = expr.to_s.strip
unfound = lambda do
if default.empty?
@@ -53,11 +53,12 @@ def evaluate(expr, *default, &bl)
end
end
if expr.index('.').nil?
- fetch(expr.to_sym, &unfound)
+ fetch(expr.to_sym, dialect, unfound)
else
keys = expr.split('.').map(&:to_sym)
keys.inject(self){|scope,key|
- Scope.coerce(scope.fetch(key, &unfound))
+ found = scope.fetch(key, dialect, unfound)
+ Scope.coerce(found)
}.subject
end
end
View
6 lib/wlang/scope/binding_scope.rb
@@ -2,10 +2,10 @@ module WLang
class Scope
class BindingScope < Scope
- def fetch(k, &blk)
- subject.eval(k.to_s)
+ def fetch(key, dialect = nil, unfound = nil)
+ subject.eval(key.to_s)
rescue NameError
- safe_parent.fetch(k, &blk)
+ safe_parent.fetch(key, dialect, unfound)
end
def inspect
View
4 lib/wlang/scope/null_scope.rb
@@ -6,8 +6,8 @@ def initialize
super(nil)
end
- def fetch(key)
- block_given? ? yield : throw(:fail)
+ def fetch(key, dialect = nil, unfound = nil)
+ unfound ? unfound.call : throw(:fail)
end
def subjects
View
16 lib/wlang/scope/object_scope.rb
@@ -2,28 +2,28 @@ module WLang
class Scope
class ObjectScope < Scope
- def fetch(k, &blk)
+ def fetch(key, dialect = nil, unfound = nil)
s = subject
# self special case
- if k == :self
+ if key == :self
return s
end
# hash indirect access
if s.respond_to?(:has_key?)
- return s[k] if s.has_key?(k)
- return s[k.to_s] if s.has_key?(k.to_s)
+ return s[key] if s.has_key?(key)
+ return s[key.to_s] if s.has_key?(key.to_s)
end
# getter
- if s.respond_to?(k)
- return s.send(k)
+ if s.respond_to?(key)
+ return s.send(key)
end
- safe_parent.fetch(k, &blk)
+ safe_parent.fetch(key, dialect, unfound)
rescue NameError
- safe_parent.fetch(k, &blk)
+ safe_parent.fetch(key, dialect, unfound)
end
def inspect
View
8 lib/wlang/scope/proc_scope.rb
@@ -2,10 +2,10 @@ module WLang
class Scope
class ProcScope < Scope
- def fetch(key, &blk)
- Scope.coerce(subject.call).fetch(key) do
- safe_parent.fetch(key, &blk)
- end
+ def fetch(key, dialect = nil, unfound = nil)
+ scoped = Scope.coerce(subject.call)
+ fallbk = lambda{ safe_parent.fetch(key, dialect, unfound) }
+ scoped.fetch(key, dialect, fallbk)
end
def inspect
View
37 spec/unit/test_scope.rb
@@ -6,24 +6,24 @@ module WLang
it 'acts like a stack' do
s = scope
- s.evaluate(:who).should eq("World")
+ s.evaluate(:who, nil).should eq("World")
s = scope.push(:who => "World2")
- s.evaluate(:who).should eq("World2")
+ s.evaluate(:who, nil).should eq("World2")
s = s.pop
- s.evaluate(:who).should eq("World")
+ s.evaluate(:who, nil).should eq("World")
end
it 'has a `with` helper' do
scope.with(:who => "World2") do |s|
- s.evaluate(:who).should eq("World2")
+ s.evaluate(:who, nil).should eq("World2")
end
- scope.evaluate(:who).should eq("World")
+ scope.evaluate(:who, nil).should eq("World")
end
it 'allows pushing scopes' do
scope.with Scope.coerce(:other => "World2") do |s|
- s.evaluate(:other).should eq("World2")
- s.evaluate(:who).should eq("World")
+ s.evaluate(:other, nil).should eq("World2")
+ s.evaluate(:who, nil).should eq("World")
end
end
@@ -35,33 +35,32 @@ module WLang
end
it 'evaluates `self` accurately' do
- scope.evaluate(:self).should eq(:who => "World")
+ scope.evaluate(:self, nil).should eq(:who => "World")
end
it 'evaluates dot expressions correctly' do
- scope.evaluate("who.upcase").should eq("WORLD")
+ scope.evaluate("who.upcase", nil).should eq("WORLD")
end
it 'strips strings before evaluation' do
- scope.evaluate(" who ").should eq("World")
- scope.evaluate(" who.upcase ").should eq("WORLD")
+ scope.evaluate(" who ", nil).should eq("World")
+ scope.evaluate(" who.upcase ", nil).should eq("WORLD")
end
it 'fails when not found' do
- lambda{ scope.evaluate(:nosuchone) }.should throw_symbol(:fail)
+ lambda{
+ scope.evaluate(:nosuchone, nil)
+ }.should throw_symbol(:fail)
end
it 'supports a default value instead of fail' do
- scope.evaluate(:nosuchone, 12).should eq(12)
+ scope.evaluate(:nosuchone, nil, 12).should eq(12)
+ scope.evaluate(:nosuchone, nil, nil).should be_nil
end
it 'supports a default value through a block instead of fail' do
- scope.evaluate(:nosuchone){ 12 }.should eq(12)
- scope.evaluate(:nosuchone){ nil }.should be_nil
- end
-
- it 'supports nil as default value' do
- scope.evaluate(:nosuchone, nil).should be_nil
+ scope.evaluate(:nosuchone, nil){ 12 }.should eq(12)
+ scope.evaluate(:nosuchone, nil){ nil }.should be_nil
end
end # describe Scope
Please sign in to comment.
Something went wrong with that request. Please try again.