facets 4.0.1 → 4.2.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.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +10 -0
  3. data/HISTORY.md +222 -0
  4. data/README.md +2 -2
  5. data/lib/core/facets/array/commonality.rb +1 -1
  6. data/lib/core/facets/array/delete_first.rb +6 -5
  7. data/lib/core/facets/array/divide.rb +1 -1
  8. data/lib/core/facets/array/from_base.rb +68 -0
  9. data/lib/core/facets/array/indexes.rb +1 -1
  10. data/lib/core/facets/array/occur.rb +1 -1
  11. data/lib/core/facets/array/split.rb +16 -9
  12. data/lib/core/facets/array/split_at.rb +3 -3
  13. data/lib/core/facets/array.rb +1 -0
  14. data/lib/core/facets/boolean.rb +2 -2
  15. data/lib/core/facets/class/descendants.rb +26 -12
  16. data/lib/core/facets/dir/ascend.rb +1 -2
  17. data/lib/core/facets/dir/multiglob.rb +4 -9
  18. data/lib/core/facets/enumerable/apply.rb +3 -3
  19. data/lib/core/facets/enumerable/cluster.rb +2 -2
  20. data/lib/core/facets/enumerable/defer.rb +4 -1
  21. data/lib/core/facets/enumerable/hashify.rb +5 -5
  22. data/lib/core/facets/enumerable/with_position.rb +2 -2
  23. data/lib/core/facets/enumerator/fx.rb +11 -11
  24. data/lib/core/facets/essentials.rb +2 -2
  25. data/lib/core/facets/file/atomic_open.rb +1 -1
  26. data/lib/core/facets/file/rewrite.rb +4 -4
  27. data/lib/core/facets/hash/count.rb +19 -8
  28. data/lib/core/facets/hash/deep_merge.rb +25 -20
  29. data/lib/core/facets/hash/deep_rekey.rb +1 -1
  30. data/lib/core/facets/hash/except.rb +1 -1
  31. data/lib/core/facets/hash/op_mul.rb +2 -0
  32. data/lib/core/facets/hash/op_push.rb +7 -7
  33. data/lib/core/facets/hash/rekey.rb +7 -7
  34. data/lib/core/facets/hash/weave.rb +1 -1
  35. data/lib/core/facets/integer/ordinal.rb +18 -13
  36. data/lib/core/facets/integer/to_base.rb +115 -0
  37. data/lib/core/facets/integer.rb +1 -0
  38. data/lib/core/facets/kernel/ask.rb +3 -3
  39. data/lib/core/facets/kernel/blank.rb +13 -8
  40. data/lib/core/facets/kernel/callstack.rb +2 -2
  41. data/lib/core/facets/kernel/d.rb +3 -3
  42. data/lib/core/facets/kernel/demo.rb +2 -2
  43. data/lib/core/facets/kernel/extend.rb +20 -4
  44. data/lib/core/facets/kernel/functor.rb +1 -1
  45. data/lib/core/facets/kernel/instance_assign.rb +2 -3
  46. data/lib/core/facets/kernel/instance_replace.rb +2 -2
  47. data/lib/core/facets/kernel/maybe.rb +1 -1
  48. data/lib/core/facets/kernel/meta_class.rb +2 -1
  49. data/lib/core/facets/kernel/meta_def.rb +3 -3
  50. data/lib/core/facets/kernel/object_hexid.rb +2 -7
  51. data/lib/core/facets/kernel/try.rb +3 -1
  52. data/lib/core/facets/module/attr_class_accessor.rb +2 -2
  53. data/lib/core/facets/module/class_accessor.rb +2 -2
  54. data/lib/core/facets/module/instance_method.rb +2 -2
  55. data/lib/core/facets/module/is.rb +1 -1
  56. data/lib/core/facets/numeric/delimit.rb +1 -1
  57. data/lib/core/facets/proc/wrap.rb +1 -1
  58. data/lib/core/facets/range/op_sub.rb +2 -2
  59. data/lib/core/facets/range/overlap.rb +32 -1
  60. data/lib/core/facets/range/within.rb +1 -1
  61. data/lib/core/facets/regexp/op_or.rb +1 -1
  62. data/lib/core/facets/string/align.rb +4 -4
  63. data/lib/core/facets/string/ascii_only.rb +2 -2
  64. data/lib/core/facets/string/dashcase.rb +1 -1
  65. data/lib/core/facets/string/file.rb +1 -1
  66. data/lib/core/facets/string/from_base.rb +85 -0
  67. data/lib/core/facets/string/indent.rb +35 -23
  68. data/lib/core/facets/string/margin.rb +18 -4
  69. data/lib/core/facets/string/mask.rb +264 -0
  70. data/lib/core/facets/string/natcmp.rb +1 -1
  71. data/lib/core/facets/string/random.rb +1 -1
  72. data/lib/core/facets/string/remove.rb +13 -8
  73. data/lib/core/facets/string/rotate.rb +1 -1
  74. data/lib/core/facets/string/similarity.rb +2 -2
  75. data/lib/core/facets/string/squish.rb +5 -3
  76. data/lib/core/facets/string/trim.rb +1 -1
  77. data/lib/core/facets/string/unfold.rb +1 -1
  78. data/lib/core/facets/string/word_wrap.rb +1 -1
  79. data/lib/core/facets/string/words.rb +2 -2
  80. data/lib/core/facets/string.rb +3 -0
  81. data/lib/core/facets/symbol/call.rb +0 -9
  82. data/lib/core/facets/time/change.rb +51 -29
  83. data/lib/core/facets/time/future.rb +6 -2
  84. data/lib/core/facets/time/stamp.rb +1 -1
  85. data/lib/core/facets/time/to_time.rb +3 -7
  86. data/lib/core/facets/version.rb +1 -1
  87. data/lib/rails/facets/file/atomic_write.rb +1 -1
  88. data/lib/standard/facets/crypt3.rb +4 -2
  89. data/lib/standard/facets/digest/salted_digest.rb +6 -2
  90. data/lib/standard/facets/fileutils/ln_r.rb +7 -56
  91. data/lib/standard/facets/mirror.rb +276 -0
  92. data/lib/standard/facets/ostruct/initialize.rb +7 -6
  93. data/lib/standard/facets/ostruct.rb +3 -3
  94. data/lib/standard/facets/paramix.rb +150 -0
  95. data/lib/standard/facets/pathname/glob.rb +11 -7
  96. data/lib/standard/facets/shellwords.rb +53 -5
  97. data/lib/standard/facets/zlib.rb +5 -9
  98. metadata +7 -1
@@ -1,6 +1,8 @@
1
+ require 'fileutils'
2
+
1
3
  module FileUtils
2
4
 
3
- unless method_defined?(:ln_r)
5
+ unless respond_to?(:ln_r)
4
6
 
5
7
  #
6
8
  # Options: noop verbose dereference_root remove_destination
@@ -25,6 +27,9 @@ module FileUtils
25
27
  # FileUtils.ln_r 'src/.', 'dest' # cp_r('src', 'dest') makes src/dest,
26
28
  # # but this doesn't.
27
29
  #
30
+ # It is much like Ruby's own FileUtils.cp_lr, and uses Ruby's
31
+ # FileUtils.link_entry to make the links.
32
+ #
28
33
  # TODO: Why --remove-destination and not just --force?
29
34
  def ln_r(src, dest, options = {})
30
35
  fu_output_message "ln -r#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
@@ -37,61 +42,7 @@ module FileUtils
37
42
  end
38
43
  module_function :ln_r
39
44
 
40
- OPT_TABLE = {} unless const_defined?(:OPT_TABLE)
41
- OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination]
42
-
43
- #
44
- # Hard links a file system entry +src+ to +dest+.
45
- # If +src+ is a directory, this method links its contents recursively.
46
- #
47
- # Both of +src+ and +dest+ must be a path name.
48
- # +src+ must exist, +dest+ must not exist.
49
- #
50
- # If +dereference_root+ is true, this method dereference tree root.
51
- #
52
- # If +remove_destination+ is true, this method removes each destination file before copy.
53
- #
54
- def link_entry(src, dest, dereference_root = false, remove_destination = false)
55
- Entry_.new(src, nil, dereference_root).traverse do |ent|
56
- destent = Entry_.new(dest, ent.rel, false)
57
- File.unlink destent.path if remove_destination && File.file?(destent.path)
58
- ent.link destent.path
59
- end
60
- end
61
- module_function :link_entry
62
-
63
- class Entry_ #:nodoc:
64
- def link(dest)
65
- case
66
- when directory?
67
- if !File.exist?(dest) and descendant_diretory?(dest, path)
68
- raise ArgumentError, "cannot link directory %s to itself %s" % [path, dest]
69
- end
70
- begin
71
- Dir.mkdir dest
72
- rescue
73
- raise unless File.directory?(dest)
74
- end
75
- else file?
76
- File.link path(), dest
77
- end
78
- end
79
-
80
- unless const_defined?(:DIRECTORY_TERM)
81
- if File::ALT_SEPARATOR
82
- DIRECTORY_TERM = "(?=[/#{Regexp.quote(File::ALT_SEPARATOR)}]|\\z)".freeze
83
- else
84
- DIRECTORY_TERM = "(?=/|\\z)".freeze
85
- end
86
- SYSCASE = File::FNM_SYSCASE.nonzero? ? "-i" : ""
87
- end
88
-
89
- unless method_defined?(:descendant_diretory?)
90
- def descendant_diretory?(descendant, ascendant)
91
- /\A(?#{SYSCASE}:#{Regexp.quote(ascendant)})#{DIRECTORY_TERM}/ =~ File.dirname(descendant)
92
- end
93
- end
94
- end
45
+ OPT_TABLE['ln_r'] = [:noop, :verbose, :dereference_root, :remove_destination] if const_defined?(:OPT_TABLE) && !OPT_TABLE.frozen?
95
46
 
96
47
  end
97
48
 
@@ -0,0 +1,276 @@
1
+ module Kernel
2
+
3
+ # A Mirror looks at an object from the outside: its instance variables,
4
+ # its class and its methods. Ruby puts these reflection methods on every
5
+ # object, where a class can redefine them or, like BasicObject, not have
6
+ # them. A mirror calls Ruby's own versions, saved when this file loads,
7
+ # so it gives the true answers for any object.
8
+ #
9
+ # class Song
10
+ # def initialize(title, year)
11
+ # @title, @year = title, year
12
+ # end
13
+ # end
14
+ #
15
+ # song = Song.new("Paranoid", 1970)
16
+ # mirror = song.mirror # or Mirror.new(song)
17
+ #
18
+ # mirror.keys #=> [:title, :year]
19
+ # mirror[:title] #=> "Paranoid"
20
+ # mirror.to_h #=> {title: "Paranoid", year: 1970}
21
+ # mirror.update(year: 1971)
22
+ # mirror[:year] #=> 1971
23
+ #
24
+ # Instance variable names can be given with or without the "@".
25
+ #
26
+ # The class lives in Kernel, so `Mirror` works everywhere, but an
27
+ # application's own top-level `Mirror` class doesn't clash with it: the
28
+ # application's takes precedence, and this one is still
29
+ # `Kernel::Mirror`. A class can also define its own `#mirror`, so code
30
+ # that must work on any object should use `Mirror.new(obj)`.
31
+ #
32
+ # NOTE: This is not loaded by `require 'facets'`. Use:
33
+ #
34
+ # require 'facets/mirror'
35
+ #
36
+ # Based on the Instance gem.
37
+ #
38
+ # Since: 4.2.0
39
+ # Author: Trans
40
+
41
+ class Mirror
42
+ include Enumerable
43
+
44
+ # Ruby's own reflection methods, saved so that later changes to them,
45
+ # or an object that redefines them, can't affect a mirror. Methods
46
+ # from Kernel and BasicObject can be called on any object, including
47
+ # a BasicObject.
48
+ KERNEL = %i[
49
+ class instance_of? kind_of? object_id to_s
50
+ instance_variables instance_variable_get instance_variable_set
51
+ instance_variable_defined? remove_instance_variable
52
+ methods public_methods protected_methods private_methods
53
+ singleton_methods method
54
+ ].to_h { |name| [name, ::Kernel.instance_method(name)] }.merge(
55
+ %i[instance_eval instance_exec].to_h { |name| [name, ::BasicObject.instance_method(name)] }
56
+ ).freeze
57
+
58
+ # Ruby's own Module reflection methods, for a mirror of a module or
59
+ # class.
60
+ MODULE = %i[
61
+ instance_methods public_instance_methods protected_instance_methods
62
+ private_instance_methods instance_method
63
+ ].to_h { |name| [name, ::Module.instance_method(name)] }.freeze
64
+
65
+ private_constant :KERNEL, :MODULE
66
+
67
+ # A mirror of +object+.
68
+ def initialize(object)
69
+ @object = object
70
+ end
71
+
72
+ # The object this mirror reflects.
73
+ attr_reader :object
74
+
75
+ # Instance variable names as Ruby gives them, with the "@".
76
+ #
77
+ # mirror.variables #=> [:@title, :@year]
78
+ def variables
79
+ call(:instance_variables)
80
+ end
81
+
82
+ # Instance variable names as symbols, without the "@".
83
+ #
84
+ # mirror.keys #=> [:title, :year]
85
+ def keys
86
+ variables.map { |name| name[1..-1].to_sym }
87
+ end
88
+
89
+ # Instance variable values, in the same order as #keys.
90
+ def values
91
+ variables.map { |name| call(:instance_variable_get, name) }
92
+ end
93
+
94
+ # Number of instance variables.
95
+ def size
96
+ variables.size
97
+ end
98
+
99
+ # Yields each instance variable's name, without the "@", and value.
100
+ # Mirror is Enumerable, so `to_h`, `select` and the rest work too.
101
+ #
102
+ # mirror.to_h #=> {title: "Paranoid", year: 1970}
103
+ def each
104
+ return to_enum(:each) { size } unless block_given?
105
+ variables.each do |name|
106
+ yield name[1..-1].to_sym, call(:instance_variable_get, name)
107
+ end
108
+ self
109
+ end
110
+
111
+ # Value of an instance variable, or nil if it isn't set.
112
+ def get(name)
113
+ call(:instance_variable_get, ivar(name))
114
+ end
115
+ alias_method :[], :get
116
+
117
+ # Sets an instance variable, and returns the value.
118
+ def set(name, value)
119
+ call(:instance_variable_set, ivar(name), value)
120
+ end
121
+ alias_method :[]=, :set
122
+
123
+ # Sets instance variables from a hash of names and values, and
124
+ # returns the mirror.
125
+ #
126
+ # mirror.update(title: "War Pigs", year: 1970)
127
+ def update(hash)
128
+ hash.each { |name, value| set(name, value) }
129
+ self
130
+ end
131
+
132
+ # Is the instance variable set?
133
+ def key?(name)
134
+ call(:instance_variable_defined?, ivar(name))
135
+ end
136
+
137
+ # Removes an instance variable, and returns its value. Raises
138
+ # NameError if it isn't set.
139
+ def remove(name)
140
+ call(:remove_instance_variable, ivar(name))
141
+ end
142
+
143
+ # Evaluates a string or a block with the object as self, like
144
+ # `instance_eval`.
145
+ def eval(*args, &block)
146
+ call(:instance_eval, *args, &block)
147
+ end
148
+
149
+ # Calls the block with the object as self, passing the arguments, like
150
+ # `instance_exec`.
151
+ def exec(*args, &block)
152
+ call(:instance_exec, *args, &block)
153
+ end
154
+
155
+ # The object's class, even if the object redefines #class.
156
+ def object_class
157
+ call(:class)
158
+ end
159
+
160
+ # Is the object an instance of exactly this class?
161
+ def of?(klass)
162
+ call(:instance_of?, klass)
163
+ end
164
+
165
+ # Is the object an instance of this class or module, or of a
166
+ # subclass, as with `is_a?`?
167
+ def is?(mod)
168
+ call(:kind_of?, mod)
169
+ end
170
+
171
+ # The object's object_id.
172
+ def id
173
+ call(:object_id)
174
+ end
175
+
176
+ # Names of the object's methods. With no arguments, the same as
177
+ # `methods`. Otherwise any of :public, :protected, :private and
178
+ # :singleton, or :all for the first three.
179
+ #
180
+ # mirror.method_names(:private)
181
+ def method_names(*kinds)
182
+ return call(:methods) if kinds.empty?
183
+ kinds = [:public, :protected, :private] if kinds.include?(:all)
184
+ kinds.uniq.flat_map do |kind|
185
+ case kind
186
+ when :public then call(:public_methods)
187
+ when :protected then call(:protected_methods)
188
+ when :private then call(:private_methods)
189
+ when :singleton then call(:singleton_methods)
190
+ else raise ArgumentError, "unknown kind of method: #{kind.inspect}"
191
+ end
192
+ end
193
+ end
194
+
195
+ # The object's method of this name, as a Method, even if the object
196
+ # redefines #method.
197
+ def method_object(name)
198
+ call(:method, name)
199
+ end
200
+
201
+ # For a module or class: names of the methods it defines for its
202
+ # instances. With no arguments, the same as `instance_methods`.
203
+ # Otherwise any of :public, :protected and :private, or :all.
204
+ # Raises TypeError for any other object.
205
+ def method_definitions(*kinds)
206
+ return module_call(:instance_methods) if kinds.empty?
207
+ kinds = [:public, :protected, :private] if kinds.include?(:all)
208
+ kinds.uniq.flat_map do |kind|
209
+ case kind
210
+ when :public then module_call(:public_instance_methods)
211
+ when :protected then module_call(:protected_instance_methods)
212
+ when :private then module_call(:private_instance_methods)
213
+ else raise ArgumentError, "unknown kind of method: #{kind.inspect}"
214
+ end
215
+ end
216
+ end
217
+
218
+ # For a module or class: the instance method of this name, as an
219
+ # UnboundMethod. Raises TypeError for any other object.
220
+ def method_definition(name)
221
+ module_call(:instance_method, name)
222
+ end
223
+
224
+ # Two mirrors are equal if they reflect the same object.
225
+ def ==(other)
226
+ other.is_a?(Mirror) && other.object.equal?(@object)
227
+ end
228
+ alias_method :eql?, :==
229
+
230
+ def hash
231
+ [Mirror, id].hash
232
+ end
233
+
234
+ # Shows the object's class and address, without calling the
235
+ # object's own #inspect, which it may not have.
236
+ def inspect
237
+ "#<#{self.class} of #{call(:to_s)}>"
238
+ end
239
+
240
+ private
241
+
242
+ def call(name, *args, &block)
243
+ KERNEL.fetch(name).bind_call(@object, *args, &block)
244
+ end
245
+
246
+ def module_call(name, *args)
247
+ unless call(:kind_of?, ::Module)
248
+ raise TypeError, "#{call(:class)} is not a module or class"
249
+ end
250
+ MODULE.fetch(name).bind_call(@object, *args)
251
+ end
252
+
253
+ def ivar(name)
254
+ name = name.to_s
255
+ name.start_with?('@') ? name.to_sym : :"@#{name}"
256
+ end
257
+ end
258
+
259
+ # A Mirror of this object, for looking at its instance variables,
260
+ # class and methods from the outside.
261
+ #
262
+ # song.mirror.to_h #=> {title: "Paranoid", year: 1970}
263
+ #
264
+ # A class can define its own #mirror. To be sure of getting a mirror
265
+ # for any object, use `Mirror.new(obj)`.
266
+ #
267
+ # NOTE: This is not loaded by `require 'facets'`. Use:
268
+ #
269
+ # require 'facets/mirror'
270
+ #
271
+ # Since: 4.2.0
272
+ def mirror
273
+ ::Kernel::Mirror.new(self)
274
+ end
275
+
276
+ end
@@ -2,6 +2,9 @@ require 'ostruct'
2
2
 
3
3
  class OpenStruct
4
4
 
5
+ alias_method :__initialize_without_block__, :initialize
6
+ private :__initialize_without_block__
7
+
5
8
  # Allows the initialization of an OpenStruct with a setter block:
6
9
  #
7
10
  # person = OpenStruct.new do |o|
@@ -17,14 +20,12 @@ class OpenStruct
17
20
  # p.gender = :M
18
21
  # end
19
22
  #
23
+ # The hash is handled by Ruby's own OpenStruct#initialize, which this
24
+ # calls first.
25
+ #
20
26
  # CREDIT: Noah Gibbs, Gavin Sinclair
21
27
  def initialize(hash=nil, &block)
22
- @table = {}
23
- if hash
24
- hash.each_pair do |k, v|
25
- self[k.to_sym] = v
26
- end
27
- end
28
+ __initialize_without_block__(hash)
28
29
  if block && block.arity == 1
29
30
  yield self
30
31
  end
@@ -1,8 +1,8 @@
1
1
  # Facets' extensions to Ruby's OpenStruct.
2
2
  #
3
- # NOTE: `ostruct` is deprecated as of Ruby 3.4 and is no longer a default gem
4
- # as of Ruby 3.5 — it must be installed/declared explicitly to be available
5
- # (`gem install ostruct`, or add `ostruct` to your Gemfile/gemspec). The gem
3
+ # NOTE: `ostruct` is deprecated as of Ruby 3.4, and as of Ruby 4.0 it is a
4
+ # bundled gem rather than a default gem. Ruby still installs it, but a Bundler
5
+ # project must list `ostruct` in its Gemfile or gemspec to load it. The gem
6
6
  # itself still exists, so these extensions continue to work on top of it.
7
7
  #
8
8
  # Given OpenStruct's deprecation, Facets may divest these extensions in a
@@ -0,0 +1,150 @@
1
+ # Paramix makes a module parametric: one that takes parameters where it
2
+ # is included or extended, with `Module[parameters]`. Its `parameterized`
3
+ # block is given the parameters, and defines the methods that depend on
4
+ # them.
5
+ #
6
+ # module Greeter
7
+ # include Paramix
8
+ #
9
+ # parameterized do |params|
10
+ # define_method :hello do
11
+ # "Hello from #{params[:name]}!"
12
+ # end
13
+ # end
14
+ # end
15
+ #
16
+ # class English
17
+ # include Greeter[:name => 'England']
18
+ # end
19
+ #
20
+ # class Scots
21
+ # include Greeter[:name => 'Scotland']
22
+ # end
23
+ #
24
+ # English.new.hello #=> "Hello from England!"
25
+ # Scots.new.hello #=> "Hello from Scotland!"
26
+ #
27
+ # Each use of `Module[parameters]` makes a new module that includes the
28
+ # parametric one, so each class gets its own methods. The block runs in
29
+ # that module, where `public`, `private` and `protected` also define a
30
+ # method when given a block:
31
+ #
32
+ # module Named
33
+ # include Paramix
34
+ #
35
+ # parameterized do |params|
36
+ # public(params[:as]) { params[:value] }
37
+ # end
38
+ # end
39
+ #
40
+ # class Config
41
+ # extend Named[:as => :env, :value => 'test']
42
+ # end
43
+ #
44
+ # Config.env #=> "test"
45
+ #
46
+ # A module that includes a parametric module without giving it
47
+ # parameters becomes parametric too, and passes its own parameters on.
48
+ #
49
+ # NOTE: This is not loaded by `require 'facets'`. Use:
50
+ #
51
+ # require 'facets/paramix'
52
+ #
53
+ # Based on the Paramix gem.
54
+ #
55
+ # Since: 4.2.0
56
+ # Author: Trans
57
+
58
+ module Paramix
59
+
60
+ #
61
+ def self.included(base)
62
+ base.extend(Extensions)
63
+ end
64
+
65
+ # Class-level methods of a parametric module.
66
+ module Extensions
67
+
68
+ # Apply parameters, giving a module to include or extend.
69
+ #
70
+ # Returns a new Mixin. [Mixin]
71
+ def [](parameters={})
72
+ Mixin.new(self, parameters)
73
+ end
74
+
75
+ # Add a block to run when parameters are applied. Without a block,
76
+ # gives the blocks added so far.
77
+ #
78
+ # Returns the list of blocks. [Array<Proc>]
79
+ def parameterized(&code)
80
+ @code ||= []
81
+ if block_given?
82
+ @code << code
83
+ else
84
+ @code
85
+ end
86
+ end
87
+
88
+ #
89
+ def append_features(base)
90
+ return super(base) if Mixin === base || Mixin === self
91
+
92
+ base.extend(Extensions)
93
+
94
+ anc = ancestors.find{ |a| a.respond_to?(:parameterized) }
95
+ base.parameterized.concat(anc.parameterized)
96
+
97
+ super(base)
98
+ end
99
+
100
+ #
101
+ def extend_object(base)
102
+ return super(base) if Mixin === base || Mixin === self
103
+
104
+ base.extend(Extensions)
105
+
106
+ anc = ancestors.find{ |a| a.respond_to?(:parameterized) }
107
+ base.parameterized.concat(anc.parameterized)
108
+
109
+ super(base)
110
+ end
111
+
112
+ end
113
+
114
+ # An instance of the Mixin module class is what is produced
115
+ # when parameters are applied to a parametric module.
116
+ #
117
+ class Mixin < Module
118
+
119
+ def initialize(base, parameters)
120
+ super()
121
+ include(base)
122
+ base.parameterized.each do |code|
123
+ instance_exec(parameters, &code)
124
+ end
125
+ end
126
+
127
+ # As Module#public, and given a block, define the method first.
128
+ def public(*names, &code)
129
+ define_method(names.first, &code) if code
130
+ super(*names)
131
+ end
132
+
133
+ # As Module#private, and given a block, define the method first.
134
+ def private(*names, &code)
135
+ define_method(names.first, &code) if code
136
+ super(*names)
137
+ end
138
+
139
+ # As Module#protected, and given a block, define the method first.
140
+ def protected(*names, &code)
141
+ define_method(names.first, &code) if code
142
+ super(*names)
143
+ end
144
+
145
+ end
146
+
147
+ # The name this module had in the Paramix gem.
148
+ Parametric = self
149
+
150
+ end
@@ -1,5 +1,10 @@
1
+ require 'pathname'
2
+
1
3
  class Pathname
2
4
 
5
+ alias_method :_glob_original, :glob
6
+ private :_glob_original
7
+
3
8
  # Extends Ruby's built-in Pathname#glob to accept symbol-based
4
9
  # flags in addition to the standard File::FNM_* constants.
5
10
  #
@@ -7,15 +12,14 @@ class Pathname
7
12
  #
8
13
  # Supported symbols: :noescape, :pathname, :dotmatch, :casefold
9
14
  #
10
- alias_method :_glob_original, :glob
11
- private :_glob_original
12
-
13
- def glob(match, *opts)
15
+ # Otherwise it is Ruby's own Pathname#glob. With a block, it gives the
16
+ # block each match and returns nil.
17
+ #
18
+ def glob(match, *opts, &block)
14
19
  if opts.any? { |o| o.is_a?(Symbol) || o.is_a?(String) }
15
- flags = glob_flags(opts)
16
- Dir.glob(::File.join(self.to_s, match), flags).map { |m| self.class.new(m) }
20
+ _glob_original(match, glob_flags(opts), &block)
17
21
  else
18
- _glob_original(match, *opts)
22
+ _glob_original(match, *opts, &block)
19
23
  end
20
24
  end
21
25
 
@@ -67,21 +67,65 @@ class Array
67
67
  # The array is accepted in the format of Ruby
68
68
  # method arguments --ie. [arg1, arg2, ..., hash]
69
69
  #
70
+ # ['a', 'b', {d: true}].shellwords #=> ["-d", "a", "b"]
71
+ #
72
+ # To make a command line of them, join them with Ruby's Array#shelljoin:
73
+ #
74
+ # ['a', 'b', {d: true}].shellwords.shelljoin #=> "-d a b"
75
+ #
70
76
  def shellwords
71
77
  opts, args = *flatten.partition{ |e| Hash === e }
72
78
  opts = opts.inject({}){ |m,h| m.update(h); m }
73
79
  opts.shellwords + args
74
80
  end
75
81
 
82
+ end
83
+
84
+ module Facets
85
+
86
+ # Facets' Array#shelljoin used to convert the array with #shellwords first,
87
+ # which changed Ruby's own Array#shelljoin: nested arrays were flattened
88
+ # and hashes became options, so structured input, such as a web request's
89
+ # parameters, could add arguments to a command. Ruby's version escapes
90
+ # each element as one argument. Facets now leaves it alone, and only
91
+ # warns when the old Facets version would have given a different result.
76
92
  #
77
- def shelljoin
78
- Shellwords.shelljoin(shellwords)
93
+ # @deprecated Scheduled for removal after 2027-09-30.
94
+ module ArrayShelljoin
95
+ def shelljoin
96
+ if any? { |e| Hash === e || Array === e }
97
+ warn "Facets' Array#shelljoin is deprecated. Array#shelljoin now escapes each element, " \
98
+ "as Ruby's own does, so hashes and nested arrays are no longer turned into options. " \
99
+ "To get those, use array.shellwords.shelljoin. It will be removed after 2027-09-30.", uplevel: 1
100
+ end
101
+ super
102
+ end
79
103
  end
80
104
 
81
105
  end
82
106
 
107
+ Array.prepend(Facets::ArrayShelljoin)
108
+
83
109
  class Hash
84
110
 
111
+ # Convert a hash into command line flags, one word per flag.
112
+ #
113
+ # {debug: true, name: 'jim', v: true}.shellwords
114
+ # #=> ["--debug", "--name=jim", "-v"]
115
+ #
116
+ # A one-letter key gives a short flag (`-v`) and a longer key a long one
117
+ # (`--debug`). A value of `true` gives the bare flag, and `false` or `nil`
118
+ # leaves the flag out. An array value repeats the flag for each element.
119
+ # Any other value is joined to the flag with `=`.
120
+ #
121
+ # {I: ['lib', 'test'], quiet: false}.shellwords
122
+ # #=> ["-I=lib", "-I=test"]
123
+ #
124
+ # Each flag and its value is one word, unquoted, ready to pass to
125
+ # `system(*words)` or to join with Ruby's Array#shelljoin, which escapes
126
+ # what the shell needs.
127
+ #
128
+ # {name: 'a b'}.shellwords.shelljoin #=> "--name\\=a\\ b"
85
129
  #
86
130
  def shellwords
87
131
  argv = []
@@ -91,17 +135,21 @@ class Hash
91
135
  when false, nil
92
136
  when Array
93
137
  v.each do |e|
94
- argv << %[#{m}#{f}="#{e}"]
138
+ argv << "#{m}#{f}=#{e}"
95
139
  end
96
140
  when true
97
- argv << %[#{m}#{f}]
141
+ argv << "#{m}#{f}"
98
142
  else
99
- argv << %[#{m}#{f}="#{v}"]
143
+ argv << "#{m}#{f}=#{v}"
100
144
  end
101
145
  end
102
146
  argv
103
147
  end
104
148
 
149
+ # Convert a hash into command line flags, escaped and joined into one
150
+ # string. See Hash#shellwords.
151
+ #
152
+ # {debug: true, name: 'jim'}.shelljoin #=> "--debug --name\\=jim"
105
153
  #
106
154
  def shelljoin
107
155
  shellwords.shelljoin
@@ -1,3 +1,6 @@
1
+ require 'zlib'
2
+ require 'stringio'
3
+
1
4
  # A convenient wrapper for the zlib standard library that allows
2
5
  # compression/decompression of strings with gzip.
3
6
 
@@ -24,15 +27,8 @@ module Zlib
24
27
  output.string
25
28
  end
26
29
 
27
- # Inflate a deflated sting.
28
- def self.inflate(string)
29
- Inflate.inflate(string)
30
- end
31
-
32
- # Deflate a string.
33
- def self.deflate(string, level=DEFAULT_COMPRESSION)
34
- Deflate.deflate(string, level)
35
- end
30
+ # Zlib.inflate and Zlib.deflate are Ruby's own. Facets used to define
31
+ # copies of them here.
36
32
 
37
33
  end
38
34