Everyday Scripting with Ruby 讀書筆記(2)

[size=small]---
Churn項目:輕鬆編寫腳本
The Churn Project: Writing Scripts without Fuss
---[/size]

subversion安裝包下載:[url=http://www.rayfile.com/zh-cn/files/b0d00902-a431-11dd-a9c3-0014221b798a/]svn-1.4.5-setup.rar[/url] [img]http://static.rayfile.com/media/img/icon_download.gif[/img]

#---
# Excerpted from "Everyday Scripting in Ruby"
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
#---

# This is the same as churn.v7.rb. It's a different file because
# of the mechanics of book production.

# 處理時間(過去一個月)
def month_before(a_time)
a_time - 28 * 24 * 60 * 60
end

# 處理打印標題信息
def header(an_svn_date)
"Changes since #{an_svn_date}:"
end

# 組成打印主體信息(參數:子項目,修改次數)
def subsystem_line(subsystem_name, change_count)
asterisks = asterisks_for(change_count)
# 註釋②
"#{subsystem_name.rjust(14)} #{asterisks} (#{change_count})"
end

# 計算星號
def asterisks_for(an_integer)
'*' * (an_integer / 5.0).round
end

# 計算修改次數
def change_count_for(name, start_date)
extract_change_count_from(svn_log(name, start_date))
end

# 根據文本信息(解析字符串)
def extract_change_count_from(log_text)
lines = log_text.split("\n")
dashed_lines = lines.find_all do | line | # 註釋③
line.include?('--------')
end
dashed_lines.length - 1
end

# 使用外部程序獲取文本信息
def svn_log(subsystem, start_date)
timespan = "--revision HEAD:{#{start_date}}"
root = "svn://rubyforge.org//var/svn/churn-demo"

`svn log #{timespan} #{root}/#{subsystem}`
end

# 格式化時間
def svn_date(a_time)
a_time.strftime("%Y-%m-%d") # 註釋①
end

if $0 == __FILE__
subsystem_names = ['audit', 'fulfillment', 'persistence',
'ui', 'util', 'inventory']
start_date = svn_date(month_before(Time.mktime(2005,9,2,0,0,0,0)))

puts header(start_date)
subsystem_names.each do | name |
puts subsystem_line(name, change_count_for(name, start_date))
end
end


[color=blue][b]** 運行結果 **[/b][/color]
>ruby churn.v7copy.rb
Changes since 2005-08-05:
audit * (5)
fulfillment (2)
persistence * (3)
ui ** (8)
util * (4)
inventory (2)
>Exit code: 0
[color=blue][b]** **[/b][/color]


[color=indigo][b]① Time對象的strftime用於格式化時間..[/b][/color]
irb(main):005:0> Time.now.strftime('%m-%d %Y')
=> "10-28 2008"

[color=indigo][b]② rjust用於實現右對齊..[/b][/color]
irb(main):007:0> 'right'.rjust(2)
=> "right"
irb(main):008:0> 'right'.rjust(8)
=> " right"

[color=indigo][b]③ find_all: 獲取代碼塊返回爲真的所有元素, 然後放入一個新的數組作爲返回值..[/b][/color]
irb(main):009:0> ['abxc', 'xx', 'b', 'ycx'].find_all do | ele |
irb(main):010:1* ele.include?('x')
irb(main):011:1> end
=> ["abxc", "xx", "ycx"]

require 'test/unit'
require 'churn.v7copy'

class ChurnMyTests < Test::Unit::TestCase

def test_month_before
assert_equal(Time.local(2008,9,30), month_before(Time.local(2008,10,28)))
end

def test_svn_date
assert_equal("2008-10-28",svn_date(Time.now))
end

def test_header
assert_equal("Changes since 2008-10-28:", header(svn_date(Time.mktime(2008,10,28,0,0,0,0))))
end

def test_extract_change_count_from
assert_equal(2,extract_change_count_from("------------------------------------------------------------------------
r2 | marick | 2005-08-07 14:26:21 -0500 (Mon, 07 Aug 2005) | 1 line

added code to handle merger
------------------------------------------------------------------------
r1 | marick | 2005-08-07 14:21:47 -0500 (Mon, 07 Aug 2005) | 1 line

first touches
No commit for revision 0.
------------------------------------------------------------------------"))
end

def test_asterisks_for
assert_equal("*********", asterisks_for(46))
end

end


[color=blue][b]** 運行結果 **[/b][/color]
>ruby churn-mytest-v7copy.rb
Loaded suite churn-mytest-v7copy
Started
.....
Finished in 0.0 seconds.

5 tests, 5 assertions, 0 failures, 0 errors
>Exit code: 0
[color=blue][b]** **[/b][/color]

# Exercises

# 處理時間(過去一個月)
def month_before(a_time)
a_time - 28 * 24 * 60 * 60
end

# 處理打印標題信息
def header(an_svn_start_date, an_svn_today_date)
"Changes between #{an_svn_start_date} and #{an_svn_today_date}:"
end

# 組成打印主體信息(參數:子項目,修改次數)
def subsystem_line(subsystem_name, change_count)
asterisks = asterisks_for(change_count)
change_text = change_text_from(change_count)
unless change_count == 0
return "#{subsystem_name.ljust(14)} #{change_text.ljust(14)} #{asterisks}"
end
return "#{subsystem_name.ljust(14)} #{'-'.ljust(14)} -"
end

# 計算星號
def asterisks_for(an_integer)
if(an_integer < 3)
'*'
else
'*' * (an_integer / 5.0).round
end
end

# 處理修改次數信息
def change_text_from(an_integer)
return "(#{an_integer} changes)"
end

# 計算修改次數
def change_count_for(name, start_date)
extract_change_count_from(svn_log(name, start_date))
end

# 根據文本信息(解析字符串)
def extract_change_count_from(log_text)
lines = log_text.split("\n")
dashed_lines = lines.find_all do | line |
line.include?('--------')
end
dashed_lines.length - 1
end

# 使用外部程序獲取文本信息
def svn_log(subsystem, start_date)
timespan = "--revision HEAD:{#{start_date}}"
root = "svn://rubyforge.org//var/svn/churn-demo"

`svn log #{timespan} #{root}/#{subsystem}`
end

# 格式化時間
def svn_date(a_time)
a_time.strftime("%Y-%m-%d")
end

if $0 == __FILE__
subsystem_names = ['audit', 'fulfillment', 'persistence',
'ui', 'util', 'inventory']
start_date = svn_date(month_before(Time.mktime(2005,9,2,0,0,0,0)))
today_date = svn_date(Time.now)

puts header(start_date, today_date)
subsystem_names.each do | name |
puts subsystem_line(name, change_count_for(name, start_date))
end
end


[color=blue][b]** 運行結果 **[/b][/color]
>ruby churn.v7copy.rb
Changes between 2005-08-05 and 2008-10-28:
audit (5 changes) *
fulfillment (2 changes) *
persistence (3 changes) *
ui (8 changes) **
util (4 changes) *
inventory (2 changes) *
>Exit code: 0
[color=blue][b]** **[/b][/color]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章