visionmedia-rext 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.rdoc CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ === 0.0.2 / 2009-03-23
3
+
4
+ * Added Proc#yield_or_eval docs
5
+ * Renamed class/helpers to module/helpers
6
+
2
7
  === 0.0.1 / 2009-03-23
3
8
 
4
9
  * Initial release
data/Manifest CHANGED
@@ -1,18 +1,40 @@
1
+ benchmarks/enumerable.rb
2
+ benchmarks/proc.rb
1
3
  History.rdoc
2
4
  lib/rext/all.rb
5
+ lib/rext/date/helpers.rb
6
+ lib/rext/date.rb
7
+ lib/rext/enumerable/helpers.rb
8
+ lib/rext/enumerable.rb
9
+ lib/rext/hash/helpers.rb
10
+ lib/rext/hash.rb
11
+ lib/rext/integer/helpers.rb
3
12
  lib/rext/integer/time.rb
4
13
  lib/rext/integer.rb
14
+ lib/rext/module/helpers.rb
15
+ lib/rext/module.rb
16
+ lib/rext/proc/helpers.rb
17
+ lib/rext/proc.rb
5
18
  lib/rext/string/escape.rb
6
19
  lib/rext/string/helpers.rb
7
20
  lib/rext/string.rb
21
+ lib/rext/time/helpers.rb
22
+ lib/rext/time.rb
8
23
  lib/rext/version.rb
9
24
  lib/rext.rb
10
25
  Manifest
11
26
  Rakefile
12
27
  README.rdoc
28
+ rext.gemspec
29
+ spec/date_spec.rb
30
+ spec/enumerable_spec.rb
31
+ spec/hash_spec.rb
13
32
  spec/integer_spec.rb
33
+ spec/module_spec.rb
14
34
  spec/spec_helper.rb
15
35
  spec/string_spec.rb
36
+ spec/time_spec.rb
37
+ tasks/benchmark.rake
16
38
  tasks/docs.rake
17
39
  tasks/gemspec.rake
18
40
  tasks/spec.rake
@@ -0,0 +1,14 @@
1
+
2
+ list = %w( just some random foo bar like cookies )
3
+
4
+ benchmark 'Enumerable#group_by', :times => $times do
5
+ report 'with block' do
6
+ list.group_by { |item| item.length }
7
+ end
8
+ report 'with evaled block' do
9
+ list.group_by { length }
10
+ end
11
+ report 'with proxy' do
12
+ list.group_by.length
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+
2
+ def yield_or_eval_test &block
3
+ block.yield_or_eval 'foo'
4
+ end
5
+
6
+ def manual &block
7
+ foo = 'foo'
8
+ if block.arity > 0
9
+ yield foo
10
+ else
11
+ foo.instance_eval &block
12
+ end
13
+ end
14
+
15
+ benchmark 'Proc', :times => $times do
16
+ report '#yield_or_eval' do
17
+ yield_or_eval_test { length }
18
+ yield_or_eval_test { |foo| foo.length }
19
+ end
20
+ report 'manual arity check' do
21
+ manual { length }
22
+ manual { |foo| foo.length }
23
+ end
24
+ end
data/lib/rext/all.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # Load all extensions.
4
4
  #++
5
5
 
6
- require 'rext/class'
6
+ require 'rext/module'
7
7
  require 'rext/proc'
8
8
  require 'rext/enumerable'
9
9
  require 'rext/hash'
@@ -0,0 +1,12 @@
1
+
2
+ class Date
3
+
4
+ ##
5
+ # Convert Date to a Time instance.
6
+ #
7
+
8
+ def to_time
9
+ Time.utc year, month, day
10
+ end
11
+
12
+ end
data/lib/rext/date.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+ #--
3
+ # Load Date specific rext extensions.
4
+ #++
5
+
6
+ require 'rext/date/helpers'
@@ -0,0 +1,75 @@
1
+
2
+ require 'rext/module/helpers'
3
+ require 'rext/proc/helpers'
4
+
5
+ module Enumerable
6
+
7
+ ##
8
+ # Proxy
9
+ #
10
+ # The enumerable proxy class is utilized to
11
+ # allow methods such as #group_by, #map, and others
12
+ # to provide a method call alternative to a block.
13
+ #
14
+ # For example words.group_by { |word| word.length } can
15
+ # be replaced by words.group_by_.length
16
+ #
17
+ # === See
18
+ #
19
+ # * Enumerable#proxy
20
+ #
21
+
22
+ class Proxy
23
+ instance_methods.each { |m| undef_method m unless m.match(/^__/) }
24
+
25
+ def initialize object, meth
26
+ @object, @method = object, meth
27
+ end
28
+
29
+ def method_missing meth, *args, &block
30
+ @object.__send__(@method) { |o| o.__send__(meth, *args, &block) }
31
+ end
32
+ end
33
+
34
+ ##
35
+ # Shortcut for Proxy.new self, +meth+.
36
+
37
+ def proxy meth
38
+ Proxy.new self, meth
39
+ end
40
+
41
+ ##
42
+ # Return a hash grouped by +block+.
43
+ #
44
+ # === Examples
45
+ #
46
+ # words = %w( just some foo bar )
47
+ # words.group_by { |word| word.length } # => {3=>["foo", "bar"], 4=>["just", "some"]}
48
+ # words.group_by { length } # => {3=>["foo", "bar"], 4=>["just", "some"]}
49
+ # words.group_by.length # => {3=>["foo", "bar"], 4=>["just", "some"]}
50
+ #
51
+
52
+ def group_by &block
53
+ return proxy(:group_by) unless block
54
+ inject({}) do |hash, value|
55
+ (hash[block.yield_or_eval(value)] ||= []) << value
56
+ hash
57
+ end
58
+ end
59
+
60
+ ##
61
+ # Sexy Symbol#to_proc replacement for mapping enums.
62
+ #
63
+ # === Examples
64
+ #
65
+ # names = %w( tj scott joe bob )
66
+ # names.every.length.join # => 2533
67
+ # names.every.empty?.any? # => false
68
+ # names.every { length > 4 }.all? # => true
69
+ #
70
+
71
+ def every &block
72
+ block ? proxy(:map).instance_eval(&block) : proxy(:map)
73
+ end
74
+
75
+ end
@@ -0,0 +1,6 @@
1
+
2
+ #--
3
+ # Load Enumerable specific rext extensions.
4
+ #++
5
+
6
+ require 'rext/enumerable/helpers'
@@ -0,0 +1,23 @@
1
+
2
+ class Hash
3
+
4
+ ##
5
+ # Delete key-value pairs, returning the values found
6
+ # using the +keys+ passed. Aliased as extract!
7
+ #
8
+ # === Examples
9
+ #
10
+ # options = { :width => 25, :height => 100 }
11
+ # width, height = options.delete_at :width, :height
12
+ #
13
+ # width # => 25
14
+ # height # => 100
15
+ # options # => {}
16
+ #
17
+
18
+ def delete_at *keys
19
+ keys.map { |key| delete key }
20
+ end
21
+ alias :extract! :delete_at
22
+
23
+ end
data/lib/rext/hash.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+ #--
3
+ # Load Hash specific rext extensions.
4
+ #++
5
+
6
+ require 'rext/hash/helpers'
@@ -0,0 +1,29 @@
1
+
2
+ class Integer
3
+
4
+ ##
5
+ # Ordinalize turns a number into an ordinal string used to denote the
6
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
7
+ #
8
+ # === Examples
9
+ #
10
+ # 1.ordinalize # => "1st"
11
+ # 2.ordinalize # => "2nd"
12
+ # 1002.ordinalize # => "1002nd"
13
+ # 1003.ordinalize # => "1003rd"
14
+ #
15
+
16
+ def ordinalize
17
+ if (11..13).include? self % 100
18
+ "#{self}th"
19
+ else
20
+ case self % 10
21
+ when 1 ; "#{self}st"
22
+ when 2 ; "#{self}nd"
23
+ when 3 ; "#{self}rd"
24
+ else "#{self}th"
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,11 @@
1
+
2
+ class Module
3
+
4
+ ##
5
+ # Shortcut for including an anonymous module.
6
+
7
+ def chain &block
8
+ include Module.new(&block)
9
+ end
10
+
11
+ end
@@ -0,0 +1,6 @@
1
+
2
+ #--
3
+ # Load Module specific rext extensions.
4
+ #++
5
+
6
+ require 'rext/module/helpers'
@@ -0,0 +1,22 @@
1
+
2
+ class Proc
3
+
4
+ ##
5
+ # Yield or instance evaluate the +object+ passed
6
+ # based on this proc's arity.
7
+ #
8
+ # === Examples
9
+ #
10
+ # def foo &block
11
+ # block.yield_or_eval 'bar'
12
+ # end
13
+ #
14
+ # foo { |v| v.length } # => 3
15
+ # foo { length } # => 3
16
+ #
17
+
18
+ def yield_or_eval object
19
+ arity > 0 ? self.call(object) : object.instance_eval(&self)
20
+ end
21
+
22
+ end
data/lib/rext/proc.rb ADDED
@@ -0,0 +1,7 @@
1
+
2
+
3
+ #--
4
+ # Load Proc specific rext extensions.
5
+ #++
6
+
7
+ require 'rext/proc/helpers'
@@ -0,0 +1,43 @@
1
+
2
+ class Time
3
+
4
+ def to_time #:nodoc:
5
+ self
6
+ end
7
+
8
+ ##
9
+ # Time in words since +time+ or now.
10
+ #
11
+ # === Examples
12
+ #
13
+ # 5.seconds.ago.in_words_since_now # => less than one minute
14
+ # 5.days.ago.in_words_since_now # => 5 days
15
+ # 1.month.ago.in_words_since_now # => 1 month
16
+ # 101.years.ago.in_words_since_now # => hundreds of years
17
+ #
18
+ # "the article was published #{article.created_at.in_words_since_now} ago"
19
+ # # => the article was published 15 minutes ago
20
+ #
21
+
22
+ def in_words_since time = Time.now
23
+ return if self > time
24
+ seconds = (time - self).to_i
25
+ # TODO: abstract this out
26
+ pluralize = lambda do |type|
27
+ n = seconds.send(:"to_#{type}s")
28
+ n == 1 ? "one #{type}" : "#{n} #{type}s"
29
+ end
30
+ case seconds
31
+ when 0..59 ; 'less than one minute'
32
+ when 1.minute..59.minutes ; pluralize[:minute]
33
+ when 1.hour..23.hours ; pluralize[:hour]
34
+ when 1.day..6.days ; pluralize[:day]
35
+ when 1.week..3.weeks ; pluralize[:week]
36
+ when 1.month..11.months ; pluralize[:month]
37
+ when 1.year..99.years ; pluralize[:year]
38
+ else 'hundreds of years'
39
+ end
40
+ end
41
+ alias :in_words_since_now :in_words_since
42
+
43
+ end
data/lib/rext/time.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+ #--
3
+ # Load Time specific rext extensions.
4
+ #++
5
+
6
+ require 'rext/time/helpers'
data/lib/rext/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Rext
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
data/rext.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rext}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["TJ Holowaychuk"]
9
9
  s.date = %q{2009-03-23}
10
10
  s.description = %q{Ruby extensions}
11
11
  s.email = %q{tj@vision-media.ca}
12
- s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/integer/time.rb", "lib/rext/integer.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/version.rb", "lib/rext.rb", "README.rdoc", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
- s.files = ["History.rdoc", "lib/rext/all.rb", "lib/rext/integer/time.rb", "lib/rext/integer.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/version.rb", "lib/rext.rb", "Manifest", "Rakefile", "README.rdoc", "spec/integer_spec.rb", "spec/spec_helper.rb", "spec/string_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "Todo.rdoc", "rext.gemspec"]
12
+ s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer/time.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "README.rdoc", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["benchmarks/enumerable.rb", "benchmarks/proc.rb", "History.rdoc", "lib/rext/all.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer/time.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "Manifest", "Rakefile", "README.rdoc", "rext.gemspec", "spec/date_spec.rb", "spec/enumerable_spec.rb", "spec/hash_spec.rb", "spec/integer_spec.rb", "spec/module_spec.rb", "spec/spec_helper.rb", "spec/string_spec.rb", "spec/time_spec.rb", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "Todo.rdoc"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://github.com/visionmedia/rext}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rext", "--main", "README.rdoc"]
data/spec/date_spec.rb ADDED
@@ -0,0 +1,15 @@
1
+
2
+ require 'rext/date'
3
+
4
+ describe Date do
5
+ describe "helpers" do
6
+
7
+ describe "#to_time" do
8
+ it "should convert to a Time instance" do
9
+ event = Date.parse('may 25th 1987')
10
+ event.to_time.should be_an_instance_of(Time)
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+
2
+ require 'rext/enumerable'
3
+
4
+ describe Enumerable do
5
+
6
+ describe "#every" do
7
+ it "should map a method call" do
8
+ list = %w( cookies icecream sugar )
9
+ list.every.length.should == [7, 8 ,5]
10
+ end
11
+
12
+ it "should allow mapping shortcuts using a block" do
13
+ people = []
14
+ people << mock('Person', :name => 'TJ')
15
+ people << mock('Person', :name => 'Scott')
16
+ people.every { name.length }.should == [2, 5]
17
+ end
18
+ end
19
+
20
+ describe "helpers" do
21
+ describe "#group_by" do
22
+ before :each do
23
+ @enum = 'foo', :bar, 1, 2, 3
24
+ @results = {
25
+ String => ['foo'],
26
+ Symbol => [:bar],
27
+ Fixnum => [1, 2, 3]
28
+ }
29
+ end
30
+
31
+ it "should group an enum" do
32
+ @enum.group_by { |v| v.class }.should == @results
33
+ end
34
+
35
+ it "should group using instance evaluated block" do
36
+ @enum.group_by { self.class }.should == @results
37
+ end
38
+
39
+ it "should group by method call" do
40
+ @enum.group_by.class.should == @results
41
+ end
42
+ end
43
+ end
44
+
45
+ end
data/spec/hash_spec.rb ADDED
@@ -0,0 +1,18 @@
1
+
2
+ require 'rext/hash'
3
+
4
+ describe Hash do
5
+ describe "helpers" do
6
+
7
+ describe "#delete_at" do
8
+ it "should delete several keys, returning their values when present" do
9
+ options = { :foo => 1, :bar => 2 }
10
+ foo, bar, does_not_exist = options.delete_at :foo, :bar, :does_not_exist
11
+ foo.should == 1
12
+ bar.should == 2
13
+ does_not_exist.should be_nil
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+
2
+ require 'rext/module'
3
+
4
+ describe Module do
5
+ describe "helpers" do
6
+
7
+ describe "#chain" do
8
+ it "should allow chaining via super" do
9
+ class Foo
10
+ def method_missing meth
11
+ if meth.to_s =~ /^say_(\w+)/
12
+ "said #{$1}"
13
+ else
14
+ super
15
+ end
16
+ end
17
+ end
18
+
19
+ class Foo
20
+ chain {
21
+ def method_missing meth
22
+ if meth.to_s =~ /^ask_for_(\w+)/
23
+ "asked for #{$1}"
24
+ else
25
+ super
26
+ end
27
+ end
28
+ }
29
+ end
30
+
31
+ Foo.new.ask_for_array.should == 'asked for array'
32
+ Foo.new.say_hello.should == 'said hello'
33
+ lambda { Foo.new.does_not_exist }.should raise_error(NoMethodError)
34
+ end
35
+ end
36
+
37
+ end
38
+ end
data/spec/time_spec.rb ADDED
@@ -0,0 +1,23 @@
1
+
2
+ require 'rext/time'
3
+
4
+ describe Time do
5
+ describe "helpers" do
6
+
7
+ describe "#in_words_since" do
8
+ it "should convert time lost to words" do
9
+ event = Time.now - 5.seconds
10
+ event.in_words_since_now.should == 'less than one minute'
11
+ event -= 1.minute
12
+ event.in_words_since_now.should == 'one minute'
13
+ event -= 2.minutes
14
+ event.in_words_since_now.should == '3 minutes'
15
+ event -= 5.years
16
+ event.in_words_since_now.should == '5 years'
17
+ event -= 100.years
18
+ event.in_words_since_now.should == 'hundreds of years'
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+
2
+ desc 'Run benchmark suites n TIMES, defaulting to 1000'
3
+ task :benchmark do
4
+ $times = ENV['TIMES'] || 1000
5
+ begin
6
+ require 'rext/all'
7
+ require 'rubygems'
8
+ require 'rgauge'
9
+ rescue LoadError
10
+ abort 'visionmedia-rgauge is required, grab it from github'
11
+ end
12
+ Dir['benchmarks/*.rb'].each { |f| load f }
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visionmedia-rext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -21,38 +21,73 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - lib/rext/all.rb
24
+ - lib/rext/date/helpers.rb
25
+ - lib/rext/date.rb
26
+ - lib/rext/enumerable/helpers.rb
27
+ - lib/rext/enumerable.rb
28
+ - lib/rext/hash/helpers.rb
29
+ - lib/rext/hash.rb
30
+ - lib/rext/integer/helpers.rb
24
31
  - lib/rext/integer/time.rb
25
32
  - lib/rext/integer.rb
33
+ - lib/rext/module/helpers.rb
34
+ - lib/rext/module.rb
35
+ - lib/rext/proc/helpers.rb
36
+ - lib/rext/proc.rb
26
37
  - lib/rext/string/escape.rb
27
38
  - lib/rext/string/helpers.rb
28
39
  - lib/rext/string.rb
40
+ - lib/rext/time/helpers.rb
41
+ - lib/rext/time.rb
29
42
  - lib/rext/version.rb
30
43
  - lib/rext.rb
31
44
  - README.rdoc
45
+ - tasks/benchmark.rake
32
46
  - tasks/docs.rake
33
47
  - tasks/gemspec.rake
34
48
  - tasks/spec.rake
35
49
  files:
50
+ - benchmarks/enumerable.rb
51
+ - benchmarks/proc.rb
36
52
  - History.rdoc
37
53
  - lib/rext/all.rb
54
+ - lib/rext/date/helpers.rb
55
+ - lib/rext/date.rb
56
+ - lib/rext/enumerable/helpers.rb
57
+ - lib/rext/enumerable.rb
58
+ - lib/rext/hash/helpers.rb
59
+ - lib/rext/hash.rb
60
+ - lib/rext/integer/helpers.rb
38
61
  - lib/rext/integer/time.rb
39
62
  - lib/rext/integer.rb
63
+ - lib/rext/module/helpers.rb
64
+ - lib/rext/module.rb
65
+ - lib/rext/proc/helpers.rb
66
+ - lib/rext/proc.rb
40
67
  - lib/rext/string/escape.rb
41
68
  - lib/rext/string/helpers.rb
42
69
  - lib/rext/string.rb
70
+ - lib/rext/time/helpers.rb
71
+ - lib/rext/time.rb
43
72
  - lib/rext/version.rb
44
73
  - lib/rext.rb
45
74
  - Manifest
46
75
  - Rakefile
47
76
  - README.rdoc
77
+ - rext.gemspec
78
+ - spec/date_spec.rb
79
+ - spec/enumerable_spec.rb
80
+ - spec/hash_spec.rb
48
81
  - spec/integer_spec.rb
82
+ - spec/module_spec.rb
49
83
  - spec/spec_helper.rb
50
84
  - spec/string_spec.rb
85
+ - spec/time_spec.rb
86
+ - tasks/benchmark.rake
51
87
  - tasks/docs.rake
52
88
  - tasks/gemspec.rake
53
89
  - tasks/spec.rake
54
90
  - Todo.rdoc
55
- - rext.gemspec
56
91
  has_rdoc: true
57
92
  homepage: http://github.com/visionmedia/rext
58
93
  post_install_message: