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.
Files changed (256) hide show
  1. data/.gitignore +1 -0
  2. data/COPYING +367 -0
  3. data/Rakefile +10 -10
  4. data/VERSION +1 -1
  5. data/bin/ai +19 -0
  6. data/csd.gemspec +257 -35
  7. data/lib/active_support.rb +75 -0
  8. data/lib/active_support/all.rb +3 -0
  9. data/lib/active_support/backtrace_cleaner.rb +94 -0
  10. data/lib/active_support/base64.rb +42 -0
  11. data/lib/active_support/basic_object.rb +21 -0
  12. data/lib/active_support/benchmarkable.rb +60 -0
  13. data/lib/active_support/buffered_logger.rb +132 -0
  14. data/lib/active_support/builder.rb +6 -0
  15. data/lib/active_support/cache.rb +626 -0
  16. data/lib/active_support/cache/compressed_mem_cache_store.rb +13 -0
  17. data/lib/active_support/cache/file_store.rb +188 -0
  18. data/lib/active_support/cache/mem_cache_store.rb +191 -0
  19. data/lib/active_support/cache/memory_store.rb +159 -0
  20. data/lib/active_support/cache/strategy/local_cache.rb +164 -0
  21. data/lib/active_support/cache/synchronized_memory_store.rb +11 -0
  22. data/lib/active_support/callbacks.rb +600 -0
  23. data/lib/active_support/concern.rb +29 -0
  24. data/lib/active_support/configurable.rb +36 -0
  25. data/lib/active_support/core_ext.rb +3 -0
  26. data/lib/active_support/core_ext/array.rb +7 -0
  27. data/lib/active_support/core_ext/array/access.rb +46 -0
  28. data/lib/active_support/core_ext/array/conversions.rb +164 -0
  29. data/lib/active_support/core_ext/array/extract_options.rb +29 -0
  30. data/lib/active_support/core_ext/array/grouping.rb +100 -0
  31. data/lib/active_support/core_ext/array/random_access.rb +20 -0
  32. data/lib/active_support/core_ext/array/uniq_by.rb +17 -0
  33. data/lib/active_support/core_ext/array/wrap.rb +22 -0
  34. data/lib/active_support/core_ext/benchmark.rb +7 -0
  35. data/lib/active_support/core_ext/big_decimal.rb +1 -0
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +27 -0
  37. data/lib/active_support/core_ext/cgi.rb +1 -0
  38. data/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb +19 -0
  39. data/lib/active_support/core_ext/class.rb +4 -0
  40. data/lib/active_support/core_ext/class/attribute.rb +67 -0
  41. data/lib/active_support/core_ext/class/attribute_accessors.rb +63 -0
  42. data/lib/active_support/core_ext/class/delegating_attributes.rb +44 -0
  43. data/lib/active_support/core_ext/class/inheritable_attributes.rb +232 -0
  44. data/lib/active_support/core_ext/class/subclasses.rb +55 -0
  45. data/lib/active_support/core_ext/date/acts_like.rb +8 -0
  46. data/lib/active_support/core_ext/date/calculations.rb +240 -0
  47. data/lib/active_support/core_ext/date/conversions.rb +99 -0
  48. data/lib/active_support/core_ext/date/freeze.rb +31 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +13 -0
  50. data/lib/active_support/core_ext/date_time/calculations.rb +113 -0
  51. data/lib/active_support/core_ext/date_time/conversions.rb +102 -0
  52. data/lib/active_support/core_ext/date_time/zones.rb +17 -0
  53. data/lib/active_support/core_ext/enumerable.rb +119 -0
  54. data/lib/active_support/core_ext/exception.rb +3 -0
  55. data/lib/active_support/core_ext/file.rb +2 -0
  56. data/lib/active_support/core_ext/file/atomic.rb +41 -0
  57. data/lib/active_support/core_ext/file/path.rb +5 -0
  58. data/lib/active_support/core_ext/float.rb +1 -0
  59. data/lib/active_support/core_ext/float/rounding.rb +19 -0
  60. data/lib/active_support/core_ext/hash.rb +8 -0
  61. data/lib/active_support/core_ext/hash/conversions.rb +150 -0
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +16 -0
  63. data/lib/active_support/core_ext/hash/diff.rb +13 -0
  64. data/lib/active_support/core_ext/hash/except.rb +24 -0
  65. data/lib/active_support/core_ext/hash/indifferent_access.rb +14 -0
  66. data/lib/active_support/core_ext/hash/keys.rb +45 -0
  67. data/lib/active_support/core_ext/hash/reverse_merge.rb +28 -0
  68. data/lib/active_support/core_ext/hash/slice.rb +38 -0
  69. data/lib/active_support/core_ext/integer.rb +3 -0
  70. data/lib/active_support/core_ext/integer/inflections.rb +14 -0
  71. data/lib/active_support/core_ext/integer/multiple.rb +6 -0
  72. data/lib/active_support/core_ext/integer/time.rb +39 -0
  73. data/lib/active_support/core_ext/kernel.rb +5 -0
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
  75. data/lib/active_support/core_ext/kernel/debugger.rb +16 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +62 -0
  77. data/lib/active_support/core_ext/kernel/requires.rb +26 -0
  78. data/lib/active_support/core_ext/kernel/singleton_class.rb +13 -0
  79. data/lib/active_support/core_ext/load_error.rb +23 -0
  80. data/lib/active_support/core_ext/logger.rb +146 -0
  81. data/lib/active_support/core_ext/module.rb +12 -0
  82. data/lib/active_support/core_ext/module/aliasing.rb +70 -0
  83. data/lib/active_support/core_ext/module/anonymous.rb +24 -0
  84. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +31 -0
  85. data/lib/active_support/core_ext/module/attr_internal.rb +32 -0
  86. data/lib/active_support/core_ext/module/attribute_accessors.rb +66 -0
  87. data/lib/active_support/core_ext/module/delegation.rb +146 -0
  88. data/lib/active_support/core_ext/module/deprecation.rb +9 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +88 -0
  90. data/lib/active_support/core_ext/module/method_names.rb +14 -0
  91. data/lib/active_support/core_ext/module/reachable.rb +10 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +6 -0
  93. data/lib/active_support/core_ext/module/synchronization.rb +42 -0
  94. data/lib/active_support/core_ext/name_error.rb +18 -0
  95. data/lib/active_support/core_ext/numeric.rb +2 -0
  96. data/lib/active_support/core_ext/numeric/bytes.rb +44 -0
  97. data/lib/active_support/core_ext/numeric/time.rb +77 -0
  98. data/lib/active_support/core_ext/object.rb +14 -0
  99. data/lib/active_support/core_ext/object/acts_like.rb +10 -0
  100. data/lib/active_support/core_ext/object/blank.rb +76 -0
  101. data/lib/active_support/core_ext/object/conversions.rb +4 -0
  102. data/lib/active_support/core_ext/object/duplicable.rb +65 -0
  103. data/lib/active_support/core_ext/object/extending.rb +11 -0
  104. data/lib/active_support/core_ext/object/instance_variables.rb +67 -0
  105. data/lib/active_support/core_ext/object/misc.rb +2 -0
  106. data/lib/active_support/core_ext/object/returning.rb +42 -0
  107. data/lib/active_support/core_ext/object/to_param.rb +49 -0
  108. data/lib/active_support/core_ext/object/to_query.rb +27 -0
  109. data/lib/active_support/core_ext/object/try.rb +36 -0
  110. data/lib/active_support/core_ext/object/with_options.rb +26 -0
  111. data/lib/active_support/core_ext/proc.rb +14 -0
  112. data/lib/active_support/core_ext/process.rb +1 -0
  113. data/lib/active_support/core_ext/process/daemon.rb +23 -0
  114. data/lib/active_support/core_ext/range.rb +4 -0
  115. data/lib/active_support/core_ext/range/blockless_step.rb +29 -0
  116. data/lib/active_support/core_ext/range/conversions.rb +21 -0
  117. data/lib/active_support/core_ext/range/include_range.rb +21 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +8 -0
  119. data/lib/active_support/core_ext/regexp.rb +5 -0
  120. data/lib/active_support/core_ext/rexml.rb +46 -0
  121. data/lib/active_support/core_ext/string.rb +12 -0
  122. data/lib/active_support/core_ext/string/access.rb +99 -0
  123. data/lib/active_support/core_ext/string/behavior.rb +7 -0
  124. data/lib/active_support/core_ext/string/conversions.rb +61 -0
  125. data/lib/active_support/core_ext/string/encoding.rb +11 -0
  126. data/lib/active_support/core_ext/string/exclude.rb +6 -0
  127. data/lib/active_support/core_ext/string/filters.rb +49 -0
  128. data/lib/active_support/core_ext/string/inflections.rb +149 -0
  129. data/lib/active_support/core_ext/string/interpolation.rb +2 -0
  130. data/lib/active_support/core_ext/string/multibyte.rb +72 -0
  131. data/lib/active_support/core_ext/string/output_safety.rb +109 -0
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
  133. data/lib/active_support/core_ext/string/xchar.rb +18 -0
  134. data/lib/active_support/core_ext/time/acts_like.rb +8 -0
  135. data/lib/active_support/core_ext/time/calculations.rb +282 -0
  136. data/lib/active_support/core_ext/time/conversions.rb +85 -0
  137. data/lib/active_support/core_ext/time/marshal.rb +56 -0
  138. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +10 -0
  139. data/lib/active_support/core_ext/time/zones.rb +78 -0
  140. data/lib/active_support/core_ext/uri.rb +22 -0
  141. data/lib/active_support/dependencies.rb +628 -0
  142. data/lib/active_support/dependencies/autoload.rb +50 -0
  143. data/lib/active_support/deprecation.rb +18 -0
  144. data/lib/active_support/deprecation/behaviors.rb +38 -0
  145. data/lib/active_support/deprecation/method_wrappers.rb +29 -0
  146. data/lib/active_support/deprecation/proxy_wrappers.rb +74 -0
  147. data/lib/active_support/deprecation/reporting.rb +56 -0
  148. data/lib/active_support/duration.rb +105 -0
  149. data/lib/active_support/gzip.rb +25 -0
  150. data/lib/active_support/hash_with_indifferent_access.rb +145 -0
  151. data/lib/active_support/i18n.rb +8 -0
  152. data/lib/active_support/inflections.rb +56 -0
  153. data/lib/active_support/inflector.rb +7 -0
  154. data/lib/active_support/inflector/inflections.rb +211 -0
  155. data/lib/active_support/inflector/methods.rb +141 -0
  156. data/lib/active_support/inflector/transliterate.rb +97 -0
  157. data/lib/active_support/json.rb +2 -0
  158. data/lib/active_support/json/backends/jsongem.rb +43 -0
  159. data/lib/active_support/json/backends/yajl.rb +40 -0
  160. data/lib/active_support/json/backends/yaml.rb +90 -0
  161. data/lib/active_support/json/decoding.rb +51 -0
  162. data/lib/active_support/json/encoding.rb +254 -0
  163. data/lib/active_support/json/variable.rb +11 -0
  164. data/lib/active_support/lazy_load_hooks.rb +27 -0
  165. data/lib/active_support/locale/en.yml +36 -0
  166. data/lib/active_support/memoizable.rb +103 -0
  167. data/lib/active_support/message_encryptor.rb +71 -0
  168. data/lib/active_support/message_verifier.rb +62 -0
  169. data/lib/active_support/multibyte.rb +44 -0
  170. data/lib/active_support/multibyte/chars.rb +480 -0
  171. data/lib/active_support/multibyte/exceptions.rb +8 -0
  172. data/lib/active_support/multibyte/unicode.rb +393 -0
  173. data/lib/active_support/multibyte/utils.rb +60 -0
  174. data/lib/active_support/notifications.rb +81 -0
  175. data/lib/active_support/notifications/fanout.rb +93 -0
  176. data/lib/active_support/notifications/instrumenter.rb +56 -0
  177. data/lib/active_support/option_merger.rb +25 -0
  178. data/lib/active_support/ordered_hash.rb +158 -0
  179. data/lib/active_support/ordered_options.rb +27 -0
  180. data/lib/active_support/railtie.rb +100 -0
  181. data/lib/active_support/rescuable.rb +114 -0
  182. data/lib/active_support/ruby/shim.rb +22 -0
  183. data/lib/active_support/secure_random.rb +199 -0
  184. data/lib/active_support/string_inquirer.rb +21 -0
  185. data/lib/active_support/test_case.rb +42 -0
  186. data/lib/active_support/testing/assertions.rb +82 -0
  187. data/lib/active_support/testing/declarative.rb +40 -0
  188. data/lib/active_support/testing/default.rb +9 -0
  189. data/lib/active_support/testing/deprecation.rb +55 -0
  190. data/lib/active_support/testing/isolation.rb +154 -0
  191. data/lib/active_support/testing/pending.rb +48 -0
  192. data/lib/active_support/testing/performance.rb +455 -0
  193. data/lib/active_support/testing/setup_and_teardown.rb +111 -0
  194. data/lib/active_support/time.rb +34 -0
  195. data/lib/active_support/time/autoload.rb +5 -0
  196. data/lib/active_support/time_with_zone.rb +341 -0
  197. data/lib/active_support/values/time_zone.rb +377 -0
  198. data/lib/active_support/values/unicode_tables.dat +0 -0
  199. data/lib/active_support/version.rb +10 -0
  200. data/lib/active_support/whiny_nil.rb +60 -0
  201. data/lib/active_support/xml_mini.rb +158 -0
  202. data/lib/active_support/xml_mini/jdom.rb +168 -0
  203. data/lib/active_support/xml_mini/libxml.rb +80 -0
  204. data/lib/active_support/xml_mini/libxmlsax.rb +85 -0
  205. data/lib/active_support/xml_mini/nokogiri.rb +78 -0
  206. data/lib/active_support/xml_mini/nokogirisax.rb +83 -0
  207. data/lib/active_support/xml_mini/rexml.rb +129 -0
  208. data/lib/csd.rb +82 -2
  209. data/lib/csd/application.rb +2 -0
  210. data/lib/csd/application/default.rb +51 -0
  211. data/lib/csd/application/default/base.rb +15 -0
  212. data/lib/csd/application/minisip.rb +25 -0
  213. data/lib/csd/application/minisip/about.yml +14 -0
  214. data/lib/csd/application/minisip/base.rb +161 -0
  215. data/lib/csd/application/minisip/error.rb +11 -0
  216. data/lib/csd/application/minisip/options/common.rb +0 -0
  217. data/lib/csd/application/minisip/options/compile.rb +59 -0
  218. data/lib/csd/{applications → application}/minisip/unix/base.rb +10 -11
  219. data/lib/csd/application/opensips/about.yml +2 -0
  220. data/lib/csd/applications.rb +55 -0
  221. data/lib/csd/commands.rb +88 -65
  222. data/lib/csd/error.rb +31 -0
  223. data/lib/csd/extensions.rb +1 -0
  224. data/lib/{extensions → csd/extensions}/core/array.rb +2 -2
  225. data/lib/csd/extensions/core/dir.rb +46 -0
  226. data/lib/{extensions → csd/extensions}/core/file.rb +2 -2
  227. data/lib/{extensions → csd/extensions}/core/object.rb +2 -2
  228. data/lib/csd/extensions/core/option_parser.rb +33 -0
  229. data/lib/{extensions → csd/extensions}/core/pathname.rb +12 -3
  230. data/lib/{extensions → csd/extensions}/core/string.rb +2 -2
  231. data/lib/{extensions → csd/extensions}/gem/platform.rb +6 -2
  232. data/lib/csd/global_open_struct.rb +18 -0
  233. data/lib/csd/options.rb +124 -95
  234. data/lib/csd/path.rb +31 -0
  235. data/lib/csd/ui.rb +1 -0
  236. data/lib/csd/ui/cli.rb +7 -0
  237. data/lib/csd/ui/ui.rb +46 -0
  238. data/lib/csd/version.rb +9 -0
  239. data/lib/term/ansicolor.rb +102 -0
  240. data/lib/term/ansicolor/.keep +0 -0
  241. data/lib/term/ansicolor/version.rb +10 -0
  242. data/test/functional/test_applications.rb +86 -0
  243. data/test/functional/test_commands.rb +42 -29
  244. data/test/functional/test_options.rb +98 -0
  245. data/test/helper.rb +14 -0
  246. data/test/unit/test_dir.rb +38 -0
  247. data/test/unit/test_pathname.rb +32 -0
  248. metadata +253 -40
  249. data/LICENSE +0 -20
  250. data/bin/csd +0 -8
  251. data/lib/csd/applications/base.rb +0 -33
  252. data/lib/csd/applications/minisip/base.rb +0 -125
  253. data/lib/csd/applications/minisip/init.rb +0 -20
  254. data/lib/csd/init.rb +0 -69
  255. data/lib/csd/path_container.rb +0 -15
  256. data/publish +0 -29
@@ -0,0 +1,8 @@
1
+ #begin
2
+ # require 'i18n'
3
+ #rescue LoadError => e
4
+ # $stderr.puts "You don't have i18n installed in your application. Please add it to your Gemfile and run bundle install"
5
+ # raise e
6
+ #end
7
+ #I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml"
8
+ #ActiveSupport.run_load_hooks(:i18n)
@@ -0,0 +1,56 @@
1
+ module ActiveSupport
2
+ Inflector.inflections do |inflect|
3
+ inflect.plural(/$/, 's')
4
+ inflect.plural(/s$/i, 's')
5
+ inflect.plural(/(ax|test)is$/i, '\1es')
6
+ inflect.plural(/(octop|vir)us$/i, '\1i')
7
+ inflect.plural(/(alias|status)$/i, '\1es')
8
+ inflect.plural(/(bu)s$/i, '\1ses')
9
+ inflect.plural(/(buffal|tomat)o$/i, '\1oes')
10
+ inflect.plural(/([ti])um$/i, '\1a')
11
+ inflect.plural(/sis$/i, 'ses')
12
+ inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
13
+ inflect.plural(/(hive)$/i, '\1s')
14
+ inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
15
+ inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
16
+ inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
17
+ inflect.plural(/([m|l])ouse$/i, '\1ice')
18
+ inflect.plural(/^(ox)$/i, '\1en')
19
+ inflect.plural(/(quiz)$/i, '\1zes')
20
+
21
+ inflect.singular(/s$/i, '')
22
+ inflect.singular(/(n)ews$/i, '\1ews')
23
+ inflect.singular(/([ti])a$/i, '\1um')
24
+ inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
25
+ inflect.singular(/(^analy)ses$/i, '\1sis')
26
+ inflect.singular(/([^f])ves$/i, '\1fe')
27
+ inflect.singular(/(hive)s$/i, '\1')
28
+ inflect.singular(/(tive)s$/i, '\1')
29
+ inflect.singular(/([lr])ves$/i, '\1f')
30
+ inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
31
+ inflect.singular(/(s)eries$/i, '\1eries')
32
+ inflect.singular(/(m)ovies$/i, '\1ovie')
33
+ inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
34
+ inflect.singular(/([m|l])ice$/i, '\1ouse')
35
+ inflect.singular(/(bus)es$/i, '\1')
36
+ inflect.singular(/(o)es$/i, '\1')
37
+ inflect.singular(/(shoe)s$/i, '\1')
38
+ inflect.singular(/(cris|ax|test)es$/i, '\1is')
39
+ inflect.singular(/(octop|vir)i$/i, '\1us')
40
+ inflect.singular(/(alias|status)es$/i, '\1')
41
+ inflect.singular(/^(ox)en/i, '\1')
42
+ inflect.singular(/(vert|ind)ices$/i, '\1ex')
43
+ inflect.singular(/(matr)ices$/i, '\1ix')
44
+ inflect.singular(/(quiz)zes$/i, '\1')
45
+ inflect.singular(/(database)s$/i, '\1')
46
+
47
+ inflect.irregular('person', 'people')
48
+ inflect.irregular('man', 'men')
49
+ inflect.irregular('child', 'children')
50
+ inflect.irregular('sex', 'sexes')
51
+ inflect.irregular('move', 'moves')
52
+ inflect.irregular('cow', 'kine')
53
+
54
+ inflect.uncountable(%w(equipment information rice money species series fish sheep jeans))
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ # in case active_support/inflector is required without the rest of active_support
2
+ require 'active_support/inflector/inflections'
3
+ require 'active_support/inflector/transliterate'
4
+ require 'active_support/inflector/methods'
5
+
6
+ require 'active_support/inflections'
7
+ require 'active_support/core_ext/string/inflections'
@@ -0,0 +1,211 @@
1
+ module ActiveSupport
2
+ module Inflector
3
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
4
+ # inflection rules. Examples:
5
+ #
6
+ # ActiveSupport::Inflector.inflections do |inflect|
7
+ # inflect.plural /^(ox)$/i, '\1\2en'
8
+ # inflect.singular /^(ox)en/i, '\1'
9
+ #
10
+ # inflect.irregular 'octopus', 'octopi'
11
+ #
12
+ # inflect.uncountable "equipment"
13
+ # end
14
+ #
15
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
16
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
17
+ # already have been loaded.
18
+ class Inflections
19
+ def self.instance
20
+ @__instance__ ||= new
21
+ end
22
+
23
+ attr_reader :plurals, :singulars, :uncountables, :humans
24
+
25
+ def initialize
26
+ @plurals, @singulars, @uncountables, @humans = [], [], [], []
27
+ end
28
+
29
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
30
+ # The replacement should always be a string that may include references to the matched data from the rule.
31
+ def plural(rule, replacement)
32
+ @uncountables.delete(rule) if rule.is_a?(String)
33
+ @uncountables.delete(replacement)
34
+ @plurals.insert(0, [rule, replacement])
35
+ end
36
+
37
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
38
+ # The replacement should always be a string that may include references to the matched data from the rule.
39
+ def singular(rule, replacement)
40
+ @uncountables.delete(rule) if rule.is_a?(String)
41
+ @uncountables.delete(replacement)
42
+ @singulars.insert(0, [rule, replacement])
43
+ end
44
+
45
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
46
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
47
+ #
48
+ # Examples:
49
+ # irregular 'octopus', 'octopi'
50
+ # irregular 'person', 'people'
51
+ def irregular(singular, plural)
52
+ @uncountables.delete(singular)
53
+ @uncountables.delete(plural)
54
+ if singular[0,1].upcase == plural[0,1].upcase
55
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
56
+ plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
57
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
58
+ else
59
+ plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
60
+ plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
61
+ plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
62
+ plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
63
+ singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
64
+ singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
65
+ end
66
+ end
67
+
68
+ # Add uncountable words that shouldn't be attempted inflected.
69
+ #
70
+ # Examples:
71
+ # uncountable "money"
72
+ # uncountable "money", "information"
73
+ # uncountable %w( money information rice )
74
+ def uncountable(*words)
75
+ (@uncountables << words).flatten!
76
+ end
77
+
78
+ # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
79
+ # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
80
+ # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
81
+ #
82
+ # Examples:
83
+ # human /_cnt$/i, '\1_count'
84
+ # human "legacy_col_person_name", "Name"
85
+ def human(rule, replacement)
86
+ @humans.insert(0, [rule, replacement])
87
+ end
88
+
89
+ # Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
90
+ # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
91
+ # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
92
+ #
93
+ # Examples:
94
+ # clear :all
95
+ # clear :plurals
96
+ def clear(scope = :all)
97
+ case scope
98
+ when :all
99
+ @plurals, @singulars, @uncountables = [], [], []
100
+ else
101
+ instance_variable_set "@#{scope}", []
102
+ end
103
+ end
104
+ end
105
+
106
+ # Yields a singleton instance of Inflector::Inflections so you can specify additional
107
+ # inflector rules.
108
+ #
109
+ # Example:
110
+ # ActiveSupport::Inflector.inflections do |inflect|
111
+ # inflect.uncountable "rails"
112
+ # end
113
+ def inflections
114
+ if block_given?
115
+ yield Inflections.instance
116
+ else
117
+ Inflections.instance
118
+ end
119
+ end
120
+
121
+ # Returns the plural form of the word in the string.
122
+ #
123
+ # Examples:
124
+ # "post".pluralize # => "posts"
125
+ # "octopus".pluralize # => "octopi"
126
+ # "sheep".pluralize # => "sheep"
127
+ # "words".pluralize # => "words"
128
+ # "CamelOctopus".pluralize # => "CamelOctopi"
129
+ def pluralize(word)
130
+ result = word.to_s.dup
131
+
132
+ if word.empty? || inflections.uncountables.include?(result.downcase)
133
+ result
134
+ else
135
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
136
+ result
137
+ end
138
+ end
139
+
140
+ # The reverse of +pluralize+, returns the singular form of a word in a string.
141
+ #
142
+ # Examples:
143
+ # "posts".singularize # => "post"
144
+ # "octopi".singularize # => "octopus"
145
+ # "sheep".singularize # => "sheep"
146
+ # "word".singularize # => "word"
147
+ # "CamelOctopi".singularize # => "CamelOctopus"
148
+ def singularize(word)
149
+ result = word.to_s.dup
150
+
151
+ if inflections.uncountables.any? { |inflection| result =~ /#{inflection}\Z/i }
152
+ result
153
+ else
154
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
155
+ result
156
+ end
157
+ end
158
+
159
+ # Capitalizes the first word and turns underscores into spaces and strips a
160
+ # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
161
+ #
162
+ # Examples:
163
+ # "employee_salary" # => "Employee salary"
164
+ # "author_id" # => "Author"
165
+ def humanize(lower_case_and_underscored_word)
166
+ result = lower_case_and_underscored_word.to_s.dup
167
+
168
+ inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
169
+ result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
170
+ end
171
+
172
+ # Capitalizes all the words and replaces some characters in the string to create
173
+ # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
174
+ # used in the Rails internals.
175
+ #
176
+ # +titleize+ is also aliased as as +titlecase+.
177
+ #
178
+ # Examples:
179
+ # "man from the boondocks".titleize # => "Man From The Boondocks"
180
+ # "x-men: the last stand".titleize # => "X Men: The Last Stand"
181
+ def titleize(word)
182
+ humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
183
+ end
184
+
185
+ # Create the name of a table like Rails does for models to table names. This method
186
+ # uses the +pluralize+ method on the last word in the string.
187
+ #
188
+ # Examples
189
+ # "RawScaledScorer".tableize # => "raw_scaled_scorers"
190
+ # "egg_and_ham".tableize # => "egg_and_hams"
191
+ # "fancyCategory".tableize # => "fancy_categories"
192
+ def tableize(class_name)
193
+ pluralize(underscore(class_name))
194
+ end
195
+
196
+ # Create a class name from a plural table name like Rails does for table names to models.
197
+ # Note that this returns a string and not a Class. (To convert to an actual class
198
+ # follow +classify+ with +constantize+.)
199
+ #
200
+ # Examples:
201
+ # "egg_and_hams".classify # => "EggAndHam"
202
+ # "posts".classify # => "Post"
203
+ #
204
+ # Singular names are not handled correctly:
205
+ # "business".classify # => "Busines"
206
+ def classify(table_name)
207
+ # strip out any leading schema name
208
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,141 @@
1
+ module ActiveSupport
2
+ # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
3
+ # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
4
+ # in inflections.rb.
5
+ #
6
+ # The Rails core team has stated patches for the inflections library will not be accepted
7
+ # in order to avoid breaking legacy applications which may be relying on errant inflections.
8
+ # If you discover an incorrect inflection and require it for your application, you'll need
9
+ # to correct it yourself (explained below).
10
+ module Inflector
11
+ extend self
12
+
13
+ # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
14
+ # is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
15
+ #
16
+ # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
17
+ #
18
+ # Examples:
19
+ # "active_record".camelize # => "ActiveRecord"
20
+ # "active_record".camelize(:lower) # => "activeRecord"
21
+ # "active_record/errors".camelize # => "ActiveRecord::Errors"
22
+ # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
23
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
24
+ if first_letter_in_uppercase
25
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
26
+ else
27
+ lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
28
+ end
29
+ end
30
+
31
+ # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
32
+ #
33
+ # Changes '::' to '/' to convert namespaces to paths.
34
+ #
35
+ # Examples:
36
+ # "ActiveRecord".underscore # => "active_record"
37
+ # "ActiveRecord::Errors".underscore # => active_record/errors
38
+ def underscore(camel_cased_word)
39
+ word = camel_cased_word.to_s.dup
40
+ word.gsub!(/::/, '/')
41
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
42
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
43
+ word.tr!("-", "_")
44
+ word.downcase!
45
+ word
46
+ end
47
+
48
+ # Replaces underscores with dashes in the string.
49
+ #
50
+ # Example:
51
+ # "puni_puni" # => "puni-puni"
52
+ def dasherize(underscored_word)
53
+ underscored_word.gsub(/_/, '-')
54
+ end
55
+
56
+ # Removes the module part from the expression in the string.
57
+ #
58
+ # Examples:
59
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
60
+ # "Inflections".demodulize # => "Inflections"
61
+ def demodulize(class_name_in_module)
62
+ class_name_in_module.to_s.gsub(/^.*::/, '')
63
+ end
64
+
65
+ # Creates a foreign key name from a class name.
66
+ # +separate_class_name_and_id_with_underscore+ sets whether
67
+ # the method should put '_' between the name and 'id'.
68
+ #
69
+ # Examples:
70
+ # "Message".foreign_key # => "message_id"
71
+ # "Message".foreign_key(false) # => "messageid"
72
+ # "Admin::Post".foreign_key # => "post_id"
73
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
74
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
75
+ end
76
+
77
+ # Ruby 1.9 introduces an inherit argument for Module#const_get and
78
+ # #const_defined? and changes their default behavior.
79
+ if Module.method(:const_get).arity == 1
80
+ # Tries to find a constant with the name specified in the argument string:
81
+ #
82
+ # "Module".constantize # => Module
83
+ # "Test::Unit".constantize # => Test::Unit
84
+ #
85
+ # The name is assumed to be the one of a top-level constant, no matter whether
86
+ # it starts with "::" or not. No lexical context is taken into account:
87
+ #
88
+ # C = 'outside'
89
+ # module M
90
+ # C = 'inside'
91
+ # C # => 'inside'
92
+ # "C".constantize # => 'outside', same as ::C
93
+ # end
94
+ #
95
+ # NameError is raised when the name is not in CamelCase or the constant is
96
+ # unknown.
97
+ def constantize(camel_cased_word)
98
+ names = camel_cased_word.split('::')
99
+ names.shift if names.empty? || names.first.empty?
100
+
101
+ constant = Object
102
+ names.each do |name|
103
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
104
+ end
105
+ constant
106
+ end
107
+ else
108
+ def constantize(camel_cased_word) #:nodoc:
109
+ names = camel_cased_word.split('::')
110
+ names.shift if names.empty? || names.first.empty?
111
+
112
+ constant = Object
113
+ names.each do |name|
114
+ constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
115
+ end
116
+ constant
117
+ end
118
+ end
119
+
120
+ # Turns a number into an ordinal string used to denote the position in an
121
+ # ordered sequence such as 1st, 2nd, 3rd, 4th.
122
+ #
123
+ # Examples:
124
+ # ordinalize(1) # => "1st"
125
+ # ordinalize(2) # => "2nd"
126
+ # ordinalize(1002) # => "1002nd"
127
+ # ordinalize(1003) # => "1003rd"
128
+ def ordinalize(number)
129
+ if (11..13).include?(number.to_i % 100)
130
+ "#{number}th"
131
+ else
132
+ case number.to_i % 10
133
+ when 1; "#{number}st"
134
+ when 2; "#{number}nd"
135
+ when 3; "#{number}rd"
136
+ else "#{number}th"
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ require 'active_support/core_ext/string/multibyte'
3
+
4
+ module ActiveSupport
5
+ module Inflector
6
+
7
+ # Replaces non-ASCII characters with an ASCII approximation, or if none
8
+ # exists, a replacement character which defaults to "?".
9
+ #
10
+ # transliterate("Ærøskøbing")
11
+ # # => "AEroskobing"
12
+ #
13
+ # Default approximations are provided for Western/Latin characters,
14
+ # e.g, "ø", "ñ", "é", "ß", etc.
15
+ #
16
+ # This method is I18n aware, so you can set up custom approximations for a
17
+ # locale. This can be useful, for example, to transliterate German's "ü"
18
+ # and "ö" to "ue" and "oe", or to add support for transliterating Russian
19
+ # to ASCII.
20
+ #
21
+ # In order to make your custom transliterations available, you must set
22
+ # them as the <tt>i18n.transliterate.rule</tt> i18n key:
23
+ #
24
+ # # Store the transliterations in locales/de.yml
25
+ # i18n:
26
+ # transliterate:
27
+ # rule:
28
+ # ü: "ue"
29
+ # ö: "oe"
30
+ #
31
+ # # Or set them using Ruby
32
+ # I18n.backend.store_translations(:de, :i18n => {
33
+ # :transliterate => {
34
+ # :rule => {
35
+ # "ü" => "ue",
36
+ # "ö" => "oe"
37
+ # }
38
+ # }
39
+ # })
40
+ #
41
+ # The value for <tt>i18n.transliterate.rule</tt> can be a simple Hash that maps
42
+ # characters to ASCII approximations as shown above, or, for more complex
43
+ # requirements, a Proc:
44
+ #
45
+ # I18n.backend.store_translations(:de, :i18n => {
46
+ # :transliterate => {
47
+ # :rule => lambda {|string| MyTransliterator.transliterate(string)}
48
+ # }
49
+ # })
50
+ #
51
+ # Now you can have different transliterations for each locale:
52
+ #
53
+ # I18n.locale = :en
54
+ # transliterate("Jürgen")
55
+ # # => "Jurgen"
56
+ #
57
+ # I18n.locale = :de
58
+ # transliterate("Jürgen")
59
+ # # => "Juergen"
60
+ def transliterate(string, replacement = "?")
61
+ I18n.transliterate(ActiveSupport::Multibyte::Unicode.normalize(
62
+ ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
63
+ :replacement => replacement)
64
+ end
65
+
66
+ # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
67
+ #
68
+ # ==== Examples
69
+ #
70
+ # class Person
71
+ # def to_param
72
+ # "#{id}-#{name.parameterize}"
73
+ # end
74
+ # end
75
+ #
76
+ # @person = Person.find(1)
77
+ # # => #<Person id: 1, name: "Donald E. Knuth">
78
+ #
79
+ # <%= link_to(@person.name, person_path(@person)) %>
80
+ # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
81
+ def parameterize(string, sep = '-')
82
+ # replace accented chars with their ascii equivalents
83
+ parameterized_string = transliterate(string)
84
+ # Turn unwanted chars into the separator
85
+ parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
86
+ unless sep.nil? || sep.empty?
87
+ re_sep = Regexp.escape(sep)
88
+ # No more than one of the separator in a row.
89
+ parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
90
+ # Remove leading/trailing separator.
91
+ parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
92
+ end
93
+ parameterized_string.downcase
94
+ end
95
+
96
+ end
97
+ end