facets 2.0.2 → 2.0.3

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.
Files changed (114) hide show
  1. data/AUTHORS +3 -3
  2. data/README +6 -7
  3. data/lib/core/facets.rb +1 -46
  4. data/lib/core/facets/array.rb +3 -0
  5. data/lib/core/facets/array/indexable.rb +6 -1
  6. data/lib/core/facets/array/only.rb +20 -0
  7. data/lib/core/facets/dir/multiglob.rb +12 -1
  8. data/lib/core/facets/enumerable.rb +0 -1
  9. data/lib/core/facets/enumerable/collect.rb +1 -0
  10. data/lib/core/facets/enumerable/combination.rb +44 -90
  11. data/lib/core/facets/facets.rb +46 -0
  12. data/lib/core/facets/file/write.rb +46 -58
  13. data/lib/core/facets/hash.rb +2 -0
  14. data/lib/core/facets/hash/select.rb +14 -0
  15. data/lib/core/facets/integer/multiples.rb +12 -55
  16. data/lib/core/facets/kernel/val.rb +14 -0
  17. data/lib/core/facets/module/alias.rb +28 -9
  18. data/lib/core/facets/module/methods.rb +18 -0
  19. data/lib/core/facets/module/traits.rb +65 -70
  20. data/lib/core/facets/proc/compose.rb +15 -12
  21. data/lib/core/facets/stackable.rb +3 -2
  22. data/lib/core/facets/string/format.rb +4 -6
  23. data/lib/core/facets/string/tabs.rb +34 -0
  24. data/lib/core/facets/symbol.rb +1 -0
  25. data/lib/core/facets/symbol/succ.rb +1 -42
  26. data/lib/core/facets/symbol/to_proc.rb +34 -0
  27. data/lib/methods/facets/array/contains.rb +1 -0
  28. data/lib/methods/facets/facets/require_core.rb +1 -0
  29. data/lib/methods/facets/file/writelines.rb +1 -0
  30. data/lib/methods/facets/io/writelines.rb +1 -0
  31. data/lib/methods/facets/kernel/not_nil.rb +1 -0
  32. data/lib/methods/facets/module/conflict.rb +1 -0
  33. data/lib/methods/facets/module/instance_method_defined.rb +1 -0
  34. data/lib/methods/facets/module/module_method_defined.rb +1 -0
  35. data/lib/methods/facets/module/private_conflict.rb +1 -0
  36. data/lib/methods/facets/module/protected_conflict.rb +1 -0
  37. data/lib/methods/facets/module/public_conflict.rb +1 -0
  38. data/lib/methods/facets/string/expand_tabs.rb +1 -0
  39. data/lib/methods/facets/string/outdent.rb +1 -0
  40. data/lib/methods/facets/string/taballto.rb +1 -0
  41. data/lib/more/facets/arguments.rb +1 -1
  42. data/lib/more/facets/association.rb +0 -46
  43. data/lib/more/facets/autoarray.rb +0 -28
  44. data/lib/more/facets/command.rb +341 -8
  45. data/lib/more/facets/dictionary.rb +25 -131
  46. data/lib/more/facets/downloader.rb +1 -1
  47. data/lib/more/facets/infinity.rb +3 -3
  48. data/lib/more/facets/interval.rb +0 -161
  49. data/lib/more/facets/multiton.rb +16 -12
  50. data/lib/more/facets/namespace.rb +1 -1
  51. data/lib/more/facets/ostruct.rb +46 -10
  52. data/lib/more/facets/overload.rb +0 -51
  53. data/lib/more/facets/paramix.rb +0 -97
  54. data/lib/more/facets/pp_s.rb +30 -0
  55. data/lib/more/facets/progressbar.rb +18 -10
  56. data/lib/more/facets/prototype.rb +1 -40
  57. data/lib/more/facets/random.rb +1 -0
  58. data/lib/more/facets/rbsystem.rb +4 -1
  59. data/lib/more/facets/snapshot.rb +8 -1
  60. data/lib/more/facets/stylize.rb +2 -0
  61. data/meta/{project.yaml → facets-2.0.3.roll} +22 -14
  62. data/meta/manifest.txt +38 -8
  63. data/task/{config.yaml → config/general.yaml} +7 -2
  64. data/task/{rdoc.yaml → config/rdoc.yaml} +1 -1
  65. data/task/crosstest +309 -0
  66. data/task/isotest +293 -0
  67. data/task/loadtest +28 -0
  68. data/task/methods +4 -4
  69. data/task/prepare +5 -0
  70. data/task/publish +2 -2
  71. data/task/rdoc +1 -0
  72. data/task/syntax +29 -0
  73. data/task/test +0 -1
  74. data/task/testeach +42 -0
  75. data/task/testpairs +50 -0
  76. data/test/lib/rq.rb +15 -0
  77. data/test/unit/array/test_merge.rb +21 -43
  78. data/test/unit/array/test_only.rb +21 -0
  79. data/test/unit/enumerable/test_collect.rb +1 -21
  80. data/test/unit/enumerable/test_combination.rb +50 -44
  81. data/test/unit/file/test_topath.rb +48 -57
  82. data/test/unit/file/test_write.rb +82 -0
  83. data/test/unit/hash/test_select.rb +43 -0
  84. data/test/unit/integer/test_multiples.rb +28 -32
  85. data/test/unit/kernel/test_report.rb +9 -12
  86. data/test/unit/kernel/test_val.rb +50 -45
  87. data/test/unit/module/test_include.rb +56 -57
  88. data/test/unit/module/test_name.rb +42 -55
  89. data/test/unit/module/test_traits.rb +46 -47
  90. data/test/unit/string/test_filter.rb +19 -34
  91. data/test/unit/string/test_format.rb +87 -96
  92. data/test/unit/string/test_regesc.rb +18 -26
  93. data/test/unit/string/test_tabs.rb +226 -119
  94. data/test/unit/symbol/test_succ.rb +14 -23
  95. data/test/unit/symbol/test_to_proc.rb +41 -0
  96. data/test/unit/test_association.rb +38 -47
  97. data/test/unit/test_attributes.rb +24 -33
  98. data/test/unit/test_autoarray.rb +23 -32
  99. data/test/unit/test_command.rb +26 -0
  100. data/test/unit/test_dictionary.rb +123 -117
  101. data/test/unit/test_infinity.rb +41 -47
  102. data/test/unit/test_inheritor.rb +133 -142
  103. data/test/unit/test_interval.rb +129 -93
  104. data/test/unit/test_ostruct.rb +148 -101
  105. data/test/unit/test_overload.rb +8 -15
  106. data/test/unit/test_paramix.rb +67 -73
  107. data/test/unit/test_pp_s.rb +23 -0
  108. data/test/unit/test_prototype.rb +28 -38
  109. metadata +47 -11
  110. data/lib/core/facets/enumerable/instance_map.rb +0 -0
  111. data/lib/more/facets/command_options.rb +0 -328
  112. data/meta/version.txt +0 -1
  113. data/task/load +0 -39
  114. data/test/unit/test_command_options.rb +0 -29
@@ -12,3 +12,5 @@ require 'facets/hash/weave.rb'
12
12
  require 'facets/hash/new.rb'
13
13
  require 'facets/hash/inverse.rb'
14
14
  require 'facets/hash/traverse.rb'
15
+ require 'facets/hash/select.rb'
16
+
@@ -0,0 +1,14 @@
1
+ class Hash
2
+
3
+ # In-place version of Hash#select. The opposite of the built-in
4
+ # Hash#reject!.
5
+ #
6
+ # CREDIT Noah Gibbs
7
+ # CREDIT Gavin Sinclair
8
+
9
+ def select!
10
+ reject! { |k,v| not yield(k,v) }
11
+ end
12
+
13
+ end
14
+
@@ -1,32 +1,26 @@
1
- # = FILE
2
- #
3
- # interger/multiple.rb
4
- #
5
- # = DESCRIPTION
6
- #
7
- # Multiple related extensions for Integer.
8
- #
9
- # = AUTHORS
10
- #
11
- # CREDIT Thomas Sawyer
12
-
13
-
14
1
  class Integer
15
2
 
16
- # Is an integer odd?
3
+ # Returns true if this integer is odd, false otherwise.
17
4
  #
18
- # 2.odd? #=> false
19
- # 3.odd? #=> true
5
+ # 2.odd? #=> false
6
+ # 3.odd? #=> true
20
7
  #
8
+ # -99.odd? # -> true
9
+ # -98.odd? # -> false
10
+ #
11
+ # CREDIT Gavin Sinclair
12
+
21
13
  def odd?
22
14
  self % 2 == 1
23
15
  end
24
16
 
25
- # Is an integer even?
17
+ # Returns true if this integer is even, false otherwise.
26
18
  #
27
19
  # 2.even? #=> true
28
20
  # 3.even? #=> false
29
21
  #
22
+ # CREDIT Gavin Sinclair
23
+
30
24
  def even?
31
25
  self % 2 == 0
32
26
  end
@@ -36,47 +30,10 @@ class Integer
36
30
  # 7.multiple?(2) #=> false
37
31
  # 8.multiple?(2) #=> true
38
32
  #
33
+
39
34
  def multiple?(number)
40
35
  self % number == 0
41
36
  end
42
37
 
43
38
  end
44
39
 
45
-
46
-
47
- # _____ _
48
- # |_ _|__ ___| |_
49
- # | |/ _ \/ __| __|
50
- # | | __/\__ \ |_
51
- # |_|\___||___/\__|
52
- #
53
- =begin test
54
-
55
- require 'test/unit'
56
-
57
- class TCInteger < Test::Unit::TestCase
58
-
59
- def test_odd?
60
- assert( ! 0.odd? )
61
- assert( 1.odd? )
62
- assert( ! 2.odd? )
63
- assert( 3.odd? )
64
- end
65
-
66
- def test_even?
67
- assert( 0.even? )
68
- assert( ! 1.even? )
69
- assert( 2.even? )
70
- assert( ! 3.even? )
71
- end
72
-
73
- def test_multiple?
74
- assert( ! 1.multiple?(2) )
75
- assert( 2.multiple?(2) )
76
- assert( ! 5.multiple?(3) )
77
- assert( 6.multiple?(3) )
78
- end
79
-
80
- end
81
-
82
- =end
@@ -10,6 +10,20 @@ module Kernel
10
10
  true
11
11
  end
12
12
 
13
+ # The opposite of #nil?.
14
+ #
15
+ # "hello".not_nil? # -> true
16
+ # nil.not_nil? # -> false
17
+ #
18
+ # CREDIT Gavin Sinclair
19
+
20
+ def not_nil?
21
+ not nil?
22
+ end
23
+
24
+ # TODO Do not need two of these.
25
+ #alias_method :non_nil?, :not_nil?
26
+
13
27
  # Is self included in other?
14
28
  #
15
29
  # 5.in?(0..10) #=> true
@@ -13,6 +13,11 @@
13
13
  # AUTHORS:
14
14
  #
15
15
  # CREDIT Thomas Saywer
16
+ #
17
+ # LOG:
18
+ #
19
+ # - 2007.10.31 trans
20
+ # Made #alias_method_chain compatible with current ActiveSupport version.
16
21
 
17
22
  class Module
18
23
 
@@ -36,9 +41,9 @@ class Module
36
41
  # self.y = 2
37
42
  # x #=> 2
38
43
 
39
- def alias_accessor( name, target )
40
- alias_method( name, target )
41
- alias_method( "#{name}=", "#{target}=" )
44
+ def alias_accessor(name, target)
45
+ alias_method(name, target)
46
+ alias_method("#{name}=", "#{target}=")
42
47
  end
43
48
 
44
49
  # Alias a module function so that the alias is also
@@ -83,18 +88,32 @@ class Module
83
88
  # is equivalent to
84
89
  #
85
90
  # alias_method :foo_without_feature?, :foo?
86
- # alias_method :foo?, :foo_without_feature?
91
+ # alias_method :foo?, :foo_with_feature?
87
92
  #
88
93
  # so you can safely chain foo, foo?, and foo! with the same feature.
89
- #--
90
- # Credit goes to bitsweat and Rails.
94
+ #
95
+ # CREDIT Bitsweat and Rails Team.
91
96
  #++
97
+
92
98
  def alias_method_chain(target, feature)
93
99
  # Strip out punctuation on predicates or bang methods since
94
100
  # e.g. target?_without_feature is not a valid method name.
95
- aliased_target, punctuation = target.to_s.sub(/([?!])$/, ''), $1
96
- alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
97
- alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
101
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
102
+ yield(aliased_target, punctuation) if block_given?
103
+
104
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
105
+
106
+ alias_method without_method, target
107
+ alias_method target, with_method
108
+
109
+ case
110
+ when public_method_defined?(without_method)
111
+ public target
112
+ when protected_method_defined?(without_method)
113
+ protected target
114
+ when private_method_defined?(without_method)
115
+ private target
116
+ end
98
117
  end
99
118
 
100
119
  end
@@ -13,4 +13,22 @@ class Module
13
13
  private_instance_methods(all)
14
14
  end
15
15
 
16
+ # Query whether an instance method is defined for the module.
17
+ #
18
+ # CREDIT Noah Gibbs
19
+ # CREDIT Gavin Sinclair
20
+
21
+ def instance_method_defined?(_method)
22
+ instance_methods(true).find { |m| m == _method.to_s }
23
+ end
24
+
25
+ # Query whether a normal (singleton) method is defined for the module.
26
+ #
27
+ # CREDIT Noah Gibbs
28
+ # CREDIT Gavin Sinclair
29
+
30
+ def module_method_defined?(_method)
31
+ singleton_methods(true).find { |m| m == _method.to_s }
32
+ end
33
+
16
34
  end
@@ -4,107 +4,102 @@
4
4
  #
5
5
  # SUMMARY:
6
6
  #
7
- # Module extensions for modifying a module.
7
+ # Module extensions for modifying a module a al traits.
8
8
  #
9
9
  # CREDITS:
10
10
  #
11
11
  # - Thomas Sawyer
12
+ # - Robert Dober
13
+ #
14
+ # NOTES:
15
+ # - In the old version of traits.rb we cloned modules and
16
+ # altered their copies. Eg.
17
+ # def +(other)
18
+ # mod1 = other.clone
19
+ # mod2 = clone
20
+ # mod1.module_eval{ include mod2 }
21
+ # end
22
+ # Later it was realized that this thwarted the main
23
+ # benefit that Ruby's concept of modules has over
24
+ # traditional traits, inheritance.
25
+ #
12
26
 
13
27
  #
14
28
  class Module
15
29
 
16
30
  # Combine modules.
17
31
 
18
- def +( other )
19
- mod1 = other.clone
20
- mod2 = clone
21
- mod1.module_eval{ include mod2 }
22
- #methods = other.instance_methods(false)
23
- #methods.each{ |name|
24
- # method = other.instance_method(name)
25
- # mod.module_eval {
26
- # define_method( name, &method )
27
- # }
28
- #}
29
- return mod1
32
+ def +(other)
33
+ base = self
34
+ Module.new do
35
+ include base
36
+ include other
37
+ end
30
38
  end
31
39
 
32
40
  # Subtract modules.
33
-
34
- def -( other )
35
- mod = clone
36
-
41
+ #--
42
+ # TODO Should this use all instance_methods, not just public?
43
+ #++
44
+ def -(other)
37
45
  case other
38
46
  when Array
39
- subtract = instance_methods & other.collect{|m| m.to_s}
47
+ subtract = instance_methods(true) & other.collect{|m| m.to_s}
40
48
  when Module
41
- subtract = instance_methods & other.instance_methods(false)
49
+ subtract = instance_methods(true) & other.instance_methods(true) # false?
42
50
  when String, Symbol
43
- subtract = instance_methods & [ other.to_s ]
51
+ subtract = instance_methods(true) & [other.to_s]
44
52
  end
45
-
46
- mod.module_eval do
47
- subtract.each{|x| remove_method x }
53
+ base = self
54
+ Module.new do
55
+ include base
56
+ subtract.each{ |x| undef_method x }
48
57
  end
49
-
50
- mod
51
58
  end
52
59
 
53
60
  # Rename methods.
54
61
 
55
- def *( rename_map )
56
- module_eval do
62
+ def *(rename_map)
63
+ base = self
64
+ Module.new do
65
+ include base
57
66
  rename_map.each do |from, to|
58
- alias_method( to, from )
59
- remove_method from
67
+ alias_method to, from
68
+ undef_method from
60
69
  end
61
70
  end
62
- self
63
71
  end
64
72
 
65
- end
66
-
67
-
68
- # _____ _
69
- # |_ _|__ ___| |_
70
- # | |/ _ \/ __| __|
71
- # | | __/\__ \ |_
72
- # |_|\___||___/\__|
73
- #
74
- =begin test
75
-
76
- require 'test/unit'
77
-
78
- class TestModuleTraits < Test::Unit::TestCase
79
-
80
- module A
81
- def x; "x"; end
82
- def z; "zA"; end
83
- end
84
-
85
- module B
86
- def y; "y"; end
87
- def z; "zB"; end
88
- end
73
+ # Detect conflicts.
74
+ #--
75
+ # TODO All instance methods, or just public?
76
+ #++
77
+ def conflict?(other)
78
+ c = []
79
+ c += (public_instance_methods(true) & other.public_instance_methods(true))
80
+ c += (private_instance_methods(true) & other.private_instance_methods(true))
81
+ c += (protected_instance_methods(true) & other.protected_instance_methods(true))
82
+ c.empty ? false : c
83
+ end
89
84
 
90
- Q = A + B
91
- R = A - B
85
+ #def conflict?(other)
86
+ # c = instance_methods & other.instance_methods
87
+ # c.empty ? false : c
88
+ #end
92
89
 
93
- def test_add
94
- assert( Q )
95
- Q.extend Q
96
- assert_equal( "x", Q.x )
97
- assert_equal( "y", Q.y )
98
- assert_equal( "zB", Q.z )
99
- end
90
+ def public_conflict?(other)
91
+ c = public_instance_methods(true) & other.public_instance_methods(true)
92
+ c.empty ? false : c
93
+ end
100
94
 
101
- def test_minus
102
- assert(R)
103
- R.extend R
104
- assert_equal( "x", R.x )
105
- assert_raises( NoMethodError ){ R.z }
106
- end
95
+ def private_conflict?(other)
96
+ c = private_instance_methods(true) & other.private_instance_methods(true)
97
+ c.empty ? false : c
98
+ end
107
99
 
100
+ def protected_conflict?(other)
101
+ c = protected_instance_methods(true) & other.protected_instance_methods(true)
102
+ c.empty ? false : c
108
103
  end
109
104
 
110
- =end
105
+ end
@@ -1,16 +1,16 @@
1
1
  class Proc
2
2
 
3
- # CREDIT Dave
4
-
5
3
  # Returns a new proc that is the functional
6
- # compostion of two procs, in order.
4
+ # composition of two procs, in order.
7
5
  #
8
- # a = lambda { |x| x + 4 }
9
- # b = lambda { |y| y / 2 }
6
+ # a = lambda { |x| x + 4 }
7
+ # b = lambda { |y| y / 2 }
10
8
  #
11
- # a.compose(b).call(4) #=> 6
12
- # b.compose(a).call(4) #=> 4
9
+ # a.compose(b).call(4) #=> 6
10
+ # b.compose(a).call(4) #=> 4
13
11
  #
12
+ # CREDIT Dave
13
+
14
14
  def compose(g)
15
15
  raise ArgumentError, "arity count mismatch" unless arity == g.arity
16
16
  lambda{ |*a| self[ *g[*a] ] }
@@ -18,12 +18,14 @@ class Proc
18
18
 
19
19
  # Operator for Proc#compose and Integer#times_collect/of.
20
20
  #
21
- # a = lambda { |x| x + 4 }
22
- # b = lambda { |y| y / 2 }
21
+ # a = lambda { |x| x + 4 }
22
+ # b = lambda { |y| y / 2 }
23
23
  #
24
- # (a * b).call(4) #=> 6
25
- # (b * a).call(4) #=> 4
24
+ # (a * b).call(4) #=> 6
25
+ # (b * a).call(4) #=> 4
26
26
  #
27
+ # CREDIT Dave
28
+
27
29
  def *(x)
28
30
  if Integer===x
29
31
  # collect times
@@ -34,9 +36,10 @@ class Proc
34
36
  end
35
37
  end
36
38
 
39
+ # Use a Proc as an observable.
40
+ #
37
41
  # CREDIT Tim Pease
38
42
 
39
- # Use a Proc as an observable.
40
43
  alias_method :update, :call
41
44
 
42
45
  end