Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DCI example in Noscript

  • Loading branch information...
commit b9850ade854bdf9d067f9c6deec6f8b5bd6818d8 1 parent cc456bd
@txus authored
Showing with 43 additions and 0 deletions.
  1. +35 −0 examples/dci.ns
  2. +8 −0 test/integration_test.rb
View
35 examples/dci.ns
@@ -0,0 +1,35 @@
+# DCI (Data-Context-Interaction)
+# ------------------------------
+
+# Models - plain, dumb data containers define what the system **is**.
+User = Object.clone({
+ cart: []
+})
+Book = Object.clone()
+
+# Roles - define what the system **does**.
+Customer = Trait.build('Customer', {
+ add to cart: -> book
+ @cart << book
+ end
+})
+
+# Define the use case (a Context): A user adds a book to his cart.
+add to cart context = -> user, book
+ user.uses(Customer)
+ user.add to cart(book)
+end
+
+# Runtime program (what a controller would do in a web application)
+# ---------------
+
+# We initialize the objects:
+john = User.clone()
+book = Book.clone()
+
+# We apply the context on these objects.
+add to cart context(john, book) # Execute the context
+
+# And now John has one item in his cart!
+msg = 'John has now %i elements in his cart' % john.cart.length()
+msg.puts()
View
8 test/integration_test.rb
@@ -37,6 +37,14 @@ def test_objects
], output
end
+ def test_dci
+ output = `./bin/noscript examples/dci.ns`.split("\n")
+
+ assert_equal [
+ "John has now 1 elements in his cart"
+ ], output
+ end
+
def test_traits
output = `./bin/noscript examples/traits.ns`.split("\n")
Please sign in to comment.
Something went wrong with that request. Please try again.