visionmedia-rext 0.0.8 → 0.1.0

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.1.0 / 2009-05-04
3
+
4
+ * Added Module#call_method
5
+ * Added String#word_frequency and String#frequency_of_word
6
+
2
7
  === 0.0.8 / 2009-04-13
3
8
 
4
9
  * Added Array#chunk/in_groups_of
data/README.rdoc CHANGED
@@ -100,7 +100,7 @@ well as forward compatibility for features such as BasicObject.
100
100
  == Contrib
101
101
 
102
102
  Feel free to fork Rext on github, or submit a ticket:
103
- http://visionmedia.lighthouseapp.com/projects/28074-rext/overview
103
+ http://github.com/visionmedia/rext/issues
104
104
 
105
105
  == License
106
106
 
@@ -8,4 +8,46 @@ class Module
8
8
  include Module.new(&block)
9
9
  end
10
10
 
11
+ ##
12
+ # Call +method+ when conditions are met.
13
+ #
14
+ # This method provides a simple means of
15
+ # utilizing method_missing, and should
16
+ # be used in most cases.
17
+ #
18
+ # When using a regular expression with
19
+ # the :if option, all captures are passed
20
+ # to the handling method.
21
+ #
22
+ # === Examples
23
+ #
24
+ # class Foo
25
+ # call_method :find_by, :if => /^find_by_(\w+)/
26
+ # def find_by attr, *args
27
+ # "finding by #{attr} with #{args.join(', ')}"
28
+ # end
29
+ # end
30
+ #
31
+ # foo = Foo.new
32
+ # foo.find_by_name('bar') # => "finding by name with bar"
33
+ #
34
+ # === Options
35
+ #
36
+ # :if regexp or proc
37
+ #
38
+
39
+ def call_method method, options = {}
40
+ chain {
41
+ define_method :method_missing do |meth, *args|
42
+ if options[:if].is_a?(Regexp) && meth.to_s =~ options[:if]
43
+ send method, *($~.captures + args)
44
+ elsif options[:if].respond_to?(:call) && options[:if].call(meth, *args)
45
+ send method, *args
46
+ else
47
+ super
48
+ end
49
+ end
50
+ }
51
+ end
52
+
11
53
  end
data/lib/rext/object.rb CHANGED
@@ -4,4 +4,4 @@
4
4
  #++
5
5
 
6
6
  require 'rext/object/helpers'
7
- require 'rext/object/metaclass'
7
+ require 'rext/object/metaclass'
@@ -15,7 +15,7 @@ class Object
15
15
  #
16
16
 
17
17
  def indifferent_hash
18
- Hash.new { |hash,key| hash[key.to_s] if key.is_a? Symbol }
18
+ Hash.new { |hash, key| hash[key.to_s] if key.is_a? Symbol }
19
19
  end
20
20
 
21
21
  end
@@ -220,6 +220,35 @@ class String
220
220
  tr '_', '-'
221
221
  end
222
222
 
223
+ ##
224
+ # Return hash of word frequencies.
225
+ #
226
+ # === Examples
227
+ #
228
+ # 'foo foo bar'.word_frequency
229
+ # # => { 'foo' => 2, 'bar' => 1 }
230
+ #
231
+
232
+ def word_frequency
233
+ split.inject Hash.new(0) do |frequencies, word|
234
+ frequencies[word] += 1
235
+ frequencies
236
+ end
237
+ end
238
+
239
+ ##
240
+ # Return frequency of _word_ or 0.
241
+ #
242
+ # === Examples
243
+ #
244
+ # 'foo foo bar'.frequency_of_word('foo') # => 2
245
+ # 'foo foo bar'.frequency_of_word('bar') # => 1
246
+ #
247
+
248
+ def frequency_of_word word
249
+ word_frequency[word]
250
+ end
251
+
223
252
  ##
224
253
  # Returns the switch equivilant of this string.
225
254
  #
data/lib/rext/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Rext
3
- VERSION = '0.0.8'
3
+ VERSION = '0.1.0'
4
4
  end
data/rext.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rext}
5
- s.version = "0.0.8"
5
+ s.version = "0.1.0"
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
- s.date = %q{2009-04-13}
9
+ s.date = %q{2009-05-04}
10
10
  s.description = %q{Ruby extensions}
11
11
  s.email = %q{tj@vision-media.ca}
12
12
  s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/compat/basic_object.rb", "lib/rext/compat.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.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.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"]
@@ -16,12 +16,12 @@ Gem::Specification.new do |s|
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rext", "--main", "README.rdoc"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubyforge_project = %q{rext}
19
- s.rubygems_version = %q{1.3.1}
19
+ s.rubygems_version = %q{1.3.2}
20
20
  s.summary = %q{Ruby extensions}
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
- s.specification_version = 2
24
+ s.specification_version = 3
25
25
 
26
26
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
27
  else
data/spec/module_spec.rb CHANGED
@@ -3,33 +3,19 @@ require 'rext/module'
3
3
 
4
4
  describe Module do
5
5
  describe "helpers" do
6
- describe "#chain" do
7
- it "should allow chaining via super" do
8
- class Foo
9
- def method_missing meth
10
- if meth.to_s =~ /^say_(\w+)/
11
- "said #{$1}"
12
- else
13
- super
14
- end
15
- end
6
+ describe "#proxy_method" do
7
+ it "should implement method_missing, passing regexp captures and arguments to the handler method" do
8
+ class FooBar
9
+ call_method :find, :if => /^find_by_(\w+)_(\w+)/
10
+ call_method :delete, :if => lambda { |meth, *args| meth.to_s =~ /^delete_by_(\w+)_(\w+)/ }
11
+ def find *args; args end
12
+ def delete *args; args; end
16
13
  end
17
-
18
- class Foo
19
- chain {
20
- def method_missing meth
21
- if meth.to_s =~ /^ask_for_(\w+)/
22
- "asked for #{$1}"
23
- else
24
- super
25
- end
26
- end
27
- }
28
- end
29
-
30
- Foo.new.ask_for_array.should == 'asked for array'
31
- Foo.new.say_hello.should == 'said hello'
32
- lambda { Foo.new.does_not_exist }.should raise_error(NoMethodError)
14
+ foo = FooBar.new
15
+ foo.find_by_user_email.should == ['user', 'email']
16
+ foo.find_by_user_name('foo').should == ['user', 'name', 'foo']
17
+ foo.delete_by_user_name('foo', 'bar').should == ['foo', 'bar']
18
+ lambda { foo.does_not_exist }.should raise_error(NoMethodError)
33
19
  end
34
20
  end
35
21
  end
data/spec/string_spec.rb CHANGED
@@ -4,6 +4,26 @@ require 'rext/string'
4
4
  describe String do
5
5
  describe "helpers" do
6
6
 
7
+ describe "#word_frequency" do
8
+ it "should return a hash with word keys and count values" do
9
+ 'mm i love cookies mm'.word_frequency.
10
+ should == {
11
+ 'mm' => 2,
12
+ 'i' => 1,
13
+ 'love' => 1,
14
+ 'cookies' => 1,
15
+ }
16
+ end
17
+ end
18
+
19
+ describe "#frequency_of_word" do
20
+ it "should return the frequency of a word, or 0" do
21
+ 'yum yum ? yes'.frequency_of_word('yum').should == 2
22
+ 'yum yum ? yes'.frequency_of_word('yes').should == 1
23
+ 'yum yum ? yes'.frequency_of_word('no').should == 0
24
+ end
25
+ end
26
+
7
27
  describe "#switchify" do
8
28
  it "should return a switch version of the string" do
9
29
  'some_foo_bar'.switchify.should == '--some-foo-bar'
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.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Holowaychuk
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-13 00:00:00 -07:00
12
+ date: 2009-05-04 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -138,7 +138,7 @@ requirements: []
138
138
  rubyforge_project: rext
139
139
  rubygems_version: 1.2.0
140
140
  signing_key:
141
- specification_version: 2
141
+ specification_version: 3
142
142
  summary: Ruby extensions
143
143
  test_files: []
144
144