fuby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.ruby-version +1 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +29 -0
  7. data/Rakefile +8 -0
  8. data/VERSION +1 -0
  9. data/fuby.gemspec +21 -0
  10. data/lib/fuby.rb +1 -0
  11. data/lib/fuby/abbreviation.rb +20 -0
  12. data/lib/fuby/alias_method.rb +13 -0
  13. data/lib/fuby/all.rb +11 -0
  14. data/lib/fuby/and.rb +9 -0
  15. data/lib/fuby/any.rb +11 -0
  16. data/lib/fuby/attributes.rb +17 -0
  17. data/lib/fuby/bind.rb +21 -0
  18. data/lib/fuby/class_attributes.rb +39 -0
  19. data/lib/fuby/deep.rb +1 -0
  20. data/lib/fuby/deep_each.rb +14 -0
  21. data/lib/fuby/define_instance_method.rb +18 -0
  22. data/lib/fuby/define_missing_method.rb +36 -0
  23. data/lib/fuby/define_object_method.rb +18 -0
  24. data/lib/fuby/define_tok.rb +11 -0
  25. data/lib/fuby/descendants.rb +15 -0
  26. data/lib/fuby/dimension.rb +23 -0
  27. data/lib/fuby/drop.rb +20 -0
  28. data/lib/fuby/drop_until.rb +18 -0
  29. data/lib/fuby/drop_while.rb +15 -0
  30. data/lib/fuby/each.rb +2 -0
  31. data/lib/fuby/each_with_index_or_key.rb +19 -0
  32. data/lib/fuby/eql.rb +1 -0
  33. data/lib/fuby/eql_componentwise.rb +13 -0
  34. data/lib/fuby/equivalence.rb +13 -0
  35. data/lib/fuby/eval.rb +54 -0
  36. data/lib/fuby/extend.rb +13 -0
  37. data/lib/fuby/format.rb +9 -0
  38. data/lib/fuby/include.rb +13 -0
  39. data/lib/fuby/index_or_key.rb +19 -0
  40. data/lib/fuby/instance_attributes.rb +39 -0
  41. data/lib/fuby/intercept.rb +27 -0
  42. data/lib/fuby/invert.rb +16 -0
  43. data/lib/fuby/longest_common.rb +16 -0
  44. data/lib/fuby/matches.rb +11 -0
  45. data/lib/fuby/matches_componentwise.rb +11 -0
  46. data/lib/fuby/matches_part_of.rb +16 -0
  47. data/lib/fuby/matches_prefix_of.rb +11 -0
  48. data/lib/fuby/matches_suffix_of.rb +13 -0
  49. data/lib/fuby/member.rb +11 -0
  50. data/lib/fuby/merge.rb +20 -0
  51. data/lib/fuby/merge_css.rb +32 -0
  52. data/lib/fuby/method_added_as.rb +1 -0
  53. data/lib/fuby/method_added_as_binary_operator.rb +16 -0
  54. data/lib/fuby/method_added_as_filter.rb +19 -0
  55. data/lib/fuby/new.rb +16 -0
  56. data/lib/fuby/not.rb +25 -0
  57. data/lib/fuby/opposite.rb +29 -0
  58. data/lib/fuby/or.rb +9 -0
  59. data/lib/fuby/otherwise.rb +18 -0
  60. data/lib/fuby/part_of.rb +16 -0
  61. data/lib/fuby/plus.rb +10 -0
  62. data/lib/fuby/pop.rb +11 -0
  63. data/lib/fuby/pop_if.rb +15 -0
  64. data/lib/fuby/pop_options.rb +15 -0
  65. data/lib/fuby/pop_unless.rb +11 -0
  66. data/lib/fuby/pop_until.rb +11 -0
  67. data/lib/fuby/pop_while.rb +11 -0
  68. data/lib/fuby/prefix_of.rb +14 -0
  69. data/lib/fuby/prefixes.rb +11 -0
  70. data/lib/fuby/prepend.rb +11 -0
  71. data/lib/fuby/private.rb +11 -0
  72. data/lib/fuby/protected.rb +11 -0
  73. data/lib/fuby/public.rb +11 -0
  74. data/lib/fuby/push.rb +1 -0
  75. data/lib/fuby/push_options.rb +9 -0
  76. data/lib/fuby/random.rb +31 -0
  77. data/lib/fuby/self_and_descendants.rb +15 -0
  78. data/lib/fuby/shift.rb +11 -0
  79. data/lib/fuby/shift_if.rb +12 -0
  80. data/lib/fuby/shift_options.rb +11 -0
  81. data/lib/fuby/shift_unless.rb +12 -0
  82. data/lib/fuby/shift_until.rb +12 -0
  83. data/lib/fuby/shift_while.rb +16 -0
  84. data/lib/fuby/splat.rb +11 -0
  85. data/lib/fuby/strip.rb +24 -0
  86. data/lib/fuby/subclass.rb +22 -0
  87. data/lib/fuby/submodule.rb +22 -0
  88. data/lib/fuby/suffix_of.rb +19 -0
  89. data/lib/fuby/suffixes.rb +11 -0
  90. data/lib/fuby/take.rb +20 -0
  91. data/lib/fuby/take_until.rb +17 -0
  92. data/lib/fuby/take_while.rb +14 -0
  93. data/lib/fuby/tap.rb +28 -0
  94. data/lib/fuby/to.rb +1 -0
  95. data/lib/fuby/to_boolean.rb +32 -0
  96. data/lib/fuby/to_camel_case.rb +32 -0
  97. data/lib/fuby/to_case.rb +1 -0
  98. data/lib/fuby/to_components.rb +33 -0
  99. data/lib/fuby/to_dash_case.rb +28 -0
  100. data/lib/fuby/to_hash.rb +25 -0
  101. data/lib/fuby/to_pred.rb +7 -0
  102. data/lib/fuby/to_proc.rb +45 -0
  103. data/lib/fuby/to_regexp.rb +38 -0
  104. data/lib/fuby/to_snake_case.rb +28 -0
  105. data/lib/fuby/to_tok.rb +27 -0
  106. data/lib/fuby/tok.rb +2 -0
  107. data/lib/fuby/tok_arity.rb +18 -0
  108. data/lib/fuby/tok_direction.rb +16 -0
  109. data/lib/fuby/tok_parity.rb +11 -0
  110. data/lib/fuby/tok_side.rb +11 -0
  111. data/lib/fuby/try.rb +73 -0
  112. data/lib/fuby/try_each.rb +25 -0
  113. data/lib/fuby/try_eval.rb +19 -0
  114. data/lib/fuby/try_exec.rb +15 -0
  115. data/lib/fuby/unabbreviation.rb +17 -0
  116. data/lib/fuby/unshift.rb +1 -0
  117. data/lib/fuby/unshift_options.rb +11 -0
  118. data/lib/fuby/uuid.rb +28 -0
  119. data/test/fuby/abbreviation.rb +25 -0
  120. data/test/fuby/alias_method.rb +29 -0
  121. data/test/fuby/all.rb +15 -0
  122. data/test/fuby/and.rb +14 -0
  123. data/test/fuby/any.rb +15 -0
  124. data/test/fuby/attributes.rb +0 -0
  125. data/test/fuby/bind.rb +17 -0
  126. data/test/fuby/class_attributes.rb +0 -0
  127. data/test/fuby/deep.rb +0 -0
  128. data/test/fuby/deep_each.rb +0 -0
  129. data/test/fuby/define_missing_method.rb +33 -0
  130. data/test/fuby/define_tok.rb +0 -0
  131. data/test/fuby/descendants.rb +0 -0
  132. data/test/fuby/dimension.rb +0 -0
  133. data/test/fuby/drop.rb +28 -0
  134. data/test/fuby/drop_until.rb +26 -0
  135. data/test/fuby/drop_while.rb +26 -0
  136. data/test/fuby/each.rb +0 -0
  137. data/test/fuby/each_with_index_or_key.rb +22 -0
  138. data/test/fuby/eql.rb +0 -0
  139. data/test/fuby/eql_componentwise.rb +0 -0
  140. data/test/fuby/equivalence.rb +0 -0
  141. data/test/fuby/eval.rb +0 -0
  142. data/test/fuby/extend.rb +20 -0
  143. data/test/fuby/format.rb +0 -0
  144. data/test/fuby/include.rb +20 -0
  145. data/test/fuby/index_or_key.rb +0 -0
  146. data/test/fuby/instance_attributes.rb +0 -0
  147. data/test/fuby/intercept.rb +16 -0
  148. data/test/fuby/invert.rb +0 -0
  149. data/test/fuby/longest_common.rb +0 -0
  150. data/test/fuby/matches.rb +0 -0
  151. data/test/fuby/matches_componentwise.rb +0 -0
  152. data/test/fuby/matches_prefix_of.rb +0 -0
  153. data/test/fuby/matches_suffix_of.rb +0 -0
  154. data/test/fuby/member.rb +0 -0
  155. data/test/fuby/merge.rb +0 -0
  156. data/test/fuby/merge_css.rb +0 -0
  157. data/test/fuby/method_added_as.rb +0 -0
  158. data/test/fuby/method_added_as_binary_operator.rb +20 -0
  159. data/test/fuby/method_added_as_filter.rb +28 -0
  160. data/test/fuby/new.rb +3 -0
  161. data/test/fuby/not.rb +0 -0
  162. data/test/fuby/opposite.rb +0 -0
  163. data/test/fuby/or.rb +19 -0
  164. data/test/fuby/otherwise.rb +30 -0
  165. data/test/fuby/part_of.rb +0 -0
  166. data/test/fuby/plus.rb +0 -0
  167. data/test/fuby/pop.rb +0 -0
  168. data/test/fuby/pop_if.rb +31 -0
  169. data/test/fuby/pop_options.rb +31 -0
  170. data/test/fuby/pop_unless.rb +0 -0
  171. data/test/fuby/pop_until.rb +0 -0
  172. data/test/fuby/pop_while.rb +0 -0
  173. data/test/fuby/prefix_of.rb +0 -0
  174. data/test/fuby/prefixes.rb +13 -0
  175. data/test/fuby/prepend.rb +34 -0
  176. data/test/fuby/private.rb +0 -0
  177. data/test/fuby/protected.rb +0 -0
  178. data/test/fuby/public.rb +0 -0
  179. data/test/fuby/push.rb +0 -0
  180. data/test/fuby/push_options.rb +32 -0
  181. data/test/fuby/random.rb +0 -0
  182. data/test/fuby/self_and_descendants.rb +0 -0
  183. data/test/fuby/shift.rb +0 -0
  184. data/test/fuby/shift_if.rb +0 -0
  185. data/test/fuby/shift_options.rb +0 -0
  186. data/test/fuby/shift_unless.rb +0 -0
  187. data/test/fuby/shift_until.rb +0 -0
  188. data/test/fuby/shift_while.rb +0 -0
  189. data/test/fuby/splat.rb +0 -0
  190. data/test/fuby/strip.rb +0 -0
  191. data/test/fuby/submodule.rb +15 -0
  192. data/test/fuby/suffix_of.rb +0 -0
  193. data/test/fuby/suffixes.rb +13 -0
  194. data/test/fuby/take.rb +0 -0
  195. data/test/fuby/take_until.rb +0 -0
  196. data/test/fuby/take_while.rb +0 -0
  197. data/test/fuby/tap.rb +58 -0
  198. data/test/fuby/to_boolean.rb +31 -0
  199. data/test/fuby/to_camel_case.rb +15 -0
  200. data/test/fuby/to_components.rb +0 -0
  201. data/test/fuby/to_dash_case.rb +15 -0
  202. data/test/fuby/to_hash.rb +23 -0
  203. data/test/fuby/to_pred.rb +25 -0
  204. data/test/fuby/to_proc.rb +56 -0
  205. data/test/fuby/to_regexp.rb +38 -0
  206. data/test/fuby/to_snake_case.rb +15 -0
  207. data/test/fuby/to_tok.rb +0 -0
  208. data/test/fuby/tok.rb +0 -0
  209. data/test/fuby/tok_arity.rb +0 -0
  210. data/test/fuby/tok_direction.rb +0 -0
  211. data/test/fuby/tok_parity.rb +0 -0
  212. data/test/fuby/tok_side.rb +0 -0
  213. data/test/fuby/try.rb +52 -0
  214. data/test/fuby/try_each.rb +30 -0
  215. data/test/fuby/try_eval.rb +34 -0
  216. data/test/fuby/try_exec.rb +0 -0
  217. data/test/fuby/unabbreviation.rb +25 -0
  218. data/test/fuby/unshift.rb +0 -0
  219. data/test/fuby/unshift_options.rb +0 -0
  220. data/test/fuby/uuid.rb +31 -0
  221. data/test/fuby/version.rb +0 -0
  222. metadata +369 -0
@@ -0,0 +1 @@
1
+ Dir.glob "#{ __FILE__.chomp ".rb" }_*.rb", &method(:require)
@@ -0,0 +1,16 @@
1
+ module Fuby
2
+ refine ::Module do
3
+
4
+ def method_added_as_binary_operator *names
5
+ names.each do |name|
6
+ prepend! do
7
+ define_method name do |*sig|
8
+ return method name if sig.empty?
9
+ return super *sig
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require 'fuby/prepend'
2
+ require 'fuby/to_pred'
3
+
4
+ module Fuby
5
+ refine ::Module do
6
+
7
+ def method_added_as_filter *names
8
+ names.each do |name|
9
+ prepend! do
10
+ define_method name do |match = nil, &pred|
11
+ return method name if match.nil? && pred.nil?
12
+ return super &(pred || match.to_pred)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+ end
data/lib/fuby/new.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Fuby
2
+ refine ::Float.singleton_class do
3
+
4
+ def new
5
+ 0.0
6
+ end
7
+
8
+ end
9
+ refine ::Integer.singleton_class do
10
+
11
+ def new
12
+ 0
13
+ end
14
+
15
+ end
16
+ end
data/lib/fuby/not.rb ADDED
@@ -0,0 +1,25 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+ refine ::Proc do
5
+
6
+ def not
7
+ proc { |*sig, &blk| ! call *sig, &blk }
8
+ end
9
+
10
+ end
11
+ refine ::TrueClass do
12
+
13
+ def not
14
+ false
15
+ end
16
+
17
+ end
18
+ refine ::FalseClass do
19
+
20
+ def not
21
+ true
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+
5
+ refine ::Symbol do
6
+
7
+ attr_reader :opposite
8
+
9
+ def opposite= sym
10
+ raise ::ArgumentError, "`opposite` of a Symbol must be a Symbol, found #{ sym.class }: #{ sym }" unless ::Symbol === sym
11
+ raise ::ArgumentError, "`opposite` of :#{ self } already :#{ opposite }, found: #{ sym }" unless @opposite.nil? || @opposite == sym
12
+ return if @opposite == sym
13
+ @opposite = sym and sym.opposite = self
14
+ end
15
+
16
+ end
17
+
18
+ :up .opposite = :down
19
+ :top .opposite = :bottom
20
+ :left .opposite = :right
21
+ :first .opposite = :last
22
+ :top_left .opposite = :bottom_right
23
+ :bottom_left .opposite = :top_right
24
+ :asc .opposite = :desc
25
+ :ascending .opposite = :descending
26
+ :to .opposite = :from
27
+ :next .opposite = :previous
28
+
29
+ end
data/lib/fuby/or.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Fuby
2
+ refine ::Hash do
3
+
4
+ def | that
5
+ dup.merge(that) { |key, src, dst| src }
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+ refine ::Object do
5
+
6
+ def otherwise that = nil
7
+ self
8
+ end
9
+
10
+ end
11
+ refine ::NilClass do
12
+
13
+ def otherwise that = nil
14
+ block_given? ? yield : that
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'method_added_as_binary_operator'
2
+ require_relative 'eql_componentwise'
3
+
4
+ using Fuby
5
+
6
+ module Fuby
7
+ module ::Enumerable # can't refine Module
8
+
9
+ def part_of? that
10
+ count <= that.count and eql_componentwise? that
11
+ end
12
+
13
+ method_added_as_binary_operator :part_of?
14
+
15
+ end
16
+ end
data/lib/fuby/plus.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Fuby
2
+ refine ::Regexp do
3
+
4
+ def + that
5
+ that = that.source if Regexp === that
6
+ self.class.new source + that.source, options
7
+ end
8
+
9
+ end
10
+ end
data/lib/fuby/pop.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def pop i = 1
5
+ return super i % length unless empty?
6
+ end
7
+
8
+ end
9
+ end
10
+
11
+ Dir.glob "#{ __FILE__.chomp ".rb" }_*.rb", &method(:require)
@@ -0,0 +1,15 @@
1
+ require_relative 'method_added_as_filter'
2
+
3
+ using Fuby
4
+
5
+ module Fuby
6
+ refine ::Array do
7
+
8
+ def pop_if &blk
9
+ pop if yield first
10
+ end
11
+
12
+ method_added_as_filter :pop_if
13
+
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def pop_options *defaults
5
+ defaults.each_with_object (Hash === last ? pop : {}) do |dft, opt|
6
+ opt.merge!(dft) { |key, val| val }
7
+ end
8
+ end
9
+
10
+ def push_options *defaults
11
+ pop_options(*defaults).tap { |opt| push opt }
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def pop_unless &blk
5
+ pop unless yield first
6
+ end
7
+
8
+ method_added_as_filter :pop_unless
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def pop_until &blk
5
+ pop count - (rindex &blk)
6
+ end
7
+
8
+ method_added_as_filter :pop_until
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def pop_while &blk
5
+ pop_until &(blk.not)
6
+ end
7
+
8
+ method_added_as_filter :pop_while
9
+
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'part_of'
2
+
3
+ module Fuby
4
+ refine ::Array do
5
+
6
+ alias_method :prefix_of?, :part_of?
7
+ alias_method :matches_prefix_of?, :matches_part_of?
8
+
9
+ method_added_as_binary_operator :prefix_of?, :matches_prefix_of?
10
+
11
+ end
12
+ end
13
+
14
+ Dir.glob "*_prefix_of.rb", &method(:require)
@@ -0,0 +1,11 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+ refine ::Array do
5
+
6
+ def prefixes
7
+ length.downto(1).map &(method :take)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Module do
3
+
4
+ def prepend! *includes, &body
5
+ prepend *includes unless includes.empty?
6
+ prepend ::Module.new(&body) if block_given?
7
+ self
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+ refine ::Method do
5
+
6
+ def private? inherited = true
7
+ owner.private_instance_methods(inherited).include? name
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+ refine ::Method do
5
+
6
+ def protected? inherited = true
7
+ owner.protected_instance_methods(inherited).include? name
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ using Fuby
2
+
3
+ module Fuby
4
+ refine ::Method do
5
+
6
+ def public? inherited = true
7
+ owner.public_instance_methods(inherited).include? name
8
+ end
9
+
10
+ end
11
+ end
data/lib/fuby/push.rb ADDED
@@ -0,0 +1 @@
1
+ Dir.glob "#{ __FILE__.chomp ".rb" }_*.rb", &method(:require)
@@ -0,0 +1,9 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def push_options *defaults
5
+ pop_options(*defaults).tap { |opt| push opt }
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ using Fuby
2
+
3
+ require 'securerandom'
4
+
5
+ module Fuby
6
+ refine ::Range do
7
+
8
+ def random
9
+ min + ::SecureRandom.random_number(max - min)
10
+ end
11
+
12
+ end
13
+ refine ::String.singleton_class do
14
+
15
+ def random n = 64
16
+ ::SecureRandom.random_bytes(64).codepoints.each_with_object("") { |n, s| s << n % 26 + 97 }
17
+ end
18
+
19
+ end
20
+ refine ::Symbol.singleton_class do
21
+
22
+ def random n = nil
23
+ ::String.random(n).to_sym
24
+ end
25
+
26
+ def __random__ n = 64
27
+ :"__%s__" % ::String.random(n)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'descendants'
2
+
3
+ using Fuby
4
+
5
+ module Fuby
6
+ refine ::Module do
7
+
8
+ def self_and_descendants type = ::Module
9
+ them = descendants type
10
+ them << self if type === self
11
+ them
12
+ end
13
+
14
+ end
15
+ end
data/lib/fuby/shift.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def shift i = 1
5
+ return super i % length unless empty?
6
+ end
7
+
8
+ end
9
+ end
10
+
11
+ Dir.glob "#{ __FILE__.chomp ".rb" }_*.rb", &method(:require)
@@ -0,0 +1,12 @@
1
+ require_relative 'method_added_as_filter'
2
+
3
+ module Fuby
4
+ refine ::Array do
5
+
6
+ def shift_if
7
+ shift if yield first
8
+ end
9
+
10
+ method_added_as_filter :shift_if
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Fuby
2
+ refine ::Array do
3
+
4
+ def shift_options *defaults
5
+ defaults.each_with_object Hash === first ? shift : {} do |default, opt|
6
+ opt.merge!(default) { |key, preexisting| preexisting }
7
+ end
8
+ end
9
+
10
+ end
11
+ end