Created
July 14, 2013 08:23
-
-
Save zaneli/5993611 to your computer and use it in GitHub Desktop.
「CoffeeScript によるデザインパターン(Strategy)」ブログ用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@outputHTML = -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{@getTitle()}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in @getText() | |
console.log " </body>" | |
console.log "</html>" | |
@outputPlainText = -> | |
console.log "*** #{@getTitle()} ***" | |
console.log line for line in @getText() | |
class @Report | |
constructor: (@formatter) -> | |
@getTitle = -> "月次報告" | |
@getText = -> ["順調", "最高の調子"] | |
outputReport: -> @formatter() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class @HTMLFormatter | |
outputReport: (title, text) -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{title}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in text | |
console.log " </body>" | |
console.log "</html>" | |
class @PlainTextFormatter | |
outputReport: (title, text) -> | |
console.log "*** #{title} ***" | |
console.log line for line in text | |
class @Report | |
constructor: (@formatter) -> | |
@getTitle = -> "月次報告" | |
@getText = -> ["順調", "最高の調子"] | |
outputReport: -> | |
@formatter.outputReport(@getTitle(), @getText()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class @HTMLFormatter | |
outputReport: (context) -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{context.getTitle()}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in context.getText() | |
console.log " </body>" | |
console.log "</html>" | |
class @PlainTextFormatter | |
outputReport: (context) -> | |
console.log "*** #{context.getTitle()} ***" | |
console.log line for line in context.getText() | |
class @Report | |
constructor: (@formatter) -> | |
@getTitle = -> "月次報告" | |
@getText = -> ["順調", "最高の調子"] | |
outputReport: -> | |
@formatter.outputReport(@) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@outputHTML = (context) -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{context.getTitle()}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in context.getText() | |
console.log " </body>" | |
console.log "</html>" | |
@outputPlainText = (context) -> | |
console.log "*** #{context.getTitle()} ***" | |
console.log line for line in context.getText() | |
class @Report | |
constructor: (@formatter) -> | |
@getTitle = -> "月次報告" | |
@getText = -> ["順調", "最高の調子"] | |
outputReport: -> @formatter(@) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@outputHTML = -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{@getTitle()}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in @getText() | |
console.log " </body>" | |
console.log "</html>" | |
@outputPlainText = -> | |
console.log "*** #{@getTitle()} ***" | |
console.log line for line in @getText() | |
class @Report | |
constructor: (@formatter) -> | |
@getTitle = -> "月次報告" | |
@getText = -> ["順調", "最高の調子"] | |
outputReport: -> | |
if (typeof(@formatter) is "function") | |
@formatter() | |
else | |
console.warn @formatter?.constructor?.name + " is not a function" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@outputHTML = -> | |
console.log "<html>" | |
console.log " <head>" | |
console.log " <title>#{@getTitle()}</title>" | |
console.log " </head>" | |
console.log " <body>" | |
console.log " <p>#{line}</p>" for line in @getText() | |
console.log " </body>" | |
console.log "</html>" | |
@outputPlainText = -> | |
console.log "*** #{@getTitle()} ***" | |
console.log line for line in @getText() | |
class @Report | |
constructor: (formatter) -> | |
@getTitle = -> "月次報告" | |
@getText = -> ["順調", "最高の調子"] | |
_formatter = undefined | |
@getFormatter = -> _formatter | |
@setFormatter = (newFormatter) -> | |
@safeExecute(newFormatter, => _formatter = newFormatter.bind(@); @) | |
@setFormatter(formatter) | |
outputReport: -> @safeExecute(@getFormatter(), @getFormatter()) | |
safeExecute: (chechObj, func) -> | |
if (typeof(chechObj) isnt "function") | |
throw new Error(chechObj?.constructor?.name + " is not a function") | |
func() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package views | |
import org.specs2.mutable.Specification | |
import play.api.test.TestServer | |
import play.api.test.Helpers.{ running, HTMLUNIT } | |
class StrategySpec extends Specification { | |
private val testPort = 3333 | |
"Strategy" should { | |
"output html report" in { | |
running(TestServer(testPort), HTMLUNIT) { browser => | |
browser.goTo(s"http://localhost:${testPort}/strategy") | |
browser.executeScript(""" | |
new Report(outputHTML).outputReport() | |
""") | |
browser.$("#__console").getValue must_== """<html> | |
<head> | |
<title>月次報告</title> | |
</head> | |
<body> | |
<p>順調</p> | |
<p>最高の調子</p> | |
</body> | |
</html> | |
""" | |
} | |
} | |
"output plain text report" in { | |
running(TestServer(testPort), HTMLUNIT) { browser => | |
browser.goTo(s"http://localhost:${testPort}/strategy") | |
browser.executeScript(""" | |
new Report(outputPlainText).outputReport() | |
""") | |
browser.$("#__console").getValue must_== """*** 月次報告 *** | |
順調 | |
最高の調子 | |
""" | |
} | |
} | |
"output report when set other format after" in { | |
running(TestServer(testPort), HTMLUNIT) { browser => | |
browser.goTo(s"http://localhost:${testPort}/strategy") | |
browser.executeScript(""" | |
var report = new Report(outputHTML) | |
report.outputReport() | |
report.formatter = outputPlainText | |
report.outputReport() | |
report.formatter = outputHTML | |
report.outputReport() | |
""") | |
browser.$("#__console").getValue must_== """<html> | |
<head> | |
<title>月次報告</title> | |
</head> | |
<body> | |
<p>順調</p> | |
<p>最高の調子</p> | |
</body> | |
</html> | |
*** 月次報告 *** | |
順調 | |
最高の調子 | |
<html> | |
<head> | |
<title>月次報告</title> | |
</head> | |
<body> | |
<p>順調</p> | |
<p>最高の調子</p> | |
</body> | |
</html> | |
""" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment