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,12 @@
1
+ require 'active_support/core_ext/module/aliasing'
2
+ require 'active_support/core_ext/module/introspection'
3
+ require 'active_support/core_ext/module/anonymous'
4
+ require 'active_support/core_ext/module/reachable'
5
+ require 'active_support/core_ext/module/attribute_accessors'
6
+ require 'active_support/core_ext/module/attr_internal'
7
+ require 'active_support/core_ext/module/attr_accessor_with_default'
8
+ require 'active_support/core_ext/module/delegation'
9
+ require 'active_support/core_ext/module/synchronization'
10
+ require 'active_support/core_ext/module/deprecation'
11
+ require 'active_support/core_ext/module/remove_method'
12
+ require 'active_support/core_ext/module/method_names'
@@ -0,0 +1,70 @@
1
+ class Module
2
+ # Encapsulates the common pattern of:
3
+ #
4
+ # alias_method :foo_without_feature, :foo
5
+ # alias_method :foo, :foo_with_feature
6
+ #
7
+ # With this, you simply do:
8
+ #
9
+ # alias_method_chain :foo, :feature
10
+ #
11
+ # And both aliases are set up for you.
12
+ #
13
+ # Query and bang methods (foo?, foo!) keep the same punctuation:
14
+ #
15
+ # alias_method_chain :foo?, :feature
16
+ #
17
+ # is equivalent to
18
+ #
19
+ # alias_method :foo_without_feature?, :foo?
20
+ # alias_method :foo?, :foo_with_feature?
21
+ #
22
+ # so you can safely chain foo, foo?, and foo! with the same feature.
23
+ def alias_method_chain(target, feature)
24
+ # Strip out punctuation on predicates or bang methods since
25
+ # e.g. target?_without_feature is not a valid method name.
26
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
27
+ yield(aliased_target, punctuation) if block_given?
28
+
29
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
30
+
31
+ alias_method without_method, target
32
+ alias_method target, with_method
33
+
34
+ case
35
+ when public_method_defined?(without_method)
36
+ public target
37
+ when protected_method_defined?(without_method)
38
+ protected target
39
+ when private_method_defined?(without_method)
40
+ private target
41
+ end
42
+ end
43
+
44
+ # Allows you to make aliases for attributes, which includes
45
+ # getter, setter, and query methods.
46
+ #
47
+ # Example:
48
+ #
49
+ # class Content < ActiveRecord::Base
50
+ # # has a title attribute
51
+ # end
52
+ #
53
+ # class Email < Content
54
+ # alias_attribute :subject, :title
55
+ # end
56
+ #
57
+ # e = Email.find(1)
58
+ # e.title # => "Superstars"
59
+ # e.subject # => "Superstars"
60
+ # e.subject? # => true
61
+ # e.subject = "Megastars"
62
+ # e.title # => "Megastars"
63
+ def alias_attribute(new_name, old_name)
64
+ module_eval <<-STR, __FILE__, __LINE__ + 1
65
+ def #{new_name}; self.#{old_name}; end # def subject; self.title; end
66
+ def #{new_name}?; self.#{old_name}?; end # def subject?; self.title?; end
67
+ def #{new_name}=(v); self.#{old_name} = v; end # def subject=(v); self.title = v; end
68
+ STR
69
+ end
70
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
3
+ class Module
4
+ # A module may or may not have a name.
5
+ #
6
+ # module M; end
7
+ # M.name # => "M"
8
+ #
9
+ # m = Module.new
10
+ # m.name # => ""
11
+ #
12
+ # A module gets a name when it is first assigned to a constant. Either
13
+ # via the +module+ or +class+ keyword or by an explicit assignment:
14
+ #
15
+ # m = Module.new # creates an anonymous module
16
+ # M = m # => m gets a name here as a side-effect
17
+ # m.name # => "M"
18
+ #
19
+ def anonymous?
20
+ # Uses blank? because the name of an anonymous class is an empty
21
+ # string in 1.8, and nil in 1.9.
22
+ name.blank?
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ class Module
2
+ # Declare an attribute accessor with an initial default return value.
3
+ #
4
+ # To give attribute <tt>:age</tt> the initial value <tt>25</tt>:
5
+ #
6
+ # class Person
7
+ # attr_accessor_with_default :age, 25
8
+ # end
9
+ #
10
+ # some_person.age
11
+ # => 25
12
+ # some_person.age = 26
13
+ # some_person.age
14
+ # => 26
15
+ #
16
+ # To give attribute <tt>:element_name</tt> a dynamic default value, evaluated
17
+ # in scope of self:
18
+ #
19
+ # attr_accessor_with_default(:element_name) { name.underscore }
20
+ #
21
+ def attr_accessor_with_default(sym, default = nil, &block)
22
+ raise 'Default value or block required' unless !default.nil? || block
23
+ define_method(sym, block_given? ? block : Proc.new { default })
24
+ module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
25
+ def #{sym}=(value) # def age=(value)
26
+ class << self; attr_reader :#{sym} end # class << self; attr_reader :age end
27
+ @#{sym} = value # @age = value
28
+ end # end
29
+ EVAL
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ class Module
2
+ # Declares an attribute reader backed by an internally-named instance variable.
3
+ def attr_internal_reader(*attrs)
4
+ attrs.each do |attr|
5
+ module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end", __FILE__, __LINE__
6
+ end
7
+ end
8
+
9
+ # Declares an attribute writer backed by an internally-named instance variable.
10
+ def attr_internal_writer(*attrs)
11
+ attrs.each do |attr|
12
+ module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end", __FILE__, __LINE__
13
+ end
14
+ end
15
+
16
+ # Declares an attribute reader and writer backed by an internally-named instance
17
+ # variable.
18
+ def attr_internal_accessor(*attrs)
19
+ attr_internal_reader(*attrs)
20
+ attr_internal_writer(*attrs)
21
+ end
22
+
23
+ alias_method :attr_internal, :attr_internal_accessor
24
+
25
+ class << self; attr_accessor :attr_internal_naming_format end
26
+ self.attr_internal_naming_format = '@_%s'
27
+
28
+ private
29
+ def attr_internal_ivar_name(attr)
30
+ Module.attr_internal_naming_format % attr
31
+ end
32
+ end
@@ -0,0 +1,66 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+
3
+ class Module
4
+ def mattr_reader(*syms)
5
+ options = syms.extract_options!
6
+ syms.each do |sym|
7
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
8
+ unless defined? @@#{sym}
9
+ @@#{sym} = nil
10
+ end
11
+
12
+ def self.#{sym}
13
+ @@#{sym}
14
+ end
15
+ EOS
16
+
17
+ unless options[:instance_reader] == false
18
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
19
+ def #{sym}
20
+ @@#{sym}
21
+ end
22
+ EOS
23
+ end
24
+ end
25
+ end
26
+
27
+ def mattr_writer(*syms)
28
+ options = syms.extract_options!
29
+ syms.each do |sym|
30
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
31
+ unless defined? @@#{sym}
32
+ @@#{sym} = nil
33
+ end
34
+
35
+ def self.#{sym}=(obj)
36
+ @@#{sym} = obj
37
+ end
38
+ EOS
39
+
40
+ unless options[:instance_writer] == false
41
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
42
+ def #{sym}=(obj)
43
+ @@#{sym} = obj
44
+ end
45
+ EOS
46
+ end
47
+ end
48
+ end
49
+
50
+ # Extends the module object with module and instance accessors for class attributes,
51
+ # just like the native attr* accessors for instance attributes.
52
+ #
53
+ # module AppConfiguration
54
+ # mattr_accessor :google_api_key
55
+ # self.google_api_key = "123456789"
56
+ #
57
+ # mattr_accessor :paypal_url
58
+ # self.paypal_url = "www.sandbox.paypal.com"
59
+ # end
60
+ #
61
+ # AppConfiguration.google_api_key = "overriding the api key!"
62
+ def mattr_accessor(*syms)
63
+ mattr_reader(*syms)
64
+ mattr_writer(*syms)
65
+ end
66
+ end
@@ -0,0 +1,146 @@
1
+ require "active_support/core_ext/module/remove_method"
2
+
3
+ class Module
4
+ # Provides a delegate class method to easily expose contained objects' methods
5
+ # as your own. Pass one or more methods (specified as symbols or strings)
6
+ # and the name of the target object via the <tt>:to</tt> option (also a symbol
7
+ # or string). At least one method and the <tt>:to</tt> option are required.
8
+ #
9
+ # Delegation is particularly useful with Active Record associations:
10
+ #
11
+ # class Greeter < ActiveRecord::Base
12
+ # def hello
13
+ # "hello"
14
+ # end
15
+ #
16
+ # def goodbye
17
+ # "goodbye"
18
+ # end
19
+ # end
20
+ #
21
+ # class Foo < ActiveRecord::Base
22
+ # belongs_to :greeter
23
+ # delegate :hello, :to => :greeter
24
+ # end
25
+ #
26
+ # Foo.new.hello # => "hello"
27
+ # Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
28
+ #
29
+ # Multiple delegates to the same target are allowed:
30
+ #
31
+ # class Foo < ActiveRecord::Base
32
+ # belongs_to :greeter
33
+ # delegate :hello, :goodbye, :to => :greeter
34
+ # end
35
+ #
36
+ # Foo.new.goodbye # => "goodbye"
37
+ #
38
+ # Methods can be delegated to instance variables, class variables, or constants
39
+ # by providing them as a symbols:
40
+ #
41
+ # class Foo
42
+ # CONSTANT_ARRAY = [0,1,2,3]
43
+ # @@class_array = [4,5,6,7]
44
+ #
45
+ # def initialize
46
+ # @instance_array = [8,9,10,11]
47
+ # end
48
+ # delegate :sum, :to => :CONSTANT_ARRAY
49
+ # delegate :min, :to => :@@class_array
50
+ # delegate :max, :to => :@instance_array
51
+ # end
52
+ #
53
+ # Foo.new.sum # => 6
54
+ # Foo.new.min # => 4
55
+ # Foo.new.max # => 11
56
+ #
57
+ # Delegates can optionally be prefixed using the <tt>:prefix</tt> option. If the value
58
+ # is <tt>true</tt>, the delegate methods are prefixed with the name of the object being
59
+ # delegated to.
60
+ #
61
+ # Person = Struct.new(:name, :address)
62
+ #
63
+ # class Invoice < Struct.new(:client)
64
+ # delegate :name, :address, :to => :client, :prefix => true
65
+ # end
66
+ #
67
+ # john_doe = Person.new("John Doe", "Vimmersvej 13")
68
+ # invoice = Invoice.new(john_doe)
69
+ # invoice.client_name # => "John Doe"
70
+ # invoice.client_address # => "Vimmersvej 13"
71
+ #
72
+ # It is also possible to supply a custom prefix.
73
+ #
74
+ # class Invoice < Struct.new(:client)
75
+ # delegate :name, :address, :to => :client, :prefix => :customer
76
+ # end
77
+ #
78
+ # invoice = Invoice.new(john_doe)
79
+ # invoice.customer_name # => "John Doe"
80
+ # invoice.customer_address # => "Vimmersvej 13"
81
+ #
82
+ # If the delegate object is +nil+ an exception is raised, and that happens
83
+ # no matter whether +nil+ responds to the delegated method. You can get a
84
+ # +nil+ instead with the +:allow_nil+ option.
85
+ #
86
+ # class Foo
87
+ # attr_accessor :bar
88
+ # def initialize(bar = nil)
89
+ # @bar = bar
90
+ # end
91
+ # delegate :zoo, :to => :bar
92
+ # end
93
+ #
94
+ # Foo.new.zoo # raises NoMethodError exception (you called nil.zoo)
95
+ #
96
+ # class Foo
97
+ # attr_accessor :bar
98
+ # def initialize(bar = nil)
99
+ # @bar = bar
100
+ # end
101
+ # delegate :zoo, :to => :bar, :allow_nil => true
102
+ # end
103
+ #
104
+ # Foo.new.zoo # returns nil
105
+ #
106
+ def delegate(*methods)
107
+ options = methods.pop
108
+ unless options.is_a?(Hash) && to = options[:to]
109
+ raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
110
+ end
111
+
112
+ if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
113
+ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
114
+ end
115
+
116
+ prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
117
+
118
+ file, line = caller.first.split(':', 2)
119
+ line = line.to_i
120
+
121
+ methods.each do |method|
122
+ on_nil =
123
+ if options[:allow_nil]
124
+ 'return'
125
+ else
126
+ %(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
127
+ end
128
+
129
+ module_eval(<<-EOS, file, line - 5)
130
+ if instance_methods(false).map(&:to_s).include?("#{prefix}#{method}")
131
+ remove_possible_method("#{prefix}#{method}")
132
+ end
133
+
134
+ def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
135
+ #{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
136
+ rescue NoMethodError # rescue NoMethodError
137
+ if #{to}.nil? # if client.nil?
138
+ #{on_nil} # return # depends on :allow_nil
139
+ else # else
140
+ raise # raise
141
+ end # end
142
+ end # end
143
+ EOS
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,9 @@
1
+ class Module
2
+ # Declare that a method has been deprecated.
3
+ # deprecate :foo
4
+ # deprecate :bar => 'message'
5
+ # deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!'
6
+ def deprecate(*method_names)
7
+ ActiveSupport::Deprecation.deprecate_methods(self, *method_names)
8
+ end
9
+ end
@@ -0,0 +1,88 @@
1
+ require 'active_support/inflector'
2
+
3
+ class Module
4
+ # Returns the name of the module containing this one.
5
+ #
6
+ # M::N.parent_name # => "M"
7
+ def parent_name
8
+ unless defined? @parent_name
9
+ @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
10
+ end
11
+ @parent_name
12
+ end
13
+
14
+ # Returns the module which contains this one according to its name.
15
+ #
16
+ # module M
17
+ # module N
18
+ # end
19
+ # end
20
+ # X = M::N
21
+ #
22
+ # M::N.parent # => M
23
+ # X.parent # => M
24
+ #
25
+ # The parent of top-level and anonymous modules is Object.
26
+ #
27
+ # M.parent # => Object
28
+ # Module.new.parent # => Object
29
+ #
30
+ def parent
31
+ parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
32
+ end
33
+
34
+ # Returns all the parents of this module according to its name, ordered from
35
+ # nested outwards. The receiver is not contained within the result.
36
+ #
37
+ # module M
38
+ # module N
39
+ # end
40
+ # end
41
+ # X = M::N
42
+ #
43
+ # M.parents # => [Object]
44
+ # M::N.parents # => [M, Object]
45
+ # X.parents # => [M, Object]
46
+ #
47
+ def parents
48
+ parents = []
49
+ if parent_name
50
+ parts = parent_name.split('::')
51
+ until parts.empty?
52
+ parents << ActiveSupport::Inflector.constantize(parts * '::')
53
+ parts.pop
54
+ end
55
+ end
56
+ parents << Object unless parents.include? Object
57
+ parents
58
+ end
59
+
60
+ if RUBY_VERSION < '1.9'
61
+ # Returns the constants that have been defined locally by this object and
62
+ # not in an ancestor. This method is exact if running under Ruby 1.9. In
63
+ # previous versions it may miss some constants if their definition in some
64
+ # ancestor is identical to their definition in the receiver.
65
+ def local_constants
66
+ inherited = {}
67
+
68
+ ancestors.each do |anc|
69
+ next if anc == self
70
+ anc.constants.each { |const| inherited[const] = anc.const_get(const) }
71
+ end
72
+
73
+ constants.select do |const|
74
+ !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
75
+ end
76
+ end
77
+ else
78
+ def local_constants #:nodoc:
79
+ constants(false)
80
+ end
81
+ end
82
+
83
+ # Returns the names of the constants defined locally rather than the
84
+ # constants themselves. See <tt>local_constants</tt>.
85
+ def local_constant_names
86
+ local_constants.map { |c| c.to_s }
87
+ end
88
+ end