ruby_ext 0.4.22 → 0.4.23
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruby_ext/core/array.rb +8 -0
- data/lib/ruby_ext/core/hash.rb +1 -1
- data/lib/ruby_ext/core/module.rb +43 -0
- data/lib/ruby_ext/core/string.rb +10 -0
- data/lib/ruby_ext/more/miscellaneous.rb +45 -0
- data/lib/ruby_ext.rb +3 -0
- data/spec/more/miscellaneous_spec.rb +14 -0
- metadata +3 -2
data/lib/ruby_ext/core/array.rb
CHANGED
data/lib/ruby_ext/core/hash.rb
CHANGED
data/lib/ruby_ext/core/module.rb
CHANGED
@@ -133,4 +133,47 @@ Module.class_eval do
|
|
133
133
|
end
|
134
134
|
|
135
135
|
public :include, :define_method
|
136
|
+
|
137
|
+
|
138
|
+
# copied from rails
|
139
|
+
def delegate(*methods)
|
140
|
+
options = methods.pop
|
141
|
+
unless options.is_a?(Hash) && to = options[:to]
|
142
|
+
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
|
143
|
+
end
|
144
|
+
|
145
|
+
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
|
146
|
+
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
|
147
|
+
end
|
148
|
+
|
149
|
+
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" || ''
|
150
|
+
|
151
|
+
file, line = caller.first.split(':', 2)
|
152
|
+
line = line.to_i
|
153
|
+
|
154
|
+
methods.each do |method|
|
155
|
+
on_nil =
|
156
|
+
if options[:allow_nil]
|
157
|
+
'return'
|
158
|
+
else
|
159
|
+
%(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
|
160
|
+
end
|
161
|
+
|
162
|
+
module_eval(<<-EOS, file, line - 5)
|
163
|
+
if instance_methods(false).map(&:to_s).include?("#{prefix}#{method}")
|
164
|
+
remove_possible_method("#{prefix}#{method}")
|
165
|
+
end
|
166
|
+
|
167
|
+
def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
|
168
|
+
#{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
|
169
|
+
rescue NoMethodError # rescue NoMethodError
|
170
|
+
if #{to}.nil? # if client.nil?
|
171
|
+
#{on_nil} # return # depends on :allow_nil
|
172
|
+
else # else
|
173
|
+
raise # raise
|
174
|
+
end # end
|
175
|
+
end # end
|
176
|
+
EOS
|
177
|
+
end
|
178
|
+
end
|
136
179
|
end
|
data/lib/ruby_ext/core/string.rb
CHANGED
@@ -65,5 +65,15 @@ String.class_eval do
|
|
65
65
|
constant
|
66
66
|
end
|
67
67
|
|
68
|
+
class String
|
69
|
+
def substitute(*args)
|
70
|
+
gsub(*args){yield Regexp.last_match.captures}
|
71
|
+
end
|
72
|
+
|
73
|
+
def substitute!(*args)
|
74
|
+
gsub!(*args){yield Regexp.last_match.captures}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
68
78
|
alias_method :blank?, :empty?
|
69
79
|
end
|
@@ -4,4 +4,49 @@ Kernel.class_eval do
|
|
4
4
|
puts args.collect{|a| a.inspect}.join(' ')
|
5
5
|
return *args
|
6
6
|
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
#
|
11
|
+
# Parsing command line
|
12
|
+
#
|
13
|
+
module RubyExt
|
14
|
+
def self.argv input = ARGV
|
15
|
+
# don't want to modify original input and can't use :clone (it may be not an array)
|
16
|
+
input = input.collect{|v| v}
|
17
|
+
|
18
|
+
list, options = [], {}
|
19
|
+
until input.empty? do
|
20
|
+
arg = input.shift.strip
|
21
|
+
if arg =~ /.+:$/ and !input.empty?
|
22
|
+
k = arg[0..-2].to_sym
|
23
|
+
v = input.shift
|
24
|
+
v = v.sub(/\s*,$/, '') if v
|
25
|
+
options[k] = simple_cast(v)
|
26
|
+
else
|
27
|
+
v = arg.gsub(/^['"]|['"]$/, '')
|
28
|
+
v = v.sub(/\s*,$/, '')
|
29
|
+
list << simple_cast(v)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
list << options
|
33
|
+
list
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
def self.simple_cast v
|
38
|
+
if v =~ /^:[a-z_0-9]+$/i
|
39
|
+
v[1..-1].to_sym
|
40
|
+
elsif v =~ /^[0-9]+$/
|
41
|
+
v.to_i
|
42
|
+
elsif v =~ /^[0-9]*\.[0-9]+$/
|
43
|
+
v.to_f
|
44
|
+
elsif v =~ /^\/.+\/$/
|
45
|
+
Regexp.new v[1..-2]
|
46
|
+
elsif v == 'nil'
|
47
|
+
nil
|
48
|
+
else
|
49
|
+
v
|
50
|
+
end
|
51
|
+
end
|
7
52
|
end
|
data/lib/ruby_ext.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "more/spec_helper"
|
2
|
+
|
3
|
+
describe "Miscellaneous" do
|
4
|
+
it "ARGV parsing" do
|
5
|
+
{
|
6
|
+
%(:a a 'a' 2 3.4 /a/) => [:a, 'a', 'a', 2, 3.4, /a/, {}],
|
7
|
+
%(a, b) => ['a', 'b', {}],
|
8
|
+
%(k: v) => [{k: 'v'}],
|
9
|
+
%(k: v, k2: 2, k3: nil) => [{k: 'v', k2: 2, k3: nil}]
|
10
|
+
}.each do |input, result|
|
11
|
+
RubyExt.argv(input.split(/\s/)).should == result
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.23
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-04 00:00:00 +04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- spec/core/open_object_spec.rb
|
76
76
|
- spec/core/spec_helper.rb
|
77
77
|
- spec/more/declarative_cache_spec.rb
|
78
|
+
- spec/more/miscellaneous_spec.rb
|
78
79
|
- spec/more/observable2_spec.rb
|
79
80
|
- spec/more/open_constructor_spec.rb
|
80
81
|
- spec/more/safe_hash_spec.rb
|