Browse files

initial

  • Loading branch information...
0 parents commit 6b602f735ce996c95bf8ca30dd6b2e404fff0bb4 @toy committed Aug 10, 2008
Showing with 180 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +20 −0 LICENSE
  3. +4 −0 README
  4. +47 −0 Rakefile
  5. +2 −0 TODO
  6. +10 −0 lib/array.rb
  7. +10 −0 lib/integer.rb
  8. +45 −0 lib/progress.rb
  9. +14 −0 script/destroy
  10. +14 −0 script/generate
  11. +7 −0 spec/progress_spec.rb
  12. +2 −0 spec/spec_helper.rb
5 .gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+pkg
+doc
+*.gem
+*.swp
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 YOUR NAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 README
@@ -0,0 +1,4 @@
+progress
+========
+
+A gem that provides...
47 Rakefile
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+require 'rubygems/specification'
+require 'date'
+
+GEM = "progress"
+GEM_VERSION = "0.0.1"
+AUTHOR = "toy"
+EMAIL = "ivan@workisfun.ru"
+HOMEPAGE = ""
+SUMMARY = "Class to show progress during script run"
+
+spec = Gem::Specification.new do |s|
+ s.name = GEM
+ s.version = GEM_VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+
+ # Uncomment this to add a dependency
+ # s.add_dependency "foo"
+
+ s.require_path = 'lib'
+ s.autorequire = GEM
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+desc "install the gem locally"
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
+end
+
+desc "create a gemspec file"
+task :make_spec do
+ File.open("#{GEM}.gemspec", "w") do |file|
+ file.puts spec.to_ruby
+ end
+end
2 TODO
@@ -0,0 +1,2 @@
+TODO:
+Add your code to lib/<%= name %>.rb
10 lib/array.rb
@@ -0,0 +1,10 @@
+class Array
+ def each_with_progress(name)
+ Progress.start(name, length) do
+ each do |item|
+ yield item
+ Progress.step
+ end
+ end
+ end
+end
10 lib/integer.rb
@@ -0,0 +1,10 @@
+class Integer
+ def times_with_progress(name)
+ Progress.start(name, self) do
+ times do |i|
+ yield i
+ Progress.step
+ end
+ end
+ end
+end
45 lib/progress.rb
@@ -0,0 +1,45 @@
+class Progress
+ def self.start(name, total = 100)
+ $stdout.sync = true
+
+ # @mutex ||= Mutex.new
+
+ @each = total / 1000
+ @count = 0
+
+ @current = 0
+ @total = total
+ @name = name + ': %s'
+ message highight('...')
+ yield
+ message percent
+ puts
+ end
+
+ def self.step
+ # @mutex.synchronize do
+ @current += 1
+ if (@count += 1) >= @each
+ message highight(percent)
+ @count = 0
+ end
+ # end
+ end
+
+private
+
+ def self.percent
+ '%5.1f%%' % (@current * 100.0 / @total)
+ end
+
+ def self.message(s)
+ print "\r\e[0K#{@name % s}"
+ end
+
+ def self.highight(s)
+ "\e[1m#{s}\e[0m"
+ end
+end
+
+require 'array'
+require 'integer'
14 script/destroy
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
14 script/generate
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
7 spec/progress_spec.rb
@@ -0,0 +1,7 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "progress" do
+ it "should do nothing" do
+ true.should == true
+ end
+end
2 spec/spec_helper.rb
@@ -0,0 +1,2 @@
+$TESTING=true
+$:.push File.join(File.dirname(__FILE__), '..', 'lib')

0 comments on commit 6b602f7

Please sign in to comment.