nakajima 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), 'nakajima')
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__), 'nakajima', 'core_ext')
3
+
4
+ # gems
5
+ require 'rubygems'
6
+ require 'colored'
7
+ require 'metaid'
8
+
9
+ # core ruby extensions
10
+ require 'basic_object'
11
+ require 'array'
12
+ require 'symbol'
13
+ require 'proc'
14
+ require 'object'
15
+
16
+ module Nakajima
17
+ VERSION = '0.0.4'
18
+ end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def extract_options!
3
+ last.is_a?(Hash) ? pop : { }
4
+ end unless [].respond_to?(:extract_options!)
5
+ end
@@ -0,0 +1,5 @@
1
+ module Nakajima
2
+ class BasicObject
3
+ instance_methods.reject { |m| m =~ /^__/ }.each { |m| undef_method m }
4
+ end
5
+ end
@@ -0,0 +1,62 @@
1
+ module Nakajima
2
+ module Object
3
+ module InstanceMethods
4
+ def try(method_id, *args, &block)
5
+ respond_to?(method_id) ? send(method_id, *args, &block) : nil
6
+ end
7
+
8
+ # Like JavaScript, but in Ruby...
9
+ def with(hash)
10
+ hash.each do |key, value|
11
+ meta_def(key) { hash[key] }
12
+ meta_def("#{key}=") { |v| hash[key] = v }
13
+ end
14
+
15
+ return unless block_given?
16
+
17
+ result = yield
18
+
19
+ hash.each do |key, value|
20
+ meta_eval { remove_method(key) }
21
+ meta_eval { remove_method("#{key}=") }
22
+ end
23
+
24
+ result
25
+ end
26
+
27
+ unless respond_to?(:instance_exec)
28
+ def instance_exec(*arguments, &block)
29
+ block.bind(self)[*arguments]
30
+ end
31
+ end
32
+
33
+ # A proxy that returns its target after invoking the method you
34
+ # invoke. Useful for #me
35
+ class ProxyReturningMe < BasicObject
36
+ def initialize(me)
37
+ super()
38
+ @me = me
39
+ end
40
+ def method_missing(sym, *args, &block)
41
+ @me.__send__(sym, *args, &block)
42
+ @me
43
+ end
44
+ end
45
+
46
+ # plucked from raganwald's andand
47
+ def tap(p = nil)
48
+ if block_given?
49
+ yield(self)
50
+ self
51
+ elsif p
52
+ p.to_proc.call(self)
53
+ self
54
+ else
55
+ ProxyReturningMe.new(self)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ Object.send :include, Nakajima::Object::InstanceMethods
@@ -0,0 +1,13 @@
1
+ # from activesupport
2
+ class Proc
3
+ def bind(object)
4
+ block, time = self, Time.now
5
+ (class << object; self end).class_eval do
6
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
7
+ define_method(method_name, &block)
8
+ method = instance_method(method_name)
9
+ remove_method(method_name)
10
+ method
11
+ end.bind(object)
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ class Symbol
2
+ def to_proc
3
+ Proc.new { |i| i.send(self) }
4
+ end
5
+ end unless :test.respond_to?(:to_proc)
@@ -0,0 +1,33 @@
1
+ # For making nicer CLI tools that take a while to do things
2
+ #
3
+ # Example:
4
+ #
5
+ # with_progress do
6
+ # sleep 2 # or some slow task
7
+ # end
8
+ #
9
+ # You can also pass in options to customize messages as well
10
+ # as the rate of the progress output.
11
+ def with_progress(options={})
12
+ options[:before] ||= "" # Message to print before starting
13
+ options[:after] ||= "" # Message to print after completion
14
+ options[:char] ||= '.' # Character to be printed as progress
15
+ options[:rate] ||= 0.2 # Delay between printing :char option
16
+ options[:change] ||= 1 # Allows for the rate to accelerate/decelerate
17
+
18
+ print options[:before]
19
+
20
+ thread = Thread.new do
21
+ printer = proc do |interval|
22
+ print(options[:char])
23
+ $stdout.flush
24
+ sleep interval
25
+ printer.call [interval * options[:change], 0.01].max
26
+ end
27
+ printer.call options[:rate]
28
+ end
29
+
30
+ yield and thread.kill
31
+
32
+ puts options[:after]
33
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nakajima
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Pat Nakajima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-04 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: colored
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: metaid
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rr
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description:
46
+ email: patnakajima@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ files:
54
+ - lib/nakajima
55
+ - lib/nakajima/core_ext
56
+ - lib/nakajima/core_ext/basic_object.rb
57
+ - lib/nakajima/core_ext/array.rb
58
+ - lib/nakajima/core_ext/proc.rb
59
+ - lib/nakajima/core_ext/object.rb
60
+ - lib/nakajima/core_ext/symbol.rb
61
+ - lib/nakajima/ui
62
+ - lib/nakajima/ui/progress.rb
63
+ - lib/nakajima.rb
64
+ has_rdoc: false
65
+ homepage: http://github.com/nakajima/nakajima
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.0
87
+ signing_key:
88
+ specification_version: 2
89
+ summary: Stuff I use 2 or 10 projects a project.
90
+ test_files: []
91
+