`
sco.struts
  • 浏览: 18909 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Everyday Scripting with Ruby 读书笔记(4)

    博客分类:
  • Ruby
阅读更多
---
用类捆绑数据和方法
Classes Bundle Data and Methods
---


# churn-classes.Sub.mine.rb

class SubversionRepository

  def initialize(root)
    @root = root
  end

  # 格式化时间
  def date(a_time)
    a_time.strftime("%Y-%m-%d")
  end
  
  # 计算修改次数 
  def change_count_for(name, start_date)
    extract_change_count_from(log(name, date(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 log(subsystem, start_date)
    timespan = "--revision HEAD:{#{start_date}}"
    
    `svn log #{timespan} #{@root}/#{subsystem}`
  end
end


# churn-classes.For.mine.rb

class Formatter
  
  def initialize
    @lines = []
  end
  
  #def use_date(date_string)
  #  @date_string = date_string
  #end
  
  def report_range(from_date, to_date)
    @from_date = from_date  # 通过实例变量实现封装
    @to_date = to_date
  end
  
  # 格式化时间
  def date(a_time)
    a_time.strftime("%Y-%m-%d")
  end
  
  def use_subsystem_with_change_count(name, count)
    @lines.push(subsystem_line(name, count))
  end
  
  def output
    ordered_lines = order_by_descending_change_count(@lines)
    return ([header] + ordered_lines).join("\n")  # 组成字符串数组,并通过join转换成字符串
  end
  
  # 处理打印标题信息
  def header
    "Changes between #{date(@from_date)} and #{date(@to_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 order_by_descending_change_count(lines)
    lines.sort do | first, second |
      first_count = churn_line_to_int(first)
      second_count = churn_line_to_int(second)
      - (first_count <=> second_count)
    end
  end

  def churn_line_to_int(line)
    /\((\d+)\)/.match(line)[1].to_i
  end
end


# Exercise
# churn-classes.Churn.mine.rb

require 'churn-classes.Sub.mine'
require 'churn-classes.For.mine'

# 处理时间(过去一个月)   
def month_before(a_time)
  a_time - 28 * 24 * 60 * 60
end


if $0 == __FILE__
  subsystem_names = ['audit', 'fulfillment', 'persistence',
                     'ui', 'util', 'inventory']
  root="svn://rubyforge.org//var/svn/churn-demo"
  repository = SubversionRepository.new(root)
  
  #start_date = repository.date(month_before(Time.mktime(2005,9,2,0,0,0,0)))
  last_month = month_before(Time.mktime(2005,9,2,0,0,0,0))
  
  formatter = Formatter.new
  #formatter.use_date(start_date)
  formatter.report_range(last_month, Time.mktime(2005,9,2,0,0,0,0))

  subsystem_names.each do | name |
    formatter.use_subsystem_with_change_count(name,
                   repository.change_count_for(name, last_month))
  end
  puts formatter.output
end


** 运行结果 **
>ruby churn-classes.Churn.mine.rb
Changes between 2005-08-05 and 2005-09-02:
            ui ** (8)
         audit * (5)
          util * (4)
   persistence * (3)
   fulfillment  (2)
     inventory  (2)
>Exit code: 0
** **
分享到:
评论

相关推荐

    everyday scripting with ruby

    everyday scripting with ruby

    Everyday Scripting With Ruby

    《Everyday Scripting with Ruby》是一本综合性的Ruby教程,涵盖了从基础知识到高级技术的各个方面。通过实践导向的教学方法,读者不仅可以学习到Ruby的核心概念,还能掌握如何在真实环境中应用这些知识。无论是初学...

    Unreal Engine 4 Scripting with C++ Cookbook first edition.pdf

    Unreal Engine 4 Scripting with C++Cookbook Get the best out of your games by scripting them using UE4 William Sherif Stephen Whittle 2016版

    Unreal Engine 4.x scripting with C cookbook

    Unreal Engine 4.x scripting with C cookbook develop quality game components and solve scripting problems with the power of C and UE4 by Stephen Whittle John P. Doran William Sherif (z-lib.org)

    Unreal Engine 4 Scripting with C++.pdf

    标题《Unreal Engine 4 Scripting with C++》指明了这本书是关于如何使用C++语言进行Unreal Engine 4(UE4)游戏引擎的脚本编程。UE4是Epic Games开发的一款先进的游戏引擎,广泛应用于现代游戏开发中,它支持C++...

    Unreal Engine 4 Scripting with C++ Cookbook

    Unreal Engine 4 Scripting with C++ Cookbook 2016 | ISBN-10: 1785885545 | 431 pages | PDF | 7 MB Key Features A straightforward and easy-to-follow format A selection of the most important tasks and ...

    Scripting With AD Scripting With AD

    标题 "Scripting With AD" 暗示了我们即将探讨的是使用脚本语言与Active Directory(AD)进行交互的主题。Active Directory是微软Windows操作系统中的一个关键组件,它用于存储和管理网络资源,如用户账户、计算机...

    Unreal Engine 4 Scripting with C++ Cookbook - 2016.pdf (带彩色插图)

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本面向游戏开发者的专业书籍,专门介绍了如何使用C++语言结合Unreal Engine 4(UE4)进行游戏脚本编写。本书在2016年10月发布了新版,并且包含彩色插图,使得...

    Unreal Engine 4 Scripting with C++ Cookbook pdf 0分

    标题和描述中提到的《Unreal Engine 4 Scripting with C++ Cookbook》是一本关于Unreal Engine 4(UE4)游戏引擎中使用C++编程语言进行脚本编程的实用指导书。这本书以食谱(Cookbook)的形式呈现,为读者提供了许多...

    Unreal Engine 4 Scripting with C++ Cookbook willam

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本专为游戏开发人员设计的实用指南,旨在帮助读者深入理解并掌握使用C++在Unreal Engine 4(UE4)中进行脚本编程的技术。这本书涵盖了从基础概念到高级技巧的...

    Unreal Engine 4 Scripting with C Cookbook (Will.pdf ) 彩色高清

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本为游戏开发者准备的实用指南,它教授如何利用C++在虚幻引擎4环境下进行高级脚本编写。通过这本书,开发者可以学习到如何优化游戏性能、实现复杂的游戏逻辑...

    Raven Scripting Java Builds With Ruby

    本书介绍了构建简单及方便的系统的方法(英语版) CHAPTER 1 Dreams of Build................CHAPTER 4 Divide and Conquer: Multimodule Projects......33 CHAPTER 5 Public or Private Repository..................47

    Advanced Bash-Scripting Guide 读书笔记

    在阅读《Advanced Bash-Scripting Guide》这本书的过程中,我们能学到许多有关Bash脚本的高级用法和技巧。这本书对于那些想要提升其Bash脚本编写能力的用户来说是一份宝贵的资料。接下来,我将根据给定文件的部分...

    Microsoft - Windows Scripting With Wmi(2007)

    《Microsoft - Windows Scripting With Wmi(2007)》是关于Windows Management Instrumentation(WMI)技术的一本PDF教程,旨在帮助IT专业人士深入理解和掌握利用WMI进行Windows脚本编程的技术。WMI是微软提供的一种...

Global site tag (gtag.js) - Google Analytics