qualitysmith_extensions 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/qualitysmith_extensions/array/group_by.rb +1 -1
- data/lib/qualitysmith_extensions/kernel/die.rb +13 -3
- data/lib/qualitysmith_extensions/kernel/example_printer.rb +75 -0
- data/lib/qualitysmith_extensions/module/alias_method.rb +6 -0
- data/lib/qualitysmith_extensions/module/attribute_accessors.rb +46 -0
- data/lib/qualitysmith_extensions/module/includable_once.rb +10 -0
- data/lib/qualitysmith_extensions/object/default.rb +69 -0
- data/lib/qualitysmith_extensions/object/methods.rb +58 -0
- metadata +10 -4
- data/lib/qualitysmith_extensions/object/own_methods.rb +0 -5
@@ -2,13 +2,14 @@
|
|
2
2
|
# Author:: Tyler Rick
|
3
3
|
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
4
|
# License:: Ruby License
|
5
|
-
# Submit to Facets?::
|
5
|
+
# Submit to Facets?:: No.
|
6
|
+
# Deprecated. Because I discovered Kernel::abort !
|
6
7
|
#++
|
7
8
|
|
8
9
|
module Kernel
|
9
|
-
def die(message)
|
10
|
+
def die(message, exit_code = 1)
|
10
11
|
$stderr.puts message
|
11
|
-
exit
|
12
|
+
exit exit_code
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
@@ -35,5 +36,14 @@ class TheTest < Test::Unit::TestCase
|
|
35
36
|
assert_equal "Aggh! I'm dying!", stderr.chomp
|
36
37
|
end
|
37
38
|
|
39
|
+
def test_abort
|
40
|
+
stderr = capture_output $stderr do
|
41
|
+
assert_raise(SystemExit) do
|
42
|
+
abort "Aggh! I'm dying!"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
assert_equal "Aggh! I'm dying!", stderr.chomp
|
46
|
+
end
|
47
|
+
|
38
48
|
end
|
39
49
|
=end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Yes
|
6
|
+
# Developer notes::
|
7
|
+
# * Can anyone think of a better name than put_statement?
|
8
|
+
# * Something more like xmp? sp? stp? xm? putst? -- Too cryptic?
|
9
|
+
# * verbose? -- too ambiguous (that could be the name for xmp, for example)
|
10
|
+
# * xmp: Do the set_trace_func trick that irb_xmp/of_caller both use... so that the user doesn't have to pass in the local binding manually...
|
11
|
+
# * Add class method for ExamplePrinter to set a template for use by xmp? So if you don't like the default ("=> #{result}"), you can specify your own...
|
12
|
+
#++
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
#require 'facets/core/binding/self/of_caller'
|
16
|
+
|
17
|
+
# This was written because the irb/xmp that was already available seemed to be
|
18
|
+
# needlessly complex and needlessly dependent upon "irb" stuff. This
|
19
|
+
# alternative is dirt simple, and it still works.
|
20
|
+
module ExamplePrinter
|
21
|
+
# Prints the given statement (+code+ -- a string) before evaluating it.
|
22
|
+
def put_statement(code, binding = nil)
|
23
|
+
# This didn't work. Still got this error: undefined local variable or method `x' for #<TheTest:0xb7dbc358> (NameError)
|
24
|
+
# Binding.of_caller do |caller_binding|
|
25
|
+
# puts caller_binding
|
26
|
+
# puts code
|
27
|
+
# eval code, caller_binding
|
28
|
+
# end
|
29
|
+
puts code
|
30
|
+
eval code, binding
|
31
|
+
end
|
32
|
+
alias_method :stp, :put_statement
|
33
|
+
|
34
|
+
# Pretty much compatible with irb/xmp. But you have currently have to pass
|
35
|
+
# in the binding manually if you have any local variables/methods that xmp
|
36
|
+
# should have access to.
|
37
|
+
def xmp(code, binding = nil)
|
38
|
+
result = put_statement(code, binding)
|
39
|
+
puts "=> #{result}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
include ExamplePrinter
|
44
|
+
|
45
|
+
# _____ _
|
46
|
+
# |_ _|__ ___| |_
|
47
|
+
# | |/ _ \/ __| __|
|
48
|
+
# | | __/\__ \ |_
|
49
|
+
# |_|\___||___/\__|
|
50
|
+
#
|
51
|
+
=begin test
|
52
|
+
require 'test/unit'
|
53
|
+
require 'rubygems'
|
54
|
+
gem 'qualitysmith_extensions'
|
55
|
+
require 'qualitysmith_extensions/kernel/capture_output'
|
56
|
+
require 'facets/core/string/margin'
|
57
|
+
|
58
|
+
class TheTest < Test::Unit::TestCase
|
59
|
+
def test_puts_statement
|
60
|
+
result = nil
|
61
|
+
x = 1
|
62
|
+
output = capture_output { result = put_statement("3 + x", binding) }
|
63
|
+
assert_equal 4, result
|
64
|
+
assert_equal "3 + x", output.chomp
|
65
|
+
end
|
66
|
+
def test_xmp
|
67
|
+
x = 1
|
68
|
+
output = capture_output { xmp("3 + x", binding) }
|
69
|
+
assert_equal <<-End.margin, output.chomp
|
70
|
+
|3 + x
|
71
|
+
|=> 4
|
72
|
+
End
|
73
|
+
end
|
74
|
+
end
|
75
|
+
=end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Copied from ActiveSupport because I couldn't get the ones in Facets to work...
|
2
|
+
|
3
|
+
# Extends the module object with module and instance accessors for class attributes,
|
4
|
+
# just like the native attr* accessors for instance attributes.
|
5
|
+
class Module # :nodoc:
|
6
|
+
def mattr_reader(*syms)
|
7
|
+
syms.each do |sym|
|
8
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
9
|
+
unless defined? @@#{sym}
|
10
|
+
@@#{sym} = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.#{sym}
|
14
|
+
@@#{sym}
|
15
|
+
end
|
16
|
+
|
17
|
+
def #{sym}
|
18
|
+
@@#{sym}
|
19
|
+
end
|
20
|
+
EOS
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def mattr_writer(*syms)
|
25
|
+
syms.each do |sym|
|
26
|
+
class_eval(<<-EOS, __FILE__, __LINE__)
|
27
|
+
unless defined? @@#{sym}
|
28
|
+
@@#{sym} = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.#{sym}=(obj)
|
32
|
+
@@#{sym} = obj
|
33
|
+
end
|
34
|
+
|
35
|
+
def #{sym}=(obj)
|
36
|
+
@@#{sym} = obj
|
37
|
+
end
|
38
|
+
EOS
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def mattr_accessor(*syms)
|
43
|
+
mattr_reader(*syms)
|
44
|
+
mattr_writer(*syms)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# Rationale:
|
2
|
+
# The ||= "operator" is often used to set a default value.
|
3
|
+
# var ||= 'default'
|
4
|
+
# Unfortunately, it does not give the desired behavior for when you want to give a default value to a *boolean* (which might have been already initialized to false).
|
5
|
+
# When setting a variable to a default value, we actually only want to set it to the default if it's currently set to *nil*!
|
6
|
+
# This was an attempt to supply that missing method...
|
7
|
+
|
8
|
+
module Kernel
|
9
|
+
def default!(object, default_value)
|
10
|
+
case object
|
11
|
+
when NilClass
|
12
|
+
#object.become default_value
|
13
|
+
#object.replace default_value
|
14
|
+
else
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
class NilClass
|
22
|
+
def default!(default_value)
|
23
|
+
#self.become default_value
|
24
|
+
#self.replace default_value
|
25
|
+
#self = default_value
|
26
|
+
|
27
|
+
# Not sure how to implemnet this! ... without writing/using a C extension that lets me turn a NilClass object into another object.
|
28
|
+
end
|
29
|
+
end
|
30
|
+
class Object
|
31
|
+
def default!(default_value)
|
32
|
+
# This should have no effect on any objects other than instances of NilClass.
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# _____ _
|
40
|
+
# |_ _|__ ___| |_
|
41
|
+
# | |/ _ \/ __| __|
|
42
|
+
# | | __/\__ \ |_
|
43
|
+
# |_|\___||___/\__|
|
44
|
+
#
|
45
|
+
=begin test
|
46
|
+
require 'test/unit'
|
47
|
+
|
48
|
+
class Foo
|
49
|
+
def do_it(options = {})
|
50
|
+
options[:how_many_times] ||= 3
|
51
|
+
#options[:actually_do_it] ||= true # Doesn't work! What if they explicitly set it to false? We would unconditionally be overriding it with true. We only want to set it to true if it's "not set yet" (if it's nil).
|
52
|
+
options[:actually_do_it].default! true # This is how I'd like to do it. It makes it clear what we're doing (setting a default value).
|
53
|
+
options[:actually_do_it] = true if options[:actually_do_it].nil? # This works, but you have to duplicate the variable name in two places. Plus it's not explicitly clear that true is being used as a *default* value.
|
54
|
+
|
55
|
+
options[:actually_do_it] ? "We did it #{options[:how_many_times]} times" : "We didn't actually do it"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class TheTest < Test::Unit::TestCase
|
60
|
+
def test_false
|
61
|
+
assert_equal "We didn't actually do it", Foo.new.do_it(:actually_do_it => false)
|
62
|
+
end
|
63
|
+
def test_true
|
64
|
+
assert_equal "We did it 3 times", Foo.new.do_it()
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
=end
|
69
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Yes
|
6
|
+
# Developer notes::
|
7
|
+
#++
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'facets/core/module/alias_method_chain'
|
11
|
+
require 'extensions/symbol' # to_proc
|
12
|
+
|
13
|
+
class Object
|
14
|
+
|
15
|
+
# Adds the following features to the built-in Object#methods:
|
16
|
+
# * Provides the same +include_super+ option that Module#instance_methods has (Backwards compatible, because default is +true+)
|
17
|
+
# * Returns an array of symbols rather than strings (Not backwards compatible)
|
18
|
+
# * Sorts the array for you so you don't have to! (Not backwards compatible)
|
19
|
+
def methods_with_sorting_and_include_super(include_super = true)
|
20
|
+
if include_super
|
21
|
+
methods_without_sorting_and_include_super
|
22
|
+
else
|
23
|
+
(methods_without_sorting_and_include_super - Object.methods_without_sorting_and_include_super)
|
24
|
+
end.sort.map(&:to_sym)
|
25
|
+
end
|
26
|
+
alias_method_chain :methods, :sorting_and_include_super
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# _____ _
|
32
|
+
# |_ _|__ ___| |_
|
33
|
+
# | |/ _ \/ __| __|
|
34
|
+
# | | __/\__ \ |_
|
35
|
+
# |_|\___||___/\__|
|
36
|
+
#
|
37
|
+
=begin test
|
38
|
+
require 'test/unit'
|
39
|
+
|
40
|
+
class TheTest < Test::Unit::TestCase
|
41
|
+
def test_1
|
42
|
+
assert_equal [:[]], Array.methods(false)
|
43
|
+
assert Array.methods.size > Array.methods(false).size
|
44
|
+
|
45
|
+
assert !Object.methods(true).include?(:[])
|
46
|
+
assert Array. methods(true).include?(:[])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
=end
|
50
|
+
|
51
|
+
# Old idea:
|
52
|
+
#
|
53
|
+
# class Object
|
54
|
+
# def own_methods # (or Object#my_methods)
|
55
|
+
# ((methods - Object.methods).sort)
|
56
|
+
# # Could just use self.class.instance_methods(false) but what if we also want class/module methods to be included?
|
57
|
+
# end
|
58
|
+
# end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: qualitysmith_extensions
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-03
|
6
|
+
version: 0.0.7
|
7
|
+
date: 2007-04-03 00:00:00 -07:00
|
8
8
|
summary: A collection of reusable Ruby methods developed by QualitySmith.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
|
+
post_install_message:
|
28
29
|
authors:
|
29
30
|
- Tyler Rick and others
|
30
31
|
files:
|
@@ -42,12 +43,14 @@ files:
|
|
42
43
|
- lib/qualitysmith_extensions/kernel/simulate_input.rb
|
43
44
|
- lib/qualitysmith_extensions/kernel/die.rb
|
44
45
|
- lib/qualitysmith_extensions/kernel/require_once.rb
|
46
|
+
- lib/qualitysmith_extensions/kernel/example_printer.rb
|
45
47
|
- lib/qualitysmith_extensions/kernel/all.rb
|
46
48
|
- lib/qualitysmith_extensions/kernel/filter_output.rb
|
47
49
|
- lib/qualitysmith_extensions/kernel/backtrace.rb
|
48
50
|
- lib/qualitysmith_extensions/kernel/capture_output.rb
|
49
51
|
- lib/qualitysmith_extensions/object/singleton_send.rb
|
50
|
-
- lib/qualitysmith_extensions/object/
|
52
|
+
- lib/qualitysmith_extensions/object/methods.rb
|
53
|
+
- lib/qualitysmith_extensions/object/default.rb
|
51
54
|
- lib/qualitysmith_extensions/file/exact_match_regexp.rb
|
52
55
|
- lib/qualitysmith_extensions/date/iso8601.rb
|
53
56
|
- lib/qualitysmith_extensions/date/deprecated.rb
|
@@ -58,7 +61,10 @@ files:
|
|
58
61
|
- lib/qualitysmith_extensions/console/command.rb
|
59
62
|
- lib/qualitysmith_extensions/console/command.facets.1.8.54.rb
|
60
63
|
- lib/qualitysmith_extensions/console/command.facets.1.8.51.rb
|
64
|
+
- lib/qualitysmith_extensions/module/alias_method.rb
|
65
|
+
- lib/qualitysmith_extensions/module/includable_once.rb
|
61
66
|
- lib/qualitysmith_extensions/module/create_setter.rb
|
67
|
+
- lib/qualitysmith_extensions/module/attribute_accessors.rb
|
62
68
|
- lib/qualitysmith_extensions/enumerable/enum.rb
|
63
69
|
- lib/qualitysmith_extensions/hash/to_query_string.rb
|
64
70
|
- lib/qualitysmith_extensions/hash/all.rb
|