Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Objects can be initialized with tuples

  • Loading branch information...
commit b99e1788f8570d89092c537b79eaf8b80c391157 1 parent 9db3721
@txus authored
View
9 examples/objects.ns
@@ -4,6 +4,13 @@ vehicle.run = ->
print('running!')
end
-car = vehicle.clone()
+car = vehicle.clone({
+ sound: 'vroom',
+ price: 99
+})
+
car.run()
+
print(car.speed)
+print(car.sound)
+print(car.price)
View
9 lib/noscript/object.rb
@@ -9,9 +9,16 @@ def initialize
@slots = {}
@traits = []
- add_slot('clone', lambda { |*|
+ add_slot('clone', lambda { |*args|
child = Object.new
child.parent = self
+
+ if context = args[0] && tuple = args[1]
+ tuple.compile(context).each do |k, v|
+ child.slots[k] = v
+ end
+ end
+
child
})
View
2  test/integration_test.rb
@@ -27,6 +27,8 @@ def test_objects
assert_equal [
"running!",
"10",
+ "vroom",
+ "99",
], output
end
View
14 test/object_test.rb
@@ -14,6 +14,20 @@ def test_clone
assert_includes(child.slots.keys, 'clone')
end
+ def test_clone_with_tuple
+ tuple = Noscript::AST::Tuple.new({
+ 'foo' => Noscript::AST::Digit.new(99),
+ 'bar' => Noscript::AST::String.new('hey'),
+ })
+ child = @object.send('clone').call(@context, tuple)
+
+ assert_equal(@object, child.parent)
+ assert_includes(child.slots.keys, 'clone')
+
+ assert_equal Noscript::AST::Digit.new(99), child.send('foo')
+ assert_equal Noscript::AST::String.new('hey'), child.send('bar')
+ end
+
def test_uses
trait = Noscript::Trait.new({
'foo' => lambda { |*| Noscript::AST::Digit.new(3) }
Please sign in to comment.
Something went wrong with that request. Please try again.