tins 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/.gitignore +4 -0
  2. data/.travis.yml +7 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +18 -0
  5. data/README.rdoc +20 -0
  6. data/Rakefile +28 -0
  7. data/TODO +1 -0
  8. data/VERSION +1 -0
  9. data/lib/spruz.rb +1 -0
  10. data/lib/tins.rb +34 -0
  11. data/lib/tins/alias.rb +1 -0
  12. data/lib/tins/attempt.rb +51 -0
  13. data/lib/tins/bijection.rb +46 -0
  14. data/lib/tins/count_by.rb +8 -0
  15. data/lib/tins/deep_dup.rb +11 -0
  16. data/lib/tins/file_binary.rb +85 -0
  17. data/lib/tins/generator.rb +68 -0
  18. data/lib/tins/go.rb +43 -0
  19. data/lib/tins/hash_symbolize_keys_recursive.rb +28 -0
  20. data/lib/tins/hash_union.rb +15 -0
  21. data/lib/tins/limited.rb +38 -0
  22. data/lib/tins/lines_file.rb +123 -0
  23. data/lib/tins/memoize.rb +78 -0
  24. data/lib/tins/minimize.rb +55 -0
  25. data/lib/tins/module_group.rb +13 -0
  26. data/lib/tins/null.rb +26 -0
  27. data/lib/tins/once.rb +25 -0
  28. data/lib/tins/p.rb +23 -0
  29. data/lib/tins/partial_application.rb +31 -0
  30. data/lib/tins/range_plus.rb +9 -0
  31. data/lib/tins/round.rb +51 -0
  32. data/lib/tins/secure_write.rb +25 -0
  33. data/lib/tins/shuffle.rb +17 -0
  34. data/lib/tins/string_camelize.rb +16 -0
  35. data/lib/tins/string_underscore.rb +15 -0
  36. data/lib/tins/string_version.rb +105 -0
  37. data/lib/tins/subhash.rb +42 -0
  38. data/lib/tins/time_dummy.rb +31 -0
  39. data/lib/tins/to_proc.rb +11 -0
  40. data/lib/tins/uniq_by.rb +10 -0
  41. data/lib/tins/version.rb +10 -0
  42. data/lib/tins/write.rb +19 -0
  43. data/lib/tins/xt.rb +25 -0
  44. data/lib/tins/xt/attempt.rb +7 -0
  45. data/lib/tins/xt/blank.rb +67 -0
  46. data/lib/tins/xt/count_by.rb +11 -0
  47. data/lib/tins/xt/deep_dup.rb +7 -0
  48. data/lib/tins/xt/file_binary.rb +7 -0
  49. data/lib/tins/xt/full.rb +33 -0
  50. data/lib/tins/xt/hash_symbolize_keys_recursive.rb +7 -0
  51. data/lib/tins/xt/hash_union.rb +11 -0
  52. data/lib/tins/xt/irb.rb +17 -0
  53. data/lib/tins/xt/named.rb +35 -0
  54. data/lib/tins/xt/null.rb +5 -0
  55. data/lib/tins/xt/p.rb +7 -0
  56. data/lib/tins/xt/partial_application.rb +11 -0
  57. data/lib/tins/xt/range_plus.rb +12 -0
  58. data/lib/tins/xt/round.rb +13 -0
  59. data/lib/tins/xt/secure_write.rb +11 -0
  60. data/lib/tins/xt/shuffle.rb +11 -0
  61. data/lib/tins/xt/string.rb +5 -0
  62. data/lib/tins/xt/string_camelize.rb +6 -0
  63. data/lib/tins/xt/string_underscore.rb +6 -0
  64. data/lib/tins/xt/string_version.rb +7 -0
  65. data/lib/tins/xt/subhash.rb +11 -0
  66. data/lib/tins/xt/symbol_to_proc.rb +7 -0
  67. data/lib/tins/xt/time_dummy.rb +7 -0
  68. data/lib/tins/xt/uniq_by.rb +15 -0
  69. data/lib/tins/xt/write.rb +11 -0
  70. data/tests/tins_file_binary_test.rb +67 -0
  71. data/tests/tins_lines_file_test.rb +84 -0
  72. data/tests/tins_memoize_test.rb +52 -0
  73. data/tests/tins_secure_write_test.rb +44 -0
  74. data/tests/tins_test.rb +629 -0
  75. data/tins.gemspec +35 -0
  76. metadata +212 -0
@@ -0,0 +1,11 @@
1
+ require 'tins/count_by'
2
+
3
+ module Tins
4
+ module ::Enumerable
5
+ include CountBy
6
+ end
7
+
8
+ class ::Array
9
+ include CountBy
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ require 'tins/deep_dup'
2
+
3
+ module Tins
4
+ class ::Object
5
+ include Tins::DeepDup
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'tins/file_binary'
2
+
3
+ module Tins
4
+ class ::File
5
+ include FileBinary
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ require 'tins/xt/blank'
2
+
3
+ module Tins
4
+ module Full
5
+ # Returns the object if it isn't blank (as in Object#blank?), otherwise it
6
+ # returns nil. If a block was given as an argument and the object isn't
7
+ # blank, the block is executed with the object as its first argument. If an
8
+ # argument +dispatch+ was given and the object wasn't blank the method
9
+ # given by dispatch is called on the object. This is the same as
10
+ # foo.full?(&:bar) in the previous block form.
11
+ def full?(dispatch = nil, *args)
12
+ if blank?
13
+ obj = nil
14
+ #elsif Module === dispatch # TODO
15
+ # dispatch.found?(self)
16
+ elsif dispatch
17
+ obj = __send__(dispatch, *args)
18
+ obj = nil if obj.blank?
19
+ else
20
+ obj = self
21
+ end
22
+ if block_given? and obj
23
+ yield obj
24
+ else
25
+ obj
26
+ end
27
+ end
28
+ end
29
+
30
+ class ::Object
31
+ include Full
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ require 'tins/hash_symbolize_keys_recursive'
2
+
3
+ module Tins
4
+ class ::Hash
5
+ include HashSymbolizeKeysRecursive
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'tins/hash_union'
2
+
3
+ module Tins
4
+ class ::Hash
5
+ if method_defined?(:|)
6
+ warn "#{self}#| already defined, didn't include at #{__FILE__}:#{__LINE__}"
7
+ else
8
+ include HashUnion
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'irb'
2
+
3
+ module Tins
4
+ IRB = ::IRB
5
+
6
+ module ::IRB
7
+ def self.examine(binding = TOPLEVEL_BINDING)
8
+ setup nil
9
+ workspace = WorkSpace.new binding
10
+ irb = Irb.new workspace
11
+ @CONF[:MAIN_CONTEXT] = irb.context
12
+ catch(:IRB_EXIT) { irb.eval_input }
13
+ rescue Interrupt
14
+ exit
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ require 'tins/xt/string_version'
2
+
3
+ class Object
4
+ if RUBY_VERSION.version >= '1.9'.version
5
+ def named(name, method, *args, &named_block)
6
+ extend Module.new {
7
+ define_method(name) do |*rest, &block|
8
+ block = named_block if named_block
9
+ __send__(method, *(args + rest), &block)
10
+ end
11
+ }
12
+ end
13
+ else
14
+ def named(name, method, *args, &block)
15
+ extend Module.new { define_method(name) { |*rest| __send__(method, *(args + rest), &block) } }
16
+ end
17
+ end
18
+ end
19
+
20
+ class Module
21
+ if RUBY_VERSION.version >= '1.9'.version
22
+ def named(name, method, *args, &named_block)
23
+ include Module.new {
24
+ define_method(name) do |*rest, &block|
25
+ block = named_block if named_block
26
+ __send__(method, *(args + rest), &block)
27
+ end
28
+ }
29
+ end
30
+ else
31
+ def named(name, method, *args, &block)
32
+ include Module.new { define_method(name) { |*rest| __send__(method, *(args + rest), &block) } }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ require 'tins/null'
2
+
3
+ module Tins
4
+ ::NULL = Tins::NULL
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'tins/p'
2
+
3
+ module Tins
4
+ class ::Object
5
+ include Tins::P
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'tins/partial_application'
2
+
3
+ module Tins
4
+ class ::Proc
5
+ include PartialApplication
6
+ end
7
+
8
+ class ::Method
9
+ include PartialApplication
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'tins/range_plus'
2
+
3
+ module Tins
4
+ class ::Range
5
+ if method_defined?(:+)
6
+ warn "#{self}#+ already defined, didn't include at #{__FILE__}:#{__LINE__}"
7
+ else
8
+ include RangePlus
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,13 @@
1
+ require 'tins/round'
2
+
3
+ module Tins
4
+ module Round
5
+ class ::Float
6
+ include Round
7
+ end
8
+
9
+ class ::Integer
10
+ include Round
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'tins/secure_write'
2
+
3
+ module Tins
4
+ #class ::Object
5
+ # include Tins::SecureWrite
6
+ #end
7
+
8
+ class ::IO
9
+ extend Tins::SecureWrite
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'tins/shuffle'
2
+
3
+ module Tins
4
+ class ::Array
5
+ if method_defined?(:shuffle)
6
+ warn "#{self}#shuffle already defined, didn't include at #{__FILE__}:#{__LINE__}"
7
+ else
8
+ include Shuffle
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Tins
2
+ require 'tins/xt/string_camelize'
3
+ require 'tins/xt/string_underscore'
4
+ require 'tins/xt/string_version'
5
+ end
@@ -0,0 +1,6 @@
1
+ module Tins
2
+ require 'tins/string_camelize'
3
+ class ::String
4
+ include StringCamelize
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Tins
2
+ require 'tins/string_underscore'
3
+ class ::String
4
+ include StringUnderscore
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Tins
2
+ require 'tins/string_version'
3
+
4
+ class ::String
5
+ include StringVersion
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ require 'tins/subhash'
2
+
3
+ module Tins
4
+ class ::Hash
5
+ include Tins::Subhash
6
+
7
+ def subhash!(*patterns)
8
+ replace subhash(*patterns)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Tins
2
+ unless ::Symbol.method_defined?(:to_proc)
3
+ class ::Symbol
4
+ include ToProc
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'tins/time_dummy'
2
+
3
+ module Tins
4
+ class ::Time
5
+ include TimeDummy
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'tins/uniq_by'
2
+
3
+ module Tins
4
+ module ::Enumerable
5
+ include UniqBy
6
+ end
7
+
8
+ class ::Array
9
+ include UniqBy
10
+
11
+ def uniq_by!(&b)
12
+ replace uniq_by(&b)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'tins/write'
2
+
3
+ module Tins
4
+ #class ::Object
5
+ # include Tins::Write
6
+ #end
7
+
8
+ class ::IO
9
+ extend Tins::Write
10
+ end
11
+ end
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'tempfile'
5
+ require 'tins/xt/file_binary'
6
+
7
+ module Tins
8
+ class TinsFileBinaryTest < Test::Unit::TestCase
9
+ def test_ascii_buffer_size
10
+ write_file do |file|
11
+ file.write "A" * 10 + "\x00"
12
+ assert_equal true, file.ascii?(:buffer_size => 10)
13
+ assert_equal true, File.ascii?(file.path, :buffer_size => 10)
14
+ assert_equal false, file.binary?(:buffer_size => 10)
15
+ assert_equal false, File.binary?(file.path, :buffer_size => 10)
16
+ end
17
+ end
18
+
19
+ def test_binary
20
+ write_file do |file|
21
+ file.write "A" * 69 + "\x01" * 31
22
+ assert_equal true, file.binary?
23
+ assert_equal true, File.binary?(file.path)
24
+ assert_equal false, file.ascii?
25
+ assert_equal false, File.ascii?(file.path)
26
+ end
27
+ end
28
+
29
+ def test_ascii_offset
30
+ write_file do |file|
31
+ file.write "\x01" * 31 + "A" * 70
32
+ assert_equal false, file.binary?(:offset => 1)
33
+ assert_equal false, File.binary?(file.path, :offset => 1)
34
+ assert_equal true, file.ascii?(:offset => 1)
35
+ assert_equal true, File.ascii?(file.path, :offset => 1)
36
+ end
37
+ end
38
+
39
+ def test_binary_zero
40
+ write_file do |file|
41
+ file.write "A" * 50 + "\0" + "A" * 49
42
+ assert_equal true, file.binary?
43
+ assert_equal true, File.binary?(file.path)
44
+ assert_equal false, file.ascii?
45
+ assert_equal false, File.ascii?(file.path)
46
+ end
47
+ end
48
+
49
+ def test_ascii
50
+ write_file do |file|
51
+ file.write "A" * 100
52
+ assert_equal false, file.binary?
53
+ assert_equal false, File.binary?(file.path)
54
+ assert_equal true, file.ascii?
55
+ assert_equal true, File.ascii?(file.path)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def write_file
62
+ File.open(File.join(Dir.tmpdir, "temp.#$$"), 'wb+') do |file|
63
+ yield file
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'tempfile'
5
+ require 'tins/lines_file'
6
+
7
+ module Tins
8
+ class TinsLinesFileTest < Test::Unit::TestCase
9
+ FILE = <<EOT
10
+ def foo
11
+ end
12
+
13
+ def bar
14
+ end
15
+
16
+ def baz
17
+ end
18
+ EOT
19
+
20
+ def test_instantiation
21
+ write_file do |file|
22
+ file.write FILE
23
+ file.rewind
24
+ assert_kind_of Tins::LinesFile, lf = Tins::LinesFile.for_file(file)
25
+ assert_equal [ "def foo\n", 1 ], [ lf.line, lf.line.line_number ]
26
+ assert_kind_of Tins::LinesFile, lf = Tins::LinesFile.for_filename(file.path)
27
+ assert_equal [ "def foo\n", 1 ], [ lf.line, lf.line.line_number ]
28
+ file.rewind
29
+ assert_kind_of Tins::LinesFile, lf = Tins::LinesFile.for_lines(file.readlines)
30
+ assert_equal [ "def foo\n", 1 ], [ lf.line, lf.line.line_number ]
31
+ end
32
+ end
33
+
34
+ def test_match
35
+ write_file do |file|
36
+ file.write FILE
37
+ file.rewind
38
+ lf = Tins::LinesFile.for_file(file)
39
+ assert_equal "def foo\n", lf.line
40
+ assert_equal 1, lf.line.line_number
41
+ assert_equal %w[foo], lf.match_forward(/def (\S+)/)
42
+ assert_equal "def foo\n", lf.line
43
+ assert_equal %w[foo], lf.match_forward(/def (\S+)/, true)
44
+ assert_equal 2, lf.line.line_number
45
+ assert_equal "end\n", lf.line
46
+ assert_equal %w[bar], lf.match_forward(/def (\S+)/, true)
47
+ assert_equal 5, lf.line.line_number
48
+ assert_equal %w[baz], lf.match_forward(/def (\S+)/, true)
49
+ assert_nil lf.match_forward(/def (\S+)/, true)
50
+ assert_equal "end\n", lf.line
51
+ assert_equal 8, lf.line.line_number
52
+ assert_equal %w[baz], lf.match_backward(/def (\S+)/)
53
+ assert_equal "def baz\n", lf.line
54
+ assert_equal 7, lf.line.line_number
55
+ assert_equal %w[baz], lf.match_backward(/def (\S+)/, true)
56
+ assert_equal "\n", lf.line
57
+ assert_equal 6, lf.line.line_number
58
+ assert_equal %w[bar], lf.match_backward(/def (\S+)/, true)
59
+ assert_equal %w[foo], lf.match_backward(/def (\S+)/, true)
60
+ assert_equal nil, lf.match_backward(/nada/, true)
61
+ end
62
+ end
63
+
64
+ def test_empty_and_not
65
+ lf = Tins::LinesFile.for_lines []
66
+ assert_equal true, lf.empty?
67
+ assert_equal 0, lf.last_line_number
68
+ assert_equal [], lf.to_a
69
+ lf = Tins::LinesFile.for_lines [ "foo\n" ]
70
+ assert_equal false, lf.empty?
71
+ assert_equal 1, lf.last_line_number
72
+ assert_equal [ "foo\n" ], lf.to_a
73
+ assert_equal [ "foo\n", 1 ], [ lf.line, lf.line.line_number ]
74
+ end
75
+
76
+ private
77
+
78
+ def write_file
79
+ File.open(File.join(Dir.tmpdir, "temp.#$$"), 'wb+') do |file|
80
+ yield file
81
+ end
82
+ end
83
+ end
84
+ end