darkhelmet-darkext 0.11.2 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ doc/
2
+ pkg/
3
+ ri/
4
+ coverage/
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Daniel Huckstep
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # darkext
2
+
3
+ ## DESCRIPTION:
4
+
5
+ Just some useful Ruby functionality. No particular focus, except usefulness
6
+
7
+ ## FEATURES:
8
+
9
+ * Adds to Array
10
+ * rotate, rotate_reverse
11
+ * sum, product
12
+ * squares, squares!
13
+ * random, randomize, randomize!
14
+ * Adds to TrueClass and FalseClass
15
+ * intern
16
+ * Adds to Hash
17
+ * with_defaults, with_defaults!
18
+ * Adds to Integer
19
+ * fact
20
+ * Adds to Numeric
21
+ * square, cube
22
+ * sqrt
23
+ * ln, log
24
+ * Adds to Object
25
+ * try
26
+ * is_boolean?
27
+ * Adds to String
28
+ * to_range
29
+ * exec
30
+ * print
31
+ * Adds IO package
32
+ * Adds statistical package
33
+
34
+ ## USAGE:
35
+
36
+ $ require 'darkext'
37
+
38
+ ## INSTALLATION:
39
+
40
+ * $ gem sources -a http://gems.github.com # unless you already have done this
41
+ * $ gem install darkhelmet-darkext
42
+
43
+ ## LICENSE:
44
+
45
+ See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = 'darkext'
7
+ s.summary = %q{Just some useful Ruby functionality}
8
+ s.email = 'darkhelmet@darkhelmetlive.com'
9
+ s.homepage = 'http://github.com/darkhelmet/darkext'
10
+ s.description = %q{Just some useful Ruby functionality. No particular focus, except usefulness}
11
+ s.authors = ['Daniel Huckstep']
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
17
+ require 'rake/rdoctask'
18
+ Rake::RDocTask.new do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'foo'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README*')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |t|
29
+ t.test_files = FileList['test/**/*_test.rb']
30
+ t.verbose = true
31
+ end
32
+ rescue LoadError
33
+ puts 'RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov'
34
+ end
35
+
36
+ begin
37
+ require 'spec/rake/spectask'
38
+ Spec::Rake::SpecTask.new do |t|
39
+ t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
40
+ end
41
+ rescue LoadError
42
+ puts 'rspec is not available. In order to run rspec, you must: sudo gem install rspec'
43
+ end
44
+
45
+ task :default => :spec
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ * mspec/rspec?
2
+ * remove testing crap if mspec/rspec?
3
+ * see above..seriously
4
+ * AI stuff
@@ -1,4 +1,4 @@
1
1
  ---
2
- :minor: 11
3
- :patch: 2
2
+ :patch: 0
4
3
  :major: 0
4
+ :minor: 12
@@ -0,0 +1,105 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{darkext}
8
+ s.version = "0.12.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel Huckstep"]
12
+ s.date = %q{2009-08-11}
13
+ s.default_executable = %q{sinatra-app}
14
+ s.description = %q{Just some useful Ruby functionality. No particular focus, except usefulness}
15
+ s.email = %q{darkhelmet@darkhelmetlive.com}
16
+ s.executables = ["sinatra-app"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ "History.txt",
24
+ "LICENSE",
25
+ "README.md",
26
+ "Rakefile",
27
+ "TODO",
28
+ "VERSION.yml",
29
+ "app_generators/sinatra_app_generator.rb",
30
+ "app_generators/templates/app.rb",
31
+ "app_generators/templates/config.ru",
32
+ "app_generators/templates/error.rb",
33
+ "app_generators/templates/gitignore",
34
+ "app_generators/templates/helpers.rb",
35
+ "app_generators/templates/http_method.rb",
36
+ "app_generators/templates/options.rb",
37
+ "bin/sinatra-app",
38
+ "darkext.gemspec",
39
+ "lib/darkext.rb",
40
+ "lib/darkext/array.rb",
41
+ "lib/darkext/beagle.rb",
42
+ "lib/darkext/boolean.rb",
43
+ "lib/darkext/fiber.rb",
44
+ "lib/darkext/float.rb",
45
+ "lib/darkext/hash.rb",
46
+ "lib/darkext/integer.rb",
47
+ "lib/darkext/io.rb",
48
+ "lib/darkext/net.rb",
49
+ "lib/darkext/numeric.rb",
50
+ "lib/darkext/object.rb",
51
+ "lib/darkext/sinatra.rb",
52
+ "lib/darkext/sitemap_generator.rb",
53
+ "lib/darkext/statistics.rb",
54
+ "lib/darkext/string.rb",
55
+ "lib/darkext/symbol.rb",
56
+ "spec/array_spec.rb",
57
+ "spec/beagle_spec.rb",
58
+ "spec/boolean_spec.rb",
59
+ "spec/fiber_spec.rb",
60
+ "spec/float_spec.rb",
61
+ "spec/hash_spec.rb",
62
+ "spec/integer_spec.rb",
63
+ "spec/io_spec.rb",
64
+ "spec/net_spec.rb",
65
+ "spec/numeric_spec.rb",
66
+ "spec/object_spec.rb",
67
+ "spec/spec.opts",
68
+ "spec/spec_helper.rb",
69
+ "spec/statistics_spec.rb",
70
+ "spec/string_spec.rb",
71
+ "spec/symbol_spec.rb"
72
+ ]
73
+ s.homepage = %q{http://github.com/darkhelmet/darkext}
74
+ s.rdoc_options = ["--charset=UTF-8"]
75
+ s.require_paths = ["lib"]
76
+ s.rubygems_version = %q{1.3.5}
77
+ s.summary = %q{Just some useful Ruby functionality}
78
+ s.test_files = [
79
+ "spec/array_spec.rb",
80
+ "spec/beagle_spec.rb",
81
+ "spec/boolean_spec.rb",
82
+ "spec/fiber_spec.rb",
83
+ "spec/float_spec.rb",
84
+ "spec/hash_spec.rb",
85
+ "spec/integer_spec.rb",
86
+ "spec/io_spec.rb",
87
+ "spec/net_spec.rb",
88
+ "spec/numeric_spec.rb",
89
+ "spec/object_spec.rb",
90
+ "spec/statistics_spec.rb",
91
+ "spec/string_spec.rb",
92
+ "spec/symbol_spec.rb",
93
+ "spec/spec_helper.rb"
94
+ ]
95
+
96
+ if s.respond_to? :specification_version then
97
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
98
+ s.specification_version = 3
99
+
100
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
101
+ else
102
+ end
103
+ else
104
+ end
105
+ end
@@ -16,12 +16,12 @@ class Array
16
16
 
17
17
  # Sums the array
18
18
  def sum
19
- self.inject(:+)
19
+ self.inject(&:+)
20
20
  end
21
21
 
22
22
  # Finds the product of the array
23
23
  def product
24
- self.inject(:*)
24
+ self.inject(&:*)
25
25
  end
26
26
 
27
27
  # Collects the squares of each value in the array
@@ -1,9 +1,8 @@
1
- require 'colored'
2
1
  require 'darkext/fiber'
3
2
  require 'darkext/string'
4
3
  require 'darkext/symbol'
5
4
 
6
- "Beagle.query is not secure! Use with caution".red.printn
5
+ "Beagle.query is not secure! Use with caution".printn
7
6
 
8
7
  module Beagle
9
8
  class BeagleError < RuntimeError; end
@@ -1,14 +1,4 @@
1
1
  class Hash
2
- # Merges defaults with the hash
3
- def with_defaults(defaults)
4
- self.merge(defaults) { |key,old,new| old.nil? ? new : old }
5
- end
6
-
7
- # Merges defaults with the hash (destructive)
8
- def with_defaults!(defaults)
9
- self.merge!(defaults) { |key,old,new| old.nil? ? new : old }
10
- end
11
-
12
2
  def nested_find(*keys)
13
3
  v = self
14
4
  keys.each do |key|
@@ -35,4 +25,14 @@ class Hash
35
25
  raise NoMethodError, "#{ m}"
36
26
  end
37
27
  end
28
+
29
+ # http://noobkit.com/show/ruby/gems/development/merb/hash/symbolize_keys-21.html
30
+ def symbolize_keys!
31
+ each do |k,v|
32
+ sym = k.respond_to?(:to_sym) ? k.to_sym : k
33
+ self[sym] = Hash === v ? v.symbolize_keys! : v
34
+ delete(k) unless k == sym
35
+ end
36
+ self
37
+ end
38
38
  end
@@ -3,7 +3,7 @@ require 'darkext/hash'
3
3
  module DarkIO
4
4
  # Runs a block and captures the output it generates
5
5
  def self.capture_output(opts = { }) # yield e
6
- opts.with_defaults!(:stdout => true, :stderr => false)
6
+ opts = { :stdout => true, :stderr => false }.merge(opts)
7
7
  cout,cerr = opts[:stdout],opts[:stderr]
8
8
 
9
9
  yield and return if !cout && !cerr
@@ -34,9 +34,7 @@ module Sinatra
34
34
  end
35
35
 
36
36
  def host
37
- port = request.env['SERVER_PORT'].to_i
38
- port = port == 80 ? "" : ":#{port}"
39
- "#{protocol}://#{server_name}#{port}"
37
+ "#{protocol}://#{request.env['HTTP_HOST']}"
40
38
  end
41
39
 
42
40
  def protocol
@@ -73,7 +73,7 @@ class Array
73
73
  # Generates a confidence interval
74
74
  def ci(opts = { })
75
75
  raise ArgumentError.new('Array size must be > 0') if self.size.zero?
76
- opts.with_defaults!({ :percent => 0.95, :rho => 1, :type => :center })
76
+ opts = { :percent => 0.95, :rho => 1, :type => :center }.merge(opts)
77
77
  percent = opts[:percent]
78
78
  rho = opts[:rho]
79
79
  m = self.mean
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe Array do
4
4
  before(:each) do
5
- @a = [1,2,3,4,5,6,7,8,9,10]
5
+ @a = (1..5).to_a
6
6
  end
7
7
 
8
8
  it 'should respond to all the new methods' do
@@ -42,13 +42,17 @@ describe Array do
42
42
  end
43
43
 
44
44
  it 'should pick randomly' do
45
- counts = [0,0,0,0,0,0,0,0,0,0]
45
+ counts = [0,0,0,0,0]
46
46
  count = 1000000
47
47
  count.times do
48
48
  r = @a.random
49
49
  counts[r - 1] += 1
50
50
  end
51
- counts.each { |v| (v/count.to_f).should be_close(1/counts.size.to_f,0.001) }
51
+ counts.each do |v|
52
+ (v/count.to_f).should be_close(1/counts.size.to_f,0.001)
53
+ end
54
+
55
+ @a.include?(@a.random).should be_true
52
56
  end
53
57
 
54
58
  it 'should return an Array from randomize' do
@@ -60,4 +64,49 @@ describe Array do
60
64
  Array.new.send(method.intern).should be_nil
61
65
  end
62
66
  end
67
+
68
+ it 'should rotate properly' do
69
+ @a.rotate
70
+ @a.should == [2,3,4,5,1]
71
+ @a.rotate
72
+ @a.should == [3,4,5,1,2]
73
+ @a.rotate(@a.size)
74
+ @a.should == [3,4,5,1,2]
75
+ end
76
+
77
+ it 'should rotate reverse properly' do
78
+ @a.rotate_reverse
79
+ @a.should == [5,1,2,3,4]
80
+ @a.rotate_reverse
81
+ @a.should == [4,5,1,2,3]
82
+ @a.rotate_reverse(@a.size)
83
+ @a.should == [4,5,1,2,3]
84
+ end
85
+
86
+ it 'should sum properly' do
87
+ @a.sum.should == 15
88
+ [1].sum.should == 1
89
+ %w(a b c).sum.should == 'abc'
90
+ [1.0,0.1,0.01].sum.should == 1.11
91
+ end
92
+
93
+ it 'should do product properly' do
94
+ @a.product.should == 120
95
+ [1].product.should == 1
96
+ ['a',3].product.should == 'aaa'
97
+ end
98
+
99
+ it 'should do squares properly' do
100
+ @a.squares.should == [1,4,9,16,25]
101
+ [1].squares.should == [1]
102
+ @a.squares!
103
+ @a.should == [1,4,9,16,25]
104
+ end
105
+
106
+ it 'should randomize' do
107
+ @a.randomize.should_not == @a
108
+ a = @a.clone
109
+ @a.randomize!
110
+ @a.should_not == a
111
+ end
63
112
  end
@@ -8,6 +8,12 @@ describe Object do
8
8
  it 'should return a boolean from is_boolean' do
9
9
  true.is_boolean?.is_boolean?.should be_true
10
10
  end
11
+
12
+ it 'should be randomly booolean' do
13
+ 1.is_boolean?.should be_false
14
+ 'true'.is_boolean?.should be_false
15
+ :true.is_boolean?.should be_false
16
+ end
11
17
  end
12
18
 
13
19
  describe TrueClass do
@@ -18,6 +24,14 @@ describe TrueClass do
18
24
  it 'should return a symbol from intern' do
19
25
  true.intern.should be_a_kind_of(Symbol)
20
26
  end
27
+
28
+ it 'should be boolean' do
29
+ true.is_boolean?.should be_true
30
+ end
31
+
32
+ it 'should intern properly' do
33
+ true.intern.should == :true
34
+ end
21
35
  end
22
36
 
23
37
  describe FalseClass do
@@ -28,4 +42,12 @@ describe FalseClass do
28
42
  it 'should return a symbol from intern' do
29
43
  false.intern.should be_a_kind_of(Symbol)
30
44
  end
45
+
46
+ it 'should be boolean' do
47
+ false.is_boolean?.should be_true
48
+ end
49
+
50
+ it 'should intern properly' do
51
+ false.intern.should == :false
52
+ end
31
53
  end