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,61 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/core_ext/time/publicize_conversion_methods'
|
3
|
+
require 'active_support/core_ext/time/calculations'
|
4
|
+
|
5
|
+
class String
|
6
|
+
# Returns the codepoint of the first character of the string, assuming a
|
7
|
+
# single-byte character encoding:
|
8
|
+
#
|
9
|
+
# "a".ord # => 97
|
10
|
+
# "à".ord # => 224, in ISO-8859-1
|
11
|
+
#
|
12
|
+
# This method is defined in Ruby 1.8 for Ruby 1.9 forward compatibility on
|
13
|
+
# these character encodings.
|
14
|
+
#
|
15
|
+
# <tt>ActiveSupport::Multibyte::Chars#ord</tt> is forward compatible with
|
16
|
+
# Ruby 1.9 on UTF8 strings:
|
17
|
+
#
|
18
|
+
# "a".mb_chars.ord # => 97
|
19
|
+
# "à".mb_chars.ord # => 224, in UTF8
|
20
|
+
#
|
21
|
+
# Note that the 224 is different in both examples. In ISO-8859-1 "à" is
|
22
|
+
# represented as a single byte, 224. In UTF8 it is represented with two
|
23
|
+
# bytes, namely 195 and 160, but its Unicode codepoint is 224. If we
|
24
|
+
# call +ord+ on the UTF8 string "à" the return value will be 195. That is
|
25
|
+
# not an error, because UTF8 is unsupported, the call itself would be
|
26
|
+
# bogus.
|
27
|
+
def ord
|
28
|
+
self[0]
|
29
|
+
end unless method_defined?(:ord)
|
30
|
+
|
31
|
+
# Form can be either :utc (default) or :local.
|
32
|
+
def to_time(form = :utc)
|
33
|
+
return nil if self.blank?
|
34
|
+
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction).map { |arg| arg || 0 }
|
35
|
+
d[6] *= 1000000
|
36
|
+
::Time.send("#{form}_time", *d)
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_date
|
40
|
+
return nil if self.blank?
|
41
|
+
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_datetime
|
45
|
+
return nil if self.blank?
|
46
|
+
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
|
47
|
+
d[5] += d.pop
|
48
|
+
::DateTime.civil(*d)
|
49
|
+
end
|
50
|
+
|
51
|
+
# +constantize+ tries to find a declared constant with the name specified
|
52
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
53
|
+
# or is not initialized.
|
54
|
+
#
|
55
|
+
# Examples
|
56
|
+
# "Module".constantize # => Module
|
57
|
+
# "Class".constantize # => Class
|
58
|
+
def constantize
|
59
|
+
ActiveSupport::Inflector.constantize(self)
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'active_support/core_ext/string/multibyte'
|
2
|
+
|
3
|
+
class String
|
4
|
+
# Returns the string, first removing all whitespace on both ends of
|
5
|
+
# the string, and then changing remaining consecutive whitespace
|
6
|
+
# groups into one space each.
|
7
|
+
#
|
8
|
+
# Examples:
|
9
|
+
# %{ Multi-line
|
10
|
+
# string }.squish # => "Multi-line string"
|
11
|
+
# " foo bar \n \t boo".squish # => "foo bar boo"
|
12
|
+
def squish
|
13
|
+
dup.squish!
|
14
|
+
end
|
15
|
+
|
16
|
+
# Performs a destructive squish. See String#squish.
|
17
|
+
def squish!
|
18
|
+
strip!
|
19
|
+
gsub!(/\s+/, ' ')
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
|
24
|
+
#
|
25
|
+
# "Once upon a time in a world far far away".truncate(27)
|
26
|
+
# # => "Once upon a time in a wo..."
|
27
|
+
#
|
28
|
+
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
|
29
|
+
# for a total length not exceeding <tt>:length</tt>:
|
30
|
+
#
|
31
|
+
# "Once upon a time in a world far far away".truncate(27, :separator => ' ')
|
32
|
+
# # => "Once upon a time in a..."
|
33
|
+
#
|
34
|
+
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break:
|
35
|
+
#
|
36
|
+
# "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
|
37
|
+
# # => "And they f... (continued)"
|
38
|
+
def truncate(length, options = {})
|
39
|
+
text = self.dup
|
40
|
+
options[:omission] ||= "..."
|
41
|
+
|
42
|
+
length_with_room_for_omission = length - options[:omission].mb_chars.length
|
43
|
+
chars = text.mb_chars
|
44
|
+
stop = options[:separator] ?
|
45
|
+
(chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
|
46
|
+
|
47
|
+
(chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# String inflections define new methods on the String class to transform names for different purposes.
|
2
|
+
# For instance, you can figure out the name of a database from the name of a class.
|
3
|
+
#
|
4
|
+
# "ScaleScore".tableize # => "scale_scores"
|
5
|
+
#
|
6
|
+
class String
|
7
|
+
# Returns the plural form of the word in the string.
|
8
|
+
#
|
9
|
+
# "post".pluralize # => "posts"
|
10
|
+
# "octopus".pluralize # => "octopi"
|
11
|
+
# "sheep".pluralize # => "sheep"
|
12
|
+
# "words".pluralize # => "words"
|
13
|
+
# "the blue mailman".pluralize # => "the blue mailmen"
|
14
|
+
# "CamelOctopus".pluralize # => "CamelOctopi"
|
15
|
+
def pluralize
|
16
|
+
ActiveSupport::Inflector.pluralize(self)
|
17
|
+
end
|
18
|
+
|
19
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
20
|
+
#
|
21
|
+
# "posts".singularize # => "post"
|
22
|
+
# "octopi".singularize # => "octopus"
|
23
|
+
# "sheep".singularize # => "sheep"
|
24
|
+
# "word".singularize # => "word"
|
25
|
+
# "the blue mailmen".singularize # => "the blue mailman"
|
26
|
+
# "CamelOctopi".singularize # => "CamelOctopus"
|
27
|
+
def singularize
|
28
|
+
ActiveSupport::Inflector.singularize(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
|
32
|
+
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
|
33
|
+
#
|
34
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
35
|
+
#
|
36
|
+
# "active_record".camelize # => "ActiveRecord"
|
37
|
+
# "active_record".camelize(:lower) # => "activeRecord"
|
38
|
+
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
39
|
+
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
40
|
+
def camelize(first_letter = :upper)
|
41
|
+
case first_letter
|
42
|
+
when :upper then ActiveSupport::Inflector.camelize(self, true)
|
43
|
+
when :lower then ActiveSupport::Inflector.camelize(self, false)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
alias_method :camelcase, :camelize
|
47
|
+
|
48
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
49
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
50
|
+
# used in the Rails internals.
|
51
|
+
#
|
52
|
+
# +titleize+ is also aliased as +titlecase+.
|
53
|
+
#
|
54
|
+
# "man from the boondocks".titleize # => "Man From The Boondocks"
|
55
|
+
# "x-men: the last stand".titleize # => "X Men: The Last Stand"
|
56
|
+
def titleize
|
57
|
+
ActiveSupport::Inflector.titleize(self)
|
58
|
+
end
|
59
|
+
alias_method :titlecase, :titleize
|
60
|
+
|
61
|
+
# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
|
62
|
+
#
|
63
|
+
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
64
|
+
#
|
65
|
+
# "ActiveRecord".underscore # => "active_record"
|
66
|
+
# "ActiveRecord::Errors".underscore # => active_record/errors
|
67
|
+
def underscore
|
68
|
+
ActiveSupport::Inflector.underscore(self)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Replaces underscores with dashes in the string.
|
72
|
+
#
|
73
|
+
# "puni_puni" # => "puni-puni"
|
74
|
+
def dasherize
|
75
|
+
ActiveSupport::Inflector.dasherize(self)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Removes the module part from the constant expression in the string.
|
79
|
+
#
|
80
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
|
81
|
+
# "Inflections".demodulize # => "Inflections"
|
82
|
+
def demodulize
|
83
|
+
ActiveSupport::Inflector.demodulize(self)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
87
|
+
#
|
88
|
+
# ==== Examples
|
89
|
+
#
|
90
|
+
# class Person
|
91
|
+
# def to_param
|
92
|
+
# "#{id}-#{name.parameterize}"
|
93
|
+
# end
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# @person = Person.find(1)
|
97
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
98
|
+
#
|
99
|
+
# <%= link_to(@person.name, person_path %>
|
100
|
+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
101
|
+
def parameterize(sep = '-')
|
102
|
+
ActiveSupport::Inflector.parameterize(self, sep)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Creates the name of a table like Rails does for models to table names. This method
|
106
|
+
# uses the +pluralize+ method on the last word in the string.
|
107
|
+
#
|
108
|
+
# "RawScaledScorer".tableize # => "raw_scaled_scorers"
|
109
|
+
# "egg_and_ham".tableize # => "egg_and_hams"
|
110
|
+
# "fancyCategory".tableize # => "fancy_categories"
|
111
|
+
def tableize
|
112
|
+
ActiveSupport::Inflector.tableize(self)
|
113
|
+
end
|
114
|
+
|
115
|
+
# Create a class name from a plural table name like Rails does for table names to models.
|
116
|
+
# Note that this returns a string and not a class. (To convert to an actual class
|
117
|
+
# follow +classify+ with +constantize+.)
|
118
|
+
#
|
119
|
+
# "egg_and_hams".classify # => "EggAndHam"
|
120
|
+
# "posts".classify # => "Post"
|
121
|
+
#
|
122
|
+
# Singular names are not handled correctly.
|
123
|
+
#
|
124
|
+
# "business".classify # => "Busines"
|
125
|
+
def classify
|
126
|
+
ActiveSupport::Inflector.classify(self)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Capitalizes the first word, turns underscores into spaces, and strips '_id'.
|
130
|
+
# Like +titleize+, this is meant for creating pretty output.
|
131
|
+
#
|
132
|
+
# "employee_salary" # => "Employee salary"
|
133
|
+
# "author_id" # => "Author"
|
134
|
+
def humanize
|
135
|
+
ActiveSupport::Inflector.humanize(self)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Creates a foreign key name from a class name.
|
139
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
140
|
+
# the method should put '_' between the name and 'id'.
|
141
|
+
#
|
142
|
+
# Examples
|
143
|
+
# "Message".foreign_key # => "message_id"
|
144
|
+
# "Message".foreign_key(false) # => "messageid"
|
145
|
+
# "Admin::Post".foreign_key # => "post_id"
|
146
|
+
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
147
|
+
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/multibyte'
|
3
|
+
|
4
|
+
class String
|
5
|
+
if '1.9'.respond_to?(:force_encoding)
|
6
|
+
# == Multibyte proxy
|
7
|
+
#
|
8
|
+
# +mb_chars+ is a multibyte safe proxy for string methods.
|
9
|
+
#
|
10
|
+
# In Ruby 1.8 and older it creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
|
11
|
+
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
|
12
|
+
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsuled string.
|
13
|
+
#
|
14
|
+
# name = 'Claus Müller'
|
15
|
+
# name.reverse #=> "rell??M sualC"
|
16
|
+
# name.length #=> 13
|
17
|
+
#
|
18
|
+
# name.mb_chars.reverse.to_s #=> "rellüM sualC"
|
19
|
+
# name.mb_chars.length #=> 12
|
20
|
+
#
|
21
|
+
# In Ruby 1.9 and newer +mb_chars+ returns +self+ because String is (mostly) encoding aware. This means that
|
22
|
+
# it becomes easy to run one version of your code on multiple Ruby versions.
|
23
|
+
#
|
24
|
+
# == Method chaining
|
25
|
+
#
|
26
|
+
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
|
27
|
+
# method chaining on the result of any of these methods.
|
28
|
+
#
|
29
|
+
# name.mb_chars.reverse.length #=> 12
|
30
|
+
#
|
31
|
+
# == Interoperability and configuration
|
32
|
+
#
|
33
|
+
# The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
|
34
|
+
# String and Char work like expected. The bang! methods change the internal string representation in the Chars
|
35
|
+
# object. Interoperability problems can be resolved easily with a +to_s+ call.
|
36
|
+
#
|
37
|
+
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
|
38
|
+
# information about how to change the default Multibyte behaviour see ActiveSupport::Multibyte.
|
39
|
+
def mb_chars
|
40
|
+
if ActiveSupport::Multibyte.proxy_class.consumes?(self)
|
41
|
+
ActiveSupport::Multibyte.proxy_class.new(self)
|
42
|
+
else
|
43
|
+
self
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def is_utf8? #:nodoc
|
48
|
+
case encoding
|
49
|
+
when Encoding::UTF_8
|
50
|
+
valid_encoding?
|
51
|
+
when Encoding::ASCII_8BIT, Encoding::US_ASCII
|
52
|
+
dup.force_encoding(Encoding::UTF_8).valid_encoding?
|
53
|
+
else
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
def mb_chars
|
59
|
+
if ActiveSupport::Multibyte.proxy_class.wants?(self)
|
60
|
+
ActiveSupport::Multibyte.proxy_class.new(self)
|
61
|
+
else
|
62
|
+
self
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns true if the string has UTF-8 semantics (a String used for purely byte resources is unlikely to have
|
67
|
+
# them), returns false otherwise.
|
68
|
+
def is_utf8?
|
69
|
+
ActiveSupport::Multibyte::Chars.consumes?(self)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
3
|
+
|
4
|
+
class ERB
|
5
|
+
module Util
|
6
|
+
HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
|
7
|
+
JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' }
|
8
|
+
|
9
|
+
# A utility method for escaping HTML tag characters.
|
10
|
+
# This method is also aliased as <tt>h</tt>.
|
11
|
+
#
|
12
|
+
# In your ERb templates, use this method to escape any unsafe content. For example:
|
13
|
+
# <%=h @person.name %>
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
# puts html_escape("is a > 0 & a < 10?")
|
17
|
+
# # => is a > 0 & a < 10?
|
18
|
+
def html_escape(s)
|
19
|
+
s = s.to_s
|
20
|
+
if s.html_safe?
|
21
|
+
s
|
22
|
+
else
|
23
|
+
s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }.html_safe
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
remove_method(:h)
|
28
|
+
alias h html_escape
|
29
|
+
|
30
|
+
module_function :h
|
31
|
+
|
32
|
+
singleton_class.send(:remove_method, :html_escape)
|
33
|
+
module_function :html_escape
|
34
|
+
|
35
|
+
# A utility method for escaping HTML entities in JSON strings.
|
36
|
+
# This method is also aliased as <tt>j</tt>.
|
37
|
+
#
|
38
|
+
# In your ERb templates, use this method to escape any HTML entities:
|
39
|
+
# <%=j @person.to_json %>
|
40
|
+
#
|
41
|
+
# ==== Example:
|
42
|
+
# puts json_escape("is a > 0 & a < 10?")
|
43
|
+
# # => is a \u003E 0 \u0026 a \u003C 10?
|
44
|
+
def json_escape(s)
|
45
|
+
s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
|
46
|
+
end
|
47
|
+
|
48
|
+
alias j json_escape
|
49
|
+
module_function :j
|
50
|
+
module_function :json_escape
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Object
|
55
|
+
def html_safe?
|
56
|
+
false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Fixnum
|
61
|
+
def html_safe?
|
62
|
+
true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
module ActiveSupport #:nodoc:
|
67
|
+
class SafeBuffer < String
|
68
|
+
alias safe_concat concat
|
69
|
+
|
70
|
+
def concat(value)
|
71
|
+
if value.html_safe?
|
72
|
+
super(value)
|
73
|
+
else
|
74
|
+
super(ERB::Util.h(value))
|
75
|
+
end
|
76
|
+
end
|
77
|
+
alias << concat
|
78
|
+
|
79
|
+
def +(other)
|
80
|
+
dup.concat(other)
|
81
|
+
end
|
82
|
+
|
83
|
+
def html_safe?
|
84
|
+
true
|
85
|
+
end
|
86
|
+
|
87
|
+
def html_safe
|
88
|
+
self
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_s
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_yaml(*args)
|
96
|
+
to_str.to_yaml(*args)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class String
|
102
|
+
def html_safe!
|
103
|
+
raise "You can't call html_safe! on a String"
|
104
|
+
end
|
105
|
+
|
106
|
+
def html_safe
|
107
|
+
ActiveSupport::SafeBuffer.new(self)
|
108
|
+
end
|
109
|
+
end
|