Browse files

add GenerateGroup component

  • Loading branch information...
1 parent 553da10 commit 00fd524b5713e5fd34c1436d6d56be6fdf944684 @djdeath djdeath committed Apr 22, 2014
Showing with 114 additions and 3 deletions.
  1. +4 −1 component.json
  2. +53 −0 components/GenerateGroup.coffee
  3. +4 −2 package.json
  4. +53 −0 test/GenerateGroup.coffee
View
5 component.json
@@ -11,6 +11,7 @@
"repo": "kenhkan/groups",
"dependencies": {
"component/underscore": "*",
+ "broofa/node-uuid": "*",
"noflo/noflo": "*"
},
"scripts": [
@@ -30,6 +31,7 @@
"components/MapGroup.coffee",
"components/MergeGroups.coffee",
"components/GroupByObjectKey.coffee",
+ "components/GenerateGroup.coffee",
"index.js"
],
"json": [
@@ -53,7 +55,8 @@
"FirstGroup": "components/FirstGroup.coffee",
"MapGroup": "components/MapGroup.coffee",
"MergeGroups": "components/MergeGroups.coffee",
- "GroupByObjectKey": "components/GroupByObjectKey.coffee"
+ "GroupByObjectKey": "components/GroupByObjectKey.coffee",
+ "GenerateGroup": "components/GenerateGroup.coffee"
}
}
}
View
53 components/GenerateGroup.coffee
@@ -0,0 +1,53 @@
+noflo = require 'noflo'
+uuid = require 'node-uuid'
+
+class GenerateGroup extends noflo.Component
+
+ description: 'Wrap IPs into a random uuid generated group'
+
+ constructor: ->
+ @inPorts =
+ in: new noflo.Port 'all'
+ @outPorts =
+ out: new noflo.Port 'all'
+
+ @groups = []
+
+ @inPorts.in.on 'begingroup', (group) =>
+ @beginGroup group
+ @inPorts.in.on 'data', (data) =>
+ @pushGeneratedGroup data
+ @inPorts.in.on 'endgroup', () =>
+ @popGeneratedGroup()
+ @endGroup()
+ @inPorts.in.on 'disconnect', () =>
+ @popGeneratedGroup()
+ @clearGroups()
+ @outPorts.out.disconnect() if @outPorts.out.isConnected()
+
+ beginGroup: (group) ->
+ @groups.push { group: group, generated: false }
+ @outPorts.out.beginGroup group if @outPorts.out.isAttached()
+
+ endGroup: () ->
+ @groups.pop()
+ @outPorts.out.endGroup() if @outPorts.out.isAttached()
+
+ pushGeneratedGroup: (data) ->
+ if @groups.length < 1 or
+ (@groups.length > 0 and not @groups[@groups.length - 1].generated)
+ generated = { group: uuid(), generated: true }
+ @groups.push generated
+ @outPorts.out.beginGroup generated.group if @outPorts.out.isAttached()
+ @outPorts.out.send data if @outPorts.out.isAttached()
+
+ popGeneratedGroup: () ->
+ return if @groups.length < 1
+ return unless @groups[@groups.length - 1].generated
+ @groups.pop()
+ @outPorts.out.endGroup() if @outPorts.out.isAttached()
+
+ clearGroups: () ->
+ @groups = []
+
+exports.getComponent = -> new GenerateGroup
View
6 package.json
@@ -30,7 +30,8 @@
],
"dependencies": {
"noflo": "~0.5.0",
- "underscore": "1.4.x"
+ "underscore": "1.4.x",
+ "node-uuid": "1.4.x"
},
"devDependencies": {
"grunt-exec": "~0.4.2",
@@ -67,7 +68,8 @@
"FirstGroup": "./components/FirstGroup.coffee",
"MapGroup": "./components/MapGroup.coffee",
"MergeGroups": "./components/MergeGroups.coffee",
- "GroupByObjectKey": "./components/GroupByObjectKey.coffee"
+ "GroupByObjectKey": "./components/GroupByObjectKey.coffee",
+ "GenerateGroup": "./components/GenerateGroup.coffee"
},
"graphs": {
"ObjectifyByGroup": "./graphs/ObjectifyByGroup.fbp"
View
53 test/GenerateGroup.coffee
@@ -0,0 +1,53 @@
+test = require "noflo-test"
+
+test.component("groups/GenerateGroup").
+ discuss("pass in a non grouped connection").
+ send.connect("in").
+ send.data("in", "data").
+ send.disconnect("in").
+ discuss("one groups is sent").
+ receive.connect("out").
+ receive.beginGroup("out").
+ receive.data("out", "data").
+ receive.endGroup("out").
+ receive.disconnect("out").
+
+ next().
+ discuss("pass in a grouped connection").
+ send.beginGroup("in", "group").
+ send.data("in", "data").
+ send.endGroup("in").
+ send.disconnect("in").
+ discuss("two groups are sent").
+ receive.connect("out").
+ receive.beginGroup("out", "group").
+ receive.beginGroup("out").
+ receive.data("out", "data").
+ receive.endGroup("out").
+ receive.endGroup("out").
+ receive.disconnect("out").
+
+ next().
+ discuss("pass in a multi grouped connection").
+ send.beginGroup("in", "group1").
+ send.data("in", "data1").
+ send.beginGroup("in", "group2").
+ send.data("in", "data2").
+ send.endGroup("in").
+ send.endGroup("in").
+ send.disconnect("in").
+ discuss("four groups are sent").
+ receive.connect("out").
+ receive.beginGroup("out", "group").
+ receive.beginGroup("out").
+ receive.data("out", "data1").
+ receive.beginGroup("out", "group2").
+ receive.beginGroup("out").
+ receive.data("out", "data2").
+ receive.endGroup("out").
+ receive.endGroup("out").
+ receive.endGroup("out").
+ receive.endGroup("out").
+ receive.disconnect("out").
+
+export module

0 comments on commit 00fd524

Please sign in to comment.