csd 0.0.15 → 0.0.16
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.
- data/.gitignore +1 -0
- data/COPYING +367 -0
- data/Rakefile +10 -10
- data/VERSION +1 -1
- data/bin/ai +19 -0
- data/csd.gemspec +257 -35
- data/lib/active_support.rb +75 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/backtrace_cleaner.rb +94 -0
- data/lib/active_support/base64.rb +42 -0
- data/lib/active_support/basic_object.rb +21 -0
- data/lib/active_support/benchmarkable.rb +60 -0
- data/lib/active_support/buffered_logger.rb +132 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +626 -0
- data/lib/active_support/cache/compressed_mem_cache_store.rb +13 -0
- data/lib/active_support/cache/file_store.rb +188 -0
- data/lib/active_support/cache/mem_cache_store.rb +191 -0
- data/lib/active_support/cache/memory_store.rb +159 -0
- data/lib/active_support/cache/strategy/local_cache.rb +164 -0
- data/lib/active_support/cache/synchronized_memory_store.rb +11 -0
- data/lib/active_support/callbacks.rb +600 -0
- data/lib/active_support/concern.rb +29 -0
- data/lib/active_support/configurable.rb +36 -0
- data/lib/active_support/core_ext.rb +3 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +46 -0
- data/lib/active_support/core_ext/array/conversions.rb +164 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +100 -0
- data/lib/active_support/core_ext/array/random_access.rb +20 -0
- data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +22 -0
- data/lib/active_support/core_ext/benchmark.rb +7 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +27 -0
- data/lib/active_support/core_ext/cgi.rb +1 -0
- data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +19 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/class/attribute.rb +67 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +63 -0
- data/lib/active_support/core_ext/class/delegating_attributes.rb +44 -0
- data/lib/active_support/core_ext/class/inheritable_attributes.rb +232 -0
- data/lib/active_support/core_ext/class/subclasses.rb +55 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/calculations.rb +240 -0
- data/lib/active_support/core_ext/date/conversions.rb +99 -0
- data/lib/active_support/core_ext/date/freeze.rb +31 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +13 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +113 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +102 -0
- data/lib/active_support/core_ext/date_time/zones.rb +17 -0
- data/lib/active_support/core_ext/enumerable.rb +119 -0
- data/lib/active_support/core_ext/exception.rb +3 -0
- data/lib/active_support/core_ext/file.rb +2 -0
- data/lib/active_support/core_ext/file/atomic.rb +41 -0
- data/lib/active_support/core_ext/file/path.rb +5 -0
- data/lib/active_support/core_ext/float.rb +1 -0
- data/lib/active_support/core_ext/float/rounding.rb +19 -0
- data/lib/active_support/core_ext/hash.rb +8 -0
- data/lib/active_support/core_ext/hash/conversions.rb +150 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +16 -0
- data/lib/active_support/core_ext/hash/diff.rb +13 -0
- data/lib/active_support/core_ext/hash/except.rb +24 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +14 -0
- data/lib/active_support/core_ext/hash/keys.rb +45 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +28 -0
- data/lib/active_support/core_ext/hash/slice.rb +38 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +14 -0
- data/lib/active_support/core_ext/integer/multiple.rb +6 -0
- data/lib/active_support/core_ext/integer/time.rb +39 -0
- data/lib/active_support/core_ext/kernel.rb +5 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +16 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +62 -0
- data/lib/active_support/core_ext/kernel/requires.rb +26 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
- data/lib/active_support/core_ext/load_error.rb +23 -0
- data/lib/active_support/core_ext/logger.rb +146 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +70 -0
- data/lib/active_support/core_ext/module/anonymous.rb +24 -0
- data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +32 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +66 -0
- data/lib/active_support/core_ext/module/delegation.rb +146 -0
- data/lib/active_support/core_ext/module/deprecation.rb +9 -0
- data/lib/active_support/core_ext/module/introspection.rb +88 -0
- data/lib/active_support/core_ext/module/method_names.rb +14 -0
- data/lib/active_support/core_ext/module/reachable.rb +10 -0
- data/lib/active_support/core_ext/module/remove_method.rb +6 -0
- data/lib/active_support/core_ext/module/synchronization.rb +42 -0
- data/lib/active_support/core_ext/name_error.rb +18 -0
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +44 -0
- data/lib/active_support/core_ext/numeric/time.rb +77 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +76 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/duplicable.rb +65 -0
- data/lib/active_support/core_ext/object/extending.rb +11 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +67 -0
- data/lib/active_support/core_ext/object/misc.rb +2 -0
- data/lib/active_support/core_ext/object/returning.rb +42 -0
- data/lib/active_support/core_ext/object/to_param.rb +49 -0
- data/lib/active_support/core_ext/object/to_query.rb +27 -0
- data/lib/active_support/core_ext/object/try.rb +36 -0
- data/lib/active_support/core_ext/object/with_options.rb +26 -0
- data/lib/active_support/core_ext/proc.rb +14 -0
- data/lib/active_support/core_ext/process.rb +1 -0
- data/lib/active_support/core_ext/process/daemon.rb +23 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/blockless_step.rb +29 -0
- data/lib/active_support/core_ext/range/conversions.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +21 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/rexml.rb +46 -0
- data/lib/active_support/core_ext/string.rb +12 -0
- data/lib/active_support/core_ext/string/access.rb +99 -0
- data/lib/active_support/core_ext/string/behavior.rb +7 -0
- data/lib/active_support/core_ext/string/conversions.rb +61 -0
- data/lib/active_support/core_ext/string/encoding.rb +11 -0
- data/lib/active_support/core_ext/string/exclude.rb +6 -0
- data/lib/active_support/core_ext/string/filters.rb +49 -0
- data/lib/active_support/core_ext/string/inflections.rb +149 -0
- data/lib/active_support/core_ext/string/interpolation.rb +2 -0
- data/lib/active_support/core_ext/string/multibyte.rb +72 -0
- data/lib/active_support/core_ext/string/output_safety.rb +109 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/xchar.rb +18 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +282 -0
- data/lib/active_support/core_ext/time/conversions.rb +85 -0
- data/lib/active_support/core_ext/time/marshal.rb +56 -0
- data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +10 -0
- data/lib/active_support/core_ext/time/zones.rb +78 -0
- data/lib/active_support/core_ext/uri.rb +22 -0
- data/lib/active_support/dependencies.rb +628 -0
- data/lib/active_support/dependencies/autoload.rb +50 -0
- data/lib/active_support/deprecation.rb +18 -0
- data/lib/active_support/deprecation/behaviors.rb +38 -0
- data/lib/active_support/deprecation/method_wrappers.rb +29 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +74 -0
- data/lib/active_support/deprecation/reporting.rb +56 -0
- data/lib/active_support/duration.rb +105 -0
- data/lib/active_support/gzip.rb +25 -0
- data/lib/active_support/hash_with_indifferent_access.rb +145 -0
- data/lib/active_support/i18n.rb +8 -0
- data/lib/active_support/inflections.rb +56 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +211 -0
- data/lib/active_support/inflector/methods.rb +141 -0
- data/lib/active_support/inflector/transliterate.rb +97 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/backends/jsongem.rb +43 -0
- data/lib/active_support/json/backends/yajl.rb +40 -0
- data/lib/active_support/json/backends/yaml.rb +90 -0
- data/lib/active_support/json/decoding.rb +51 -0
- data/lib/active_support/json/encoding.rb +254 -0
- data/lib/active_support/json/variable.rb +11 -0
- data/lib/active_support/lazy_load_hooks.rb +27 -0
- data/lib/active_support/locale/en.yml +36 -0
- data/lib/active_support/memoizable.rb +103 -0
- data/lib/active_support/message_encryptor.rb +71 -0
- data/lib/active_support/message_verifier.rb +62 -0
- data/lib/active_support/multibyte.rb +44 -0
- data/lib/active_support/multibyte/chars.rb +480 -0
- data/lib/active_support/multibyte/exceptions.rb +8 -0
- data/lib/active_support/multibyte/unicode.rb +393 -0
- data/lib/active_support/multibyte/utils.rb +60 -0
- data/lib/active_support/notifications.rb +81 -0
- data/lib/active_support/notifications/fanout.rb +93 -0
- data/lib/active_support/notifications/instrumenter.rb +56 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +158 -0
- data/lib/active_support/ordered_options.rb +27 -0
- data/lib/active_support/railtie.rb +100 -0
- data/lib/active_support/rescuable.rb +114 -0
- data/lib/active_support/ruby/shim.rb +22 -0
- data/lib/active_support/secure_random.rb +199 -0
- data/lib/active_support/string_inquirer.rb +21 -0
- data/lib/active_support/test_case.rb +42 -0
- data/lib/active_support/testing/assertions.rb +82 -0
- data/lib/active_support/testing/declarative.rb +40 -0
- data/lib/active_support/testing/default.rb +9 -0
- data/lib/active_support/testing/deprecation.rb +55 -0
- data/lib/active_support/testing/isolation.rb +154 -0
- data/lib/active_support/testing/pending.rb +48 -0
- data/lib/active_support/testing/performance.rb +455 -0
- data/lib/active_support/testing/setup_and_teardown.rb +111 -0
- data/lib/active_support/time.rb +34 -0
- data/lib/active_support/time/autoload.rb +5 -0
- data/lib/active_support/time_with_zone.rb +341 -0
- data/lib/active_support/values/time_zone.rb +377 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/whiny_nil.rb +60 -0
- data/lib/active_support/xml_mini.rb +158 -0
- data/lib/active_support/xml_mini/jdom.rb +168 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
- data/lib/active_support/xml_mini/nokogiri.rb +78 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
- data/lib/active_support/xml_mini/rexml.rb +129 -0
- data/lib/csd.rb +82 -2
- data/lib/csd/application.rb +2 -0
- data/lib/csd/application/default.rb +51 -0
- data/lib/csd/application/default/base.rb +15 -0
- data/lib/csd/application/minisip.rb +25 -0
- data/lib/csd/application/minisip/about.yml +14 -0
- data/lib/csd/application/minisip/base.rb +161 -0
- data/lib/csd/application/minisip/error.rb +11 -0
- data/lib/csd/application/minisip/options/common.rb +0 -0
- data/lib/csd/application/minisip/options/compile.rb +59 -0
- data/lib/csd/{applications → application}/minisip/unix/base.rb +10 -11
- data/lib/csd/application/opensips/about.yml +2 -0
- data/lib/csd/applications.rb +55 -0
- data/lib/csd/commands.rb +88 -65
- data/lib/csd/error.rb +31 -0
- data/lib/csd/extensions.rb +1 -0
- data/lib/{extensions → csd/extensions}/core/array.rb +2 -2
- data/lib/csd/extensions/core/dir.rb +46 -0
- data/lib/{extensions → csd/extensions}/core/file.rb +2 -2
- data/lib/{extensions → csd/extensions}/core/object.rb +2 -2
- data/lib/csd/extensions/core/option_parser.rb +33 -0
- data/lib/{extensions → csd/extensions}/core/pathname.rb +12 -3
- data/lib/{extensions → csd/extensions}/core/string.rb +2 -2
- data/lib/{extensions → csd/extensions}/gem/platform.rb +6 -2
- data/lib/csd/global_open_struct.rb +18 -0
- data/lib/csd/options.rb +124 -95
- data/lib/csd/path.rb +31 -0
- data/lib/csd/ui.rb +1 -0
- data/lib/csd/ui/cli.rb +7 -0
- data/lib/csd/ui/ui.rb +46 -0
- data/lib/csd/version.rb +9 -0
- data/lib/term/ansicolor.rb +102 -0
- data/lib/term/ansicolor/.keep +0 -0
- data/lib/term/ansicolor/version.rb +10 -0
- data/test/functional/test_applications.rb +86 -0
- data/test/functional/test_commands.rb +42 -29
- data/test/functional/test_options.rb +98 -0
- data/test/helper.rb +14 -0
- data/test/unit/test_dir.rb +38 -0
- data/test/unit/test_pathname.rb +32 -0
- metadata +253 -40
- data/LICENSE +0 -20
- data/bin/csd +0 -8
- data/lib/csd/applications/base.rb +0 -33
- data/lib/csd/applications/minisip/base.rb +0 -125
- data/lib/csd/applications/minisip/init.rb +0 -20
- data/lib/csd/init.rb +0 -69
- data/lib/csd/path_container.rb +0 -15
- data/publish +0 -29
@@ -0,0 +1,16 @@
|
|
1
|
+
class Hash
|
2
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
3
|
+
def deep_merge(other_hash)
|
4
|
+
dup.deep_merge!(other_hash)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
8
|
+
# Modifies the receiver in place.
|
9
|
+
def deep_merge!(other_hash)
|
10
|
+
other_hash.each_pair do |k,v|
|
11
|
+
tv = self[k]
|
12
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
13
|
+
end
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Hash
|
2
|
+
# Returns a hash that represents the difference between two hashes.
|
3
|
+
#
|
4
|
+
# Examples:
|
5
|
+
#
|
6
|
+
# {1 => 2}.diff(1 => 2) # => {}
|
7
|
+
# {1 => 2}.diff(1 => 3) # => {1 => 2}
|
8
|
+
# {}.diff(1 => 2) # => {1 => 2}
|
9
|
+
# {1 => 2, 3 => 4}.diff(1 => 2) # => {3 => 4}
|
10
|
+
def diff(h2)
|
11
|
+
dup.delete_if { |k, v| h2[k] == v }.merge!(h2.dup.delete_if { |k, v| has_key?(k) })
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Hash
|
2
|
+
# Return a hash that includes everything but the given keys. This is useful for
|
3
|
+
# limiting a set of parameters to everything but a few known toggles:
|
4
|
+
#
|
5
|
+
# @person.update_attributes(params[:person].except(:admin))
|
6
|
+
#
|
7
|
+
# If the receiver responds to +convert_key+, the method is called on each of the
|
8
|
+
# arguments. This allows +except+ to play nice with hashes with indifferent access
|
9
|
+
# for instance:
|
10
|
+
#
|
11
|
+
# {:a => 1}.with_indifferent_access.except(:a) # => {}
|
12
|
+
# {:a => 1}.with_indifferent_access.except("a") # => {}
|
13
|
+
#
|
14
|
+
def except(*keys)
|
15
|
+
dup.except!(*keys)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Replaces the hash without the given keys.
|
19
|
+
def except!(*keys)
|
20
|
+
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
21
|
+
keys.each { |key| delete(key) }
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_support/hash_with_indifferent_access'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
|
5
|
+
# Returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver:
|
6
|
+
#
|
7
|
+
# {:a => 1}.with_indifferent_access["a"] # => 1
|
8
|
+
#
|
9
|
+
def with_indifferent_access
|
10
|
+
hash = ActiveSupport::HashWithIndifferentAccess.new(self)
|
11
|
+
hash.default = self.default
|
12
|
+
hash
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class Hash
|
2
|
+
# Return a new hash with all keys converted to strings.
|
3
|
+
def stringify_keys
|
4
|
+
dup.stringify_keys!
|
5
|
+
end
|
6
|
+
|
7
|
+
# Destructively convert all keys to strings.
|
8
|
+
def stringify_keys!
|
9
|
+
keys.each do |key|
|
10
|
+
self[key.to_s] = delete(key)
|
11
|
+
end
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
# Return a new hash with all keys converted to symbols, as long as
|
16
|
+
# they respond to +to_sym+.
|
17
|
+
def symbolize_keys
|
18
|
+
dup.symbolize_keys!
|
19
|
+
end
|
20
|
+
|
21
|
+
# Destructively convert all keys to symbols, as long as they respond
|
22
|
+
# to +to_sym+.
|
23
|
+
def symbolize_keys!
|
24
|
+
keys.each do |key|
|
25
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
26
|
+
end
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :to_options, :symbolize_keys
|
31
|
+
alias_method :to_options!, :symbolize_keys!
|
32
|
+
|
33
|
+
# Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
|
34
|
+
# Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
|
35
|
+
# as keys, this will fail.
|
36
|
+
#
|
37
|
+
# ==== Examples
|
38
|
+
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
|
39
|
+
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
|
40
|
+
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
|
41
|
+
def assert_valid_keys(*valid_keys)
|
42
|
+
unknown_keys = keys - [valid_keys].flatten
|
43
|
+
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Hash
|
2
|
+
# Allows for reverse merging two hashes where the keys in the calling hash take precedence over those
|
3
|
+
# in the <tt>other_hash</tt>. This is particularly useful for initializing an option hash with default values:
|
4
|
+
#
|
5
|
+
# def setup(options = {})
|
6
|
+
# options.reverse_merge! :size => 25, :velocity => 10
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# Using <tt>merge</tt>, the above example would look as follows:
|
10
|
+
#
|
11
|
+
# def setup(options = {})
|
12
|
+
# { :size => 25, :velocity => 10 }.merge(options)
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# The default <tt>:size</tt> and <tt>:velocity</tt> are only set if the +options+ hash passed in doesn't already
|
16
|
+
# have the respective key.
|
17
|
+
def reverse_merge(other_hash)
|
18
|
+
other_hash.merge(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second.
|
22
|
+
# Modifies the receiver in place.
|
23
|
+
def reverse_merge!(other_hash)
|
24
|
+
merge!( other_hash ){|k,o,n| o }
|
25
|
+
end
|
26
|
+
|
27
|
+
alias_method :reverse_update, :reverse_merge!
|
28
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Hash
|
2
|
+
# Slice a hash to include only the given keys. This is useful for
|
3
|
+
# limiting an options hash to valid keys before passing to a method:
|
4
|
+
#
|
5
|
+
# def search(criteria = {})
|
6
|
+
# assert_valid_keys(:mass, :velocity, :time)
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# search(options.slice(:mass, :velocity, :time))
|
10
|
+
#
|
11
|
+
# If you have an array of keys you want to limit to, you should splat them:
|
12
|
+
#
|
13
|
+
# valid_keys = [:mass, :velocity, :time]
|
14
|
+
# search(options.slice(*valid_keys))
|
15
|
+
def slice(*keys)
|
16
|
+
keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
17
|
+
hash = self.class.new
|
18
|
+
keys.each { |k| hash[k] = self[k] if has_key?(k) }
|
19
|
+
hash
|
20
|
+
end
|
21
|
+
|
22
|
+
# Replaces the hash with only the given keys.
|
23
|
+
# Returns a hash contained the removed key/value pairs
|
24
|
+
# {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d =>4}
|
25
|
+
def slice!(*keys)
|
26
|
+
keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
27
|
+
omit = slice(*self.keys - keys)
|
28
|
+
hash = slice(*keys)
|
29
|
+
replace(hash)
|
30
|
+
omit
|
31
|
+
end
|
32
|
+
|
33
|
+
def extract!(*keys)
|
34
|
+
result = {}
|
35
|
+
keys.each {|key| result[key] = delete(key) }
|
36
|
+
result
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
class Integer
|
4
|
+
# Ordinalize turns a number into an ordinal string used to denote the
|
5
|
+
# position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
|
6
|
+
#
|
7
|
+
# 1.ordinalize # => "1st"
|
8
|
+
# 2.ordinalize # => "2nd"
|
9
|
+
# 1002.ordinalize # => "1002nd"
|
10
|
+
# 1003.ordinalize # => "1003rd"
|
11
|
+
def ordinalize
|
12
|
+
ActiveSupport::Inflector.ordinalize(self)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Integer
|
2
|
+
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
3
|
+
#
|
4
|
+
# These methods use Time#advance for precise date calculations when using from_now, ago, etc.
|
5
|
+
# as well as adding or subtracting their results from a Time object. For example:
|
6
|
+
#
|
7
|
+
# # equivalent to Time.now.advance(:months => 1)
|
8
|
+
# 1.month.from_now
|
9
|
+
#
|
10
|
+
# # equivalent to Time.now.advance(:years => 2)
|
11
|
+
# 2.years.from_now
|
12
|
+
#
|
13
|
+
# # equivalent to Time.now.advance(:months => 4, :years => 5)
|
14
|
+
# (4.months + 5.years).from_now
|
15
|
+
#
|
16
|
+
# While these methods provide precise calculation when used as in the examples above, care
|
17
|
+
# should be taken to note that this is not true if the result of `months', `years', etc is
|
18
|
+
# converted before use:
|
19
|
+
#
|
20
|
+
# # equivalent to 30.days.to_i.from_now
|
21
|
+
# 1.month.to_i.from_now
|
22
|
+
#
|
23
|
+
# # equivalent to 365.25.days.to_f.from_now
|
24
|
+
# 1.year.to_f.from_now
|
25
|
+
#
|
26
|
+
# In such cases, Ruby's core
|
27
|
+
# Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
|
28
|
+
# Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
|
29
|
+
# date and time arithmetic
|
30
|
+
def months
|
31
|
+
ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
|
32
|
+
end
|
33
|
+
alias :month :months
|
34
|
+
|
35
|
+
def years
|
36
|
+
ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]])
|
37
|
+
end
|
38
|
+
alias :year :years
|
39
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Object
|
2
|
+
# Makes backticks behave (somewhat more) similarly on all platforms.
|
3
|
+
# On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the
|
4
|
+
# spawned shell prints a message to stderr and sets $?. We emulate
|
5
|
+
# Unix on the former but not the latter.
|
6
|
+
def `(command) #:nodoc:
|
7
|
+
super
|
8
|
+
rescue Errno::ENOENT => e
|
9
|
+
STDERR.puts "#$0: #{e}"
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Kernel
|
2
|
+
unless respond_to?(:debugger)
|
3
|
+
# Starts a debugging session if ruby-debug has been loaded (call rails server --debugger to do load it).
|
4
|
+
def debugger
|
5
|
+
message = "\n***** Debugger requested, but was not available (ensure ruby-debug is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n"
|
6
|
+
defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
undef :breakpoint if respond_to?(:breakpoint)
|
11
|
+
def breakpoint
|
12
|
+
message = "\n***** The 'breakpoint' command has been renamed 'debugger' -- please change *****\n"
|
13
|
+
defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message)
|
14
|
+
debugger
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
module Kernel
|
3
|
+
# Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
|
4
|
+
#
|
5
|
+
# silence_warnings do
|
6
|
+
# value = noisy_call # no warning voiced
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# noisy_call # warning voiced
|
10
|
+
def silence_warnings
|
11
|
+
with_warnings(nil) { yield }
|
12
|
+
end
|
13
|
+
|
14
|
+
# Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
|
15
|
+
def enable_warnings
|
16
|
+
with_warnings(true) { yield }
|
17
|
+
end
|
18
|
+
|
19
|
+
# Sets $VERBOSE for the duration of the block and back to its original value afterwards.
|
20
|
+
def with_warnings(flag)
|
21
|
+
old_verbose, $VERBOSE = $VERBOSE, flag
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
$VERBOSE = old_verbose
|
25
|
+
end
|
26
|
+
|
27
|
+
# For compatibility
|
28
|
+
def silence_stderr #:nodoc:
|
29
|
+
silence_stream(STDERR) { yield }
|
30
|
+
end
|
31
|
+
|
32
|
+
# Silences any stream for the duration of the block.
|
33
|
+
#
|
34
|
+
# silence_stream(STDOUT) do
|
35
|
+
# puts 'This will never be seen'
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# puts 'But this will'
|
39
|
+
def silence_stream(stream)
|
40
|
+
old_stream = stream.dup
|
41
|
+
stream.reopen(Config::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
|
42
|
+
stream.sync = true
|
43
|
+
yield
|
44
|
+
ensure
|
45
|
+
stream.reopen(old_stream)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Blocks and ignores any exception passed as argument if raised within the block.
|
49
|
+
#
|
50
|
+
# suppress(ZeroDivisionError) do
|
51
|
+
# 1/0
|
52
|
+
# puts "This code is NOT reached"
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# puts "This code gets executed and nothing related to ZeroDivisionError was seen"
|
56
|
+
def suppress(*exception_classes)
|
57
|
+
begin yield
|
58
|
+
rescue Exception => e
|
59
|
+
raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_support/core_ext/kernel/reporting'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
# Require a library with fallback to RubyGems. Warnings during library
|
5
|
+
# loading are silenced to increase signal/noise for application warnings.
|
6
|
+
def require_library_or_gem(library_name)
|
7
|
+
silence_warnings do
|
8
|
+
begin
|
9
|
+
require library_name
|
10
|
+
rescue LoadError => cannot_require
|
11
|
+
# 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try.
|
12
|
+
begin
|
13
|
+
require 'rubygems'
|
14
|
+
rescue LoadError => rubygems_not_installed
|
15
|
+
raise cannot_require
|
16
|
+
end
|
17
|
+
# 2. Rubygems is installed and loaded. Try to load the library again
|
18
|
+
begin
|
19
|
+
require library_name
|
20
|
+
rescue LoadError => gem_not_installed
|
21
|
+
raise cannot_require
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Kernel
|
2
|
+
# Returns the object's singleton class.
|
3
|
+
def singleton_class
|
4
|
+
class << self
|
5
|
+
self
|
6
|
+
end
|
7
|
+
end unless respond_to?(:singleton_class) # exists in 1.9.2
|
8
|
+
|
9
|
+
# class_eval on an object acts like singleton_class.class_eval.
|
10
|
+
def class_eval(*args, &block)
|
11
|
+
singleton_class.class_eval(*args, &block)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class LoadError
|
2
|
+
REGEXPS = [
|
3
|
+
/^no such file to load -- (.+)$/i,
|
4
|
+
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
|
5
|
+
/^Missing API definition file in (.+)$/i,
|
6
|
+
/^cannot load such file -- (.+)$/i,
|
7
|
+
]
|
8
|
+
|
9
|
+
def path
|
10
|
+
@path ||= begin
|
11
|
+
REGEXPS.find do |regex|
|
12
|
+
message =~ regex
|
13
|
+
end
|
14
|
+
$1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def is_missing?(location)
|
19
|
+
location.sub(/\.rb$/, '') == path.sub(/\.rb$/, '')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
MissingSourceFile = LoadError
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
2
|
+
|
3
|
+
# Adds the 'around_level' method to Logger.
|
4
|
+
class Logger #:nodoc:
|
5
|
+
def self.define_around_helper(level)
|
6
|
+
module_eval <<-end_eval, __FILE__, __LINE__ + 1
|
7
|
+
def around_#{level}(before_message, after_message, &block) # def around_debug(before_message, after_message, &block)
|
8
|
+
self.#{level}(before_message) # self.debug(before_message)
|
9
|
+
return_value = block.call(self) # return_value = block.call(self)
|
10
|
+
self.#{level}(after_message) # self.debug(after_message)
|
11
|
+
return return_value # return return_value
|
12
|
+
end # end
|
13
|
+
end_eval
|
14
|
+
end
|
15
|
+
[:debug, :info, :error, :fatal].each {|level| define_around_helper(level) }
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
require 'logger'
|
20
|
+
|
21
|
+
# Extensions to the built in Ruby logger.
|
22
|
+
#
|
23
|
+
# If you want to use the default log formatter as defined in the Ruby core, then you
|
24
|
+
# will need to set the formatter for the logger as in:
|
25
|
+
#
|
26
|
+
# logger.formatter = Formatter.new
|
27
|
+
#
|
28
|
+
# You can then specify the datetime format, for example:
|
29
|
+
#
|
30
|
+
# logger.datetime_format = "%Y-%m-%d"
|
31
|
+
#
|
32
|
+
# Note: This logger is deprecated in favor of ActiveSupport::BufferedLogger
|
33
|
+
class Logger
|
34
|
+
##
|
35
|
+
# :singleton-method:
|
36
|
+
# Set to false to disable the silencer
|
37
|
+
cattr_accessor :silencer
|
38
|
+
self.silencer = true
|
39
|
+
|
40
|
+
# Silences the logger for the duration of the block.
|
41
|
+
def silence(temporary_level = Logger::ERROR)
|
42
|
+
if silencer
|
43
|
+
begin
|
44
|
+
old_logger_level, self.level = level, temporary_level
|
45
|
+
yield self
|
46
|
+
ensure
|
47
|
+
self.level = old_logger_level
|
48
|
+
end
|
49
|
+
else
|
50
|
+
yield self
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
alias :old_datetime_format= :datetime_format=
|
55
|
+
# Logging date-time format (string passed to +strftime+). Ignored if the formatter
|
56
|
+
# does not respond to datetime_format=.
|
57
|
+
def datetime_format=(datetime_format)
|
58
|
+
formatter.datetime_format = datetime_format if formatter.respond_to?(:datetime_format=)
|
59
|
+
end
|
60
|
+
|
61
|
+
alias :old_datetime_format :datetime_format
|
62
|
+
# Get the logging datetime format. Returns nil if the formatter does not support
|
63
|
+
# datetime formatting.
|
64
|
+
def datetime_format
|
65
|
+
formatter.datetime_format if formatter.respond_to?(:datetime_format)
|
66
|
+
end
|
67
|
+
|
68
|
+
alias :old_formatter :formatter if method_defined?(:formatter)
|
69
|
+
# Get the current formatter. The default formatter is a SimpleFormatter which only
|
70
|
+
# displays the log message
|
71
|
+
def formatter
|
72
|
+
@formatter ||= SimpleFormatter.new
|
73
|
+
end
|
74
|
+
|
75
|
+
unless const_defined? :Formatter
|
76
|
+
class Formatter
|
77
|
+
Format = "%s, [%s#%d] %5s -- %s: %s\n"
|
78
|
+
|
79
|
+
attr_accessor :datetime_format
|
80
|
+
|
81
|
+
def initialize
|
82
|
+
@datetime_format = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
def call(severity, time, progname, msg)
|
86
|
+
Format % [severity[0..0], format_datetime(time), $$, severity, progname,
|
87
|
+
msg2str(msg)]
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
def format_datetime(time)
|
92
|
+
if @datetime_format.nil?
|
93
|
+
time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % time.usec
|
94
|
+
else
|
95
|
+
time.strftime(@datetime_format)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def msg2str(msg)
|
100
|
+
case msg
|
101
|
+
when ::String
|
102
|
+
msg
|
103
|
+
when ::Exception
|
104
|
+
"#{ msg.message } (#{ msg.class })\n" <<
|
105
|
+
(msg.backtrace || []).join("\n")
|
106
|
+
else
|
107
|
+
msg.inspect
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Simple formatter which only displays the message.
|
114
|
+
class SimpleFormatter < Logger::Formatter
|
115
|
+
# This method is invoked when a log event occurs
|
116
|
+
def call(severity, timestamp, progname, msg)
|
117
|
+
"#{String === msg ? msg : msg.inspect}\n"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
alias old_format_message format_message
|
123
|
+
|
124
|
+
# Ruby 1.8.3 transposed the msg and progname arguments to format_message.
|
125
|
+
# We can't test RUBY_VERSION because some distributions don't keep Ruby
|
126
|
+
# and its standard library in sync, leading to installations of Ruby 1.8.2
|
127
|
+
# with Logger from 1.8.3 and vice versa.
|
128
|
+
if method_defined?(:formatter=)
|
129
|
+
def format_message(severity, timestamp, progname, msg)
|
130
|
+
formatter.call(severity, timestamp, progname, msg)
|
131
|
+
end
|
132
|
+
else
|
133
|
+
def format_message(severity, timestamp, msg, progname)
|
134
|
+
formatter.call(severity, timestamp, progname, msg)
|
135
|
+
end
|
136
|
+
|
137
|
+
attr_writer :formatter
|
138
|
+
public :formatter=
|
139
|
+
|
140
|
+
alias old_format_datetime format_datetime
|
141
|
+
def format_datetime(datetime) datetime end
|
142
|
+
|
143
|
+
alias old_msg2str msg2str
|
144
|
+
def msg2str(msg) msg end
|
145
|
+
end
|
146
|
+
end
|