monetra-ruby 0.0.6
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/Rakefile +35 -0
- data/lib/monetra.rb +239 -0
- data/lib/monetra/active_support.rb +42 -0
- data/lib/monetra/active_support/binding_of_caller.rb +84 -0
- data/lib/monetra/active_support/breakpoint.rb +523 -0
- data/lib/monetra/active_support/caching_tools.rb +62 -0
- data/lib/monetra/active_support/clean_logger.rb +38 -0
- data/lib/monetra/active_support/core_ext.rb +1 -0
- data/lib/monetra/active_support/core_ext/array.rb +7 -0
- data/lib/monetra/active_support/core_ext/array/conversions.rb +72 -0
- data/lib/monetra/active_support/core_ext/array/grouping.rb +46 -0
- data/lib/monetra/active_support/core_ext/bigdecimal.rb +3 -0
- data/lib/monetra/active_support/core_ext/bigdecimal/formatting.rb +7 -0
- data/lib/monetra/active_support/core_ext/blank.rb +50 -0
- data/lib/monetra/active_support/core_ext/cgi.rb +5 -0
- data/lib/monetra/active_support/core_ext/cgi/escape_skipping_slashes.rb +14 -0
- data/lib/monetra/active_support/core_ext/class.rb +3 -0
- data/lib/monetra/active_support/core_ext/class/attribute_accessors.rb +44 -0
- data/lib/monetra/active_support/core_ext/class/inheritable_attributes.rb +115 -0
- data/lib/monetra/active_support/core_ext/class/removal.rb +24 -0
- data/lib/monetra/active_support/core_ext/date.rb +6 -0
- data/lib/monetra/active_support/core_ext/date/conversions.rb +39 -0
- data/lib/monetra/active_support/core_ext/enumerable.rb +62 -0
- data/lib/monetra/active_support/core_ext/exception.rb +33 -0
- data/lib/monetra/active_support/core_ext/hash.rb +13 -0
- data/lib/monetra/active_support/core_ext/hash/conversions.rb +148 -0
- data/lib/monetra/active_support/core_ext/hash/diff.rb +11 -0
- data/lib/monetra/active_support/core_ext/hash/indifferent_access.rb +88 -0
- data/lib/monetra/active_support/core_ext/hash/keys.rb +53 -0
- data/lib/monetra/active_support/core_ext/hash/reverse_merge.rb +25 -0
- data/lib/monetra/active_support/core_ext/integer.rb +7 -0
- data/lib/monetra/active_support/core_ext/integer/even_odd.rb +24 -0
- data/lib/monetra/active_support/core_ext/integer/inflections.rb +15 -0
- data/lib/monetra/active_support/core_ext/kernel.rb +4 -0
- data/lib/monetra/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/monetra/active_support/core_ext/kernel/daemonizing.rb +15 -0
- data/lib/monetra/active_support/core_ext/kernel/reporting.rb +51 -0
- data/lib/monetra/active_support/core_ext/kernel/requires.rb +24 -0
- data/lib/monetra/active_support/core_ext/load_error.rb +38 -0
- data/lib/monetra/active_support/core_ext/logger.rb +16 -0
- data/lib/monetra/active_support/core_ext/module.rb +7 -0
- data/lib/monetra/active_support/core_ext/module/aliasing.rb +57 -0
- data/lib/monetra/active_support/core_ext/module/attr_internal.rb +31 -0
- data/lib/monetra/active_support/core_ext/module/attribute_accessors.rb +44 -0
- data/lib/monetra/active_support/core_ext/module/delegation.rb +41 -0
- data/lib/monetra/active_support/core_ext/module/inclusion.rb +11 -0
- data/lib/monetra/active_support/core_ext/module/introspection.rb +21 -0
- data/lib/monetra/active_support/core_ext/module/loading.rb +13 -0
- data/lib/monetra/active_support/core_ext/name_error.rb +20 -0
- data/lib/monetra/active_support/core_ext/numeric.rb +7 -0
- data/lib/monetra/active_support/core_ext/numeric/bytes.rb +44 -0
- data/lib/monetra/active_support/core_ext/numeric/time.rb +72 -0
- data/lib/monetra/active_support/core_ext/object.rb +2 -0
- data/lib/monetra/active_support/core_ext/object/extending.rb +47 -0
- data/lib/monetra/active_support/core_ext/object/misc.rb +34 -0
- data/lib/monetra/active_support/core_ext/pathname.rb +7 -0
- data/lib/monetra/active_support/core_ext/pathname/clean_within.rb +14 -0
- data/lib/monetra/active_support/core_ext/proc.rb +12 -0
- data/lib/monetra/active_support/core_ext/range.rb +5 -0
- data/lib/monetra/active_support/core_ext/range/conversions.rb +21 -0
- data/lib/monetra/active_support/core_ext/string.rb +13 -0
- data/lib/monetra/active_support/core_ext/string/access.rb +58 -0
- data/lib/monetra/active_support/core_ext/string/conversions.rb +19 -0
- data/lib/monetra/active_support/core_ext/string/inflections.rb +153 -0
- data/lib/monetra/active_support/core_ext/string/iterators.rb +17 -0
- data/lib/monetra/active_support/core_ext/string/starts_ends_with.rb +20 -0
- data/lib/monetra/active_support/core_ext/symbol.rb +12 -0
- data/lib/monetra/active_support/core_ext/time.rb +7 -0
- data/lib/monetra/active_support/core_ext/time/calculations.rb +188 -0
- data/lib/monetra/active_support/core_ext/time/conversions.rb +36 -0
- data/lib/monetra/active_support/dependencies.rb +187 -0
- data/lib/monetra/active_support/deprecation.rb +106 -0
- data/lib/monetra/active_support/inflections.rb +53 -0
- data/lib/monetra/active_support/inflector.rb +179 -0
- data/lib/monetra/active_support/json.rb +37 -0
- data/lib/monetra/active_support/json/encoders.rb +25 -0
- data/lib/monetra/active_support/json/encoders/core.rb +65 -0
- data/lib/monetra/active_support/option_merger.rb +25 -0
- data/lib/monetra/active_support/ordered_options.rb +50 -0
- data/lib/monetra/active_support/reloadable.rb +30 -0
- data/lib/monetra/active_support/values/time_zone.rb +180 -0
- data/lib/monetra/active_support/vendor/builder.rb +13 -0
- data/lib/monetra/active_support/vendor/builder/blankslate.rb +63 -0
- data/lib/monetra/active_support/vendor/builder/xchar.rb +112 -0
- data/lib/monetra/active_support/vendor/builder/xmlbase.rb +145 -0
- data/lib/monetra/active_support/vendor/builder/xmlevents.rb +63 -0
- data/lib/monetra/active_support/vendor/builder/xmlmarkup.rb +328 -0
- data/lib/monetra/active_support/vendor/flexmock.rb +84 -0
- data/lib/monetra/active_support/vendor/xml_simple.rb +1019 -0
- data/lib/monetra/active_support/version.rb +9 -0
- data/lib/monetra/active_support/whiny_nil.rb +38 -0
- data/test/test.rb +21 -0
- metadata +167 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class Module
|
|
2
|
+
# Return the module which contains this one; if this is a root module, such as
|
|
3
|
+
# +::MyModule+, then Object is returned.
|
|
4
|
+
def parent
|
|
5
|
+
parent_name = name.split('::')[0..-2] * '::'
|
|
6
|
+
parent_name.empty? ? Object : parent_name.constantize
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Return all the parents of this module, ordered from nested outwards. The
|
|
10
|
+
# receiver is not contained within the result.
|
|
11
|
+
def parents
|
|
12
|
+
parents = []
|
|
13
|
+
parts = name.split('::')[0..-2]
|
|
14
|
+
until parts.empty?
|
|
15
|
+
parents << (parts * '::').constantize
|
|
16
|
+
parts.pop
|
|
17
|
+
end
|
|
18
|
+
parents << Object unless parents.include? Object
|
|
19
|
+
parents
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
# Add a +missing_name+ method to NameError instances.
|
|
3
|
+
class NameError < StandardError
|
|
4
|
+
|
|
5
|
+
# Add a method to obtain the missing name from a NameError.
|
|
6
|
+
def missing_name
|
|
7
|
+
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Was this exception raised because the given name was missing?
|
|
11
|
+
def missing_name?(name)
|
|
12
|
+
if name.is_a? Symbol
|
|
13
|
+
last_name = (missing_name || '').split('::').last
|
|
14
|
+
last_name == name.to_s
|
|
15
|
+
else
|
|
16
|
+
missing_name == name.to_s
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module ActiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Numeric #:nodoc:
|
|
4
|
+
# Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
|
|
5
|
+
module Bytes
|
|
6
|
+
def bytes
|
|
7
|
+
self
|
|
8
|
+
end
|
|
9
|
+
alias :byte :bytes
|
|
10
|
+
|
|
11
|
+
def kilobytes
|
|
12
|
+
self * 1024
|
|
13
|
+
end
|
|
14
|
+
alias :kilobyte :kilobytes
|
|
15
|
+
|
|
16
|
+
def megabytes
|
|
17
|
+
self * 1024.kilobytes
|
|
18
|
+
end
|
|
19
|
+
alias :megabyte :megabytes
|
|
20
|
+
|
|
21
|
+
def gigabytes
|
|
22
|
+
self * 1024.megabytes
|
|
23
|
+
end
|
|
24
|
+
alias :gigabyte :gigabytes
|
|
25
|
+
|
|
26
|
+
def terabytes
|
|
27
|
+
self * 1024.gigabytes
|
|
28
|
+
end
|
|
29
|
+
alias :terabyte :terabytes
|
|
30
|
+
|
|
31
|
+
def petabytes
|
|
32
|
+
self * 1024.terabytes
|
|
33
|
+
end
|
|
34
|
+
alias :petabyte :petabytes
|
|
35
|
+
|
|
36
|
+
def exabytes
|
|
37
|
+
self * 1024.petabytes
|
|
38
|
+
end
|
|
39
|
+
alias :exabyte :exabytes
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module ActiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Numeric #:nodoc:
|
|
4
|
+
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
|
5
|
+
#
|
|
6
|
+
# If you need precise date calculations that doesn't just treat months as 30 days, then have
|
|
7
|
+
# a look at Time#advance.
|
|
8
|
+
#
|
|
9
|
+
# Some of these methods are approximations, Ruby's core
|
|
10
|
+
# Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
|
|
11
|
+
# Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
|
|
12
|
+
# date and time arithmetic
|
|
13
|
+
module Time
|
|
14
|
+
def seconds
|
|
15
|
+
self
|
|
16
|
+
end
|
|
17
|
+
alias :second :seconds
|
|
18
|
+
|
|
19
|
+
def minutes
|
|
20
|
+
self * 60
|
|
21
|
+
end
|
|
22
|
+
alias :minute :minutes
|
|
23
|
+
|
|
24
|
+
def hours
|
|
25
|
+
self * 60.minutes
|
|
26
|
+
end
|
|
27
|
+
alias :hour :hours
|
|
28
|
+
|
|
29
|
+
def days
|
|
30
|
+
self * 24.hours
|
|
31
|
+
end
|
|
32
|
+
alias :day :days
|
|
33
|
+
|
|
34
|
+
def weeks
|
|
35
|
+
self * 7.days
|
|
36
|
+
end
|
|
37
|
+
alias :week :weeks
|
|
38
|
+
|
|
39
|
+
def fortnights
|
|
40
|
+
self * 2.weeks
|
|
41
|
+
end
|
|
42
|
+
alias :fortnight :fortnights
|
|
43
|
+
|
|
44
|
+
def months
|
|
45
|
+
self * 30.days
|
|
46
|
+
end
|
|
47
|
+
alias :month :months
|
|
48
|
+
|
|
49
|
+
def years
|
|
50
|
+
(self * 365.25.days).to_i
|
|
51
|
+
end
|
|
52
|
+
alias :year :years
|
|
53
|
+
|
|
54
|
+
# Reads best without arguments: 10.minutes.ago
|
|
55
|
+
def ago(time = ::Time.now)
|
|
56
|
+
time - self
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Reads best with argument: 10.minutes.until(time)
|
|
60
|
+
alias :until :ago
|
|
61
|
+
|
|
62
|
+
# Reads best with argument: 10.minutes.since(time)
|
|
63
|
+
def since(time = ::Time.now)
|
|
64
|
+
time + self
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Reads best without arguments: 10.minutes.from_now
|
|
68
|
+
alias :from_now :since
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
class Object #:nodoc:
|
|
2
|
+
def remove_subclasses_of(*superclasses)
|
|
3
|
+
Class.remove_class(*subclasses_of(*superclasses))
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def subclasses_of(*superclasses)
|
|
7
|
+
subclasses = []
|
|
8
|
+
ObjectSpace.each_object(Class) do |k|
|
|
9
|
+
next if # Exclude this class if
|
|
10
|
+
(k.ancestors & superclasses).empty? || # It's not a subclass of our supers
|
|
11
|
+
superclasses.include?(k) || # It *is* one of the supers
|
|
12
|
+
eval("! defined?(::#{k})") || # It's not defined.
|
|
13
|
+
eval("::#{k}").object_id != k.object_id
|
|
14
|
+
subclasses << k
|
|
15
|
+
end
|
|
16
|
+
subclasses
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def extended_by
|
|
20
|
+
ancestors = class << self; ancestors end
|
|
21
|
+
ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def copy_instance_variables_from(object, exclude = [])
|
|
25
|
+
exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables
|
|
26
|
+
|
|
27
|
+
instance_variables = object.instance_variables - exclude.map { |name| name.to_s }
|
|
28
|
+
instance_variables.each { |name| instance_variable_set(name, object.instance_variable_get(name)) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def extend_with_included_modules_from(object)
|
|
32
|
+
object.extended_by.each { |mod| extend mod }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def instance_values
|
|
36
|
+
instance_variables.inject({}) do |values, name|
|
|
37
|
+
values[name[1..-1]] = instance_variable_get(name)
|
|
38
|
+
values
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
unless defined? instance_exec # 1.9
|
|
43
|
+
def instance_exec(*arguments, &block)
|
|
44
|
+
block.bind(self)[*arguments]
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
class Object #:nodoc:
|
|
2
|
+
# A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
|
|
3
|
+
#
|
|
4
|
+
# def foo
|
|
5
|
+
# returning values = [] do
|
|
6
|
+
# values << 'bar'
|
|
7
|
+
# values << 'baz'
|
|
8
|
+
# end
|
|
9
|
+
# end
|
|
10
|
+
#
|
|
11
|
+
# foo # => ['bar', 'baz']
|
|
12
|
+
#
|
|
13
|
+
# def foo
|
|
14
|
+
# returning [] do |values|
|
|
15
|
+
# values << 'bar'
|
|
16
|
+
# values << 'baz'
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# foo # => ['bar', 'baz']
|
|
21
|
+
#
|
|
22
|
+
def returning(value)
|
|
23
|
+
yield(value)
|
|
24
|
+
value
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def with_options(options)
|
|
28
|
+
yield ActiveSupport::OptionMerger.new(self, options)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_json
|
|
32
|
+
ActiveSupport::JSON.encode(self)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module ActiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Pathname #:nodoc:
|
|
4
|
+
module CleanWithin
|
|
5
|
+
# Clean the paths contained in the provided string.
|
|
6
|
+
def clean_within(string)
|
|
7
|
+
string.gsub(%r{[\w. ]+(/[\w. ]+)+(\.rb)?(\b|$)}) do |path|
|
|
8
|
+
new(path).cleanpath
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class Proc #:nodoc:
|
|
2
|
+
def bind(object)
|
|
3
|
+
block, time = self, Time.now
|
|
4
|
+
(class << object; self end).class_eval do
|
|
5
|
+
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
|
6
|
+
define_method(method_name, &block)
|
|
7
|
+
method = instance_method(method_name)
|
|
8
|
+
remove_method(method_name)
|
|
9
|
+
method
|
|
10
|
+
end.bind(object)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module ActiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module Range #:nodoc:
|
|
4
|
+
# Getting dates in different convenient string representations and other objects
|
|
5
|
+
module Conversions
|
|
6
|
+
DATE_FORMATS = {
|
|
7
|
+
:db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
def self.included(klass) #:nodoc:
|
|
11
|
+
klass.send(:alias_method, :to_default_s, :to_s)
|
|
12
|
+
klass.send(:alias_method, :to_s, :to_formatted_s)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def to_formatted_s(format = :default)
|
|
16
|
+
DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/string/inflections'
|
|
2
|
+
require File.dirname(__FILE__) + '/string/conversions'
|
|
3
|
+
require File.dirname(__FILE__) + '/string/access'
|
|
4
|
+
require File.dirname(__FILE__) + '/string/starts_ends_with'
|
|
5
|
+
require File.dirname(__FILE__) + '/string/iterators'
|
|
6
|
+
|
|
7
|
+
class String #:nodoc:
|
|
8
|
+
include ActiveSupport::CoreExtensions::String::Access
|
|
9
|
+
include ActiveSupport::CoreExtensions::String::Conversions
|
|
10
|
+
include ActiveSupport::CoreExtensions::String::Inflections
|
|
11
|
+
include ActiveSupport::CoreExtensions::String::StartsEndsWith
|
|
12
|
+
include ActiveSupport::CoreExtensions::String::Iterators
|
|
13
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module ActiveSupport #:nodoc:
|
|
2
|
+
module CoreExtensions #:nodoc:
|
|
3
|
+
module String #:nodoc:
|
|
4
|
+
# Makes it easier to access parts of a string, such as specific characters and substrings.
|
|
5
|
+
module Access
|
|
6
|
+
# Returns the character at the +position+ treating the string as an array (where 0 is the first character).
|
|
7
|
+
#
|
|
8
|
+
# Examples:
|
|
9
|
+
# "hello".at(0) # => "h"
|
|
10
|
+
# "hello".at(4) # => "o"
|
|
11
|
+
# "hello".at(10) # => nil
|
|
12
|
+
def at(position)
|
|
13
|
+
self[position, 1]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character).
|
|
17
|
+
#
|
|
18
|
+
# Examples:
|
|
19
|
+
# "hello".from(0) # => "hello"
|
|
20
|
+
# "hello".from(2) # => "llo"
|
|
21
|
+
# "hello".from(10) # => nil
|
|
22
|
+
def from(position)
|
|
23
|
+
self[position..-1]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character).
|
|
27
|
+
#
|
|
28
|
+
# Examples:
|
|
29
|
+
# "hello".to(0) # => "h"
|
|
30
|
+
# "hello".to(2) # => "hel"
|
|
31
|
+
# "hello".to(10) # => "hello"
|
|
32
|
+
def to(position)
|
|
33
|
+
self[0..position]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Returns the first character of the string or the first +limit+ characters.
|
|
37
|
+
#
|
|
38
|
+
# Examples:
|
|
39
|
+
# "hello".first # => "h"
|
|
40
|
+
# "hello".first(2) # => "he"
|
|
41
|
+
# "hello".first(10) # => "hello"
|
|
42
|
+
def first(limit = 1)
|
|
43
|
+
self[0..(limit - 1)]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Returns the last character of the string or the last +limit+ characters.
|
|
47
|
+
#
|
|
48
|
+
# Examples:
|
|
49
|
+
# "hello".last # => "o"
|
|
50
|
+
# "hello".last(2) # => "lo"
|
|
51
|
+
# "hello".last(10) # => "hello"
|
|
52
|
+
def last(limit = 1)
|
|
53
|
+
self[(-limit)..-1] || self
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'parsedate'
|
|
2
|
+
|
|
3
|
+
module ActiveSupport #:nodoc:
|
|
4
|
+
module CoreExtensions #:nodoc:
|
|
5
|
+
module String #:nodoc:
|
|
6
|
+
# Converting strings to other objects
|
|
7
|
+
module Conversions
|
|
8
|
+
# Form can be either :utc (default) or :local.
|
|
9
|
+
def to_time(form = :utc)
|
|
10
|
+
::Time.send(form, *ParseDate.parsedate(self))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def to_date
|
|
14
|
+
::Date.new(*ParseDate.parsedate(self)[0..2])
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../../inflector' unless defined? Inflector
|
|
2
|
+
|
|
3
|
+
module ActiveSupport #:nodoc:
|
|
4
|
+
module CoreExtensions #:nodoc:
|
|
5
|
+
module String #:nodoc:
|
|
6
|
+
# String inflections define new methods on the String class to transform names for different purposes.
|
|
7
|
+
# For instance, you can figure out the name of a database from the name of a class.
|
|
8
|
+
# "ScaleScore".tableize => "scale_scores"
|
|
9
|
+
module Inflections
|
|
10
|
+
# Returns the plural form of the word in the string.
|
|
11
|
+
#
|
|
12
|
+
# Examples
|
|
13
|
+
# "post".pluralize #=> "posts"
|
|
14
|
+
# "octopus".pluralize #=> "octopi"
|
|
15
|
+
# "sheep".pluralize #=> "sheep"
|
|
16
|
+
# "words".pluralize #=> "words"
|
|
17
|
+
# "the blue mailman".pluralize #=> "the blue mailmen"
|
|
18
|
+
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
|
19
|
+
def pluralize
|
|
20
|
+
Inflector.pluralize(self)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The reverse of pluralize, returns the singular form of a word in a string.
|
|
24
|
+
#
|
|
25
|
+
# Examples
|
|
26
|
+
# "posts".singularize #=> "post"
|
|
27
|
+
# "octopi".singularize #=> "octopus"
|
|
28
|
+
# "sheep".singluarize #=> "sheep"
|
|
29
|
+
# "word".singluarize #=> "word"
|
|
30
|
+
# "the blue mailmen".singularize #=> "the blue mailman"
|
|
31
|
+
# "CamelOctopi".singularize #=> "CamelOctopus"
|
|
32
|
+
def singularize
|
|
33
|
+
Inflector.singularize(self)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# By default, camelize converts strings to UpperCamelCase. If the argument to camelize
|
|
37
|
+
# is set to ":lower" then camelize produces lowerCamelCase.
|
|
38
|
+
#
|
|
39
|
+
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
|
40
|
+
#
|
|
41
|
+
# Examples
|
|
42
|
+
# "active_record".camelize #=> "ActiveRecord"
|
|
43
|
+
# "active_record".camelize(:lower) #=> "activeRecord"
|
|
44
|
+
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
|
45
|
+
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
|
|
46
|
+
def camelize(first_letter = :upper)
|
|
47
|
+
case first_letter
|
|
48
|
+
when :upper then Inflector.camelize(self, true)
|
|
49
|
+
when :lower then Inflector.camelize(self, false)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
alias_method :camelcase, :camelize
|
|
53
|
+
|
|
54
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
|
55
|
+
# a nicer looking title. Titleize is meant for creating pretty output. It is not
|
|
56
|
+
# used in the Rails internals.
|
|
57
|
+
#
|
|
58
|
+
# titleize is also aliased as as titlecase
|
|
59
|
+
#
|
|
60
|
+
# Examples
|
|
61
|
+
# "man from the boondocks".titleize #=> "Man From The Boondocks"
|
|
62
|
+
# "x-men: the last stand".titleize #=> "X Men: The Last Stand"
|
|
63
|
+
def titleize
|
|
64
|
+
Inflector.titleize(self)
|
|
65
|
+
end
|
|
66
|
+
alias_method :titlecase, :titleize
|
|
67
|
+
|
|
68
|
+
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
|
|
69
|
+
#
|
|
70
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
|
71
|
+
#
|
|
72
|
+
# Examples
|
|
73
|
+
# "ActiveRecord".underscore #=> "active_record"
|
|
74
|
+
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
|
75
|
+
def underscore
|
|
76
|
+
Inflector.underscore(self)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Replaces underscores with dashes in the string.
|
|
80
|
+
#
|
|
81
|
+
# Example
|
|
82
|
+
# "puni_puni" #=> "puni-puni"
|
|
83
|
+
def dasherize
|
|
84
|
+
Inflector.dasherize(self)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Removes the module part from the expression in the string
|
|
88
|
+
#
|
|
89
|
+
# Examples
|
|
90
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
|
91
|
+
# "Inflections".demodulize #=> "Inflections"
|
|
92
|
+
def demodulize
|
|
93
|
+
Inflector.demodulize(self)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Create the name of a table like Rails does for models to table names. This method
|
|
97
|
+
# uses the pluralize method on the last word in the string.
|
|
98
|
+
#
|
|
99
|
+
# Examples
|
|
100
|
+
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
|
101
|
+
# "egg_and_ham".tableize #=> "egg_and_hams"
|
|
102
|
+
# "fancyCategory".tableize #=> "fancy_categories"
|
|
103
|
+
def tableize
|
|
104
|
+
Inflector.tableize(self)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Create a class name from a table name like Rails does for table names to models.
|
|
108
|
+
# Note that this returns a string and not a Class. (To convert to an actual class
|
|
109
|
+
# follow classify with constantize.)
|
|
110
|
+
#
|
|
111
|
+
# Examples
|
|
112
|
+
# "egg_and_hams".classify #=> "EggAndHam"
|
|
113
|
+
# "post".classify #=> "Post"
|
|
114
|
+
def classify
|
|
115
|
+
Inflector.classify(self)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
|
119
|
+
# Like titleize, this is meant for creating pretty output.
|
|
120
|
+
#
|
|
121
|
+
# Examples
|
|
122
|
+
# "employee_salary" #=> "Employee salary"
|
|
123
|
+
# "author_id" #=> "Author"
|
|
124
|
+
def humanize
|
|
125
|
+
Inflector.humanize(self)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Creates a foreign key name from a class name.
|
|
129
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
|
130
|
+
# the method should put '_' between the name and 'id'.
|
|
131
|
+
#
|
|
132
|
+
# Examples
|
|
133
|
+
# "Message".foreign_key #=> "message_id"
|
|
134
|
+
# "Message".foreign_key(false) #=> "messageid"
|
|
135
|
+
# "Admin::Post".foreign_key #=> "post_id"
|
|
136
|
+
def foreign_key(separate_class_name_and_id_with_underscore = true)
|
|
137
|
+
Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Constantize tries to find a declared constant with the name specified
|
|
141
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
|
142
|
+
# or is not initialized.
|
|
143
|
+
#
|
|
144
|
+
# Examples
|
|
145
|
+
# "Module".constantize #=> Module
|
|
146
|
+
# "Class".constantize #=> Class
|
|
147
|
+
def constantize
|
|
148
|
+
Inflector.constantize(self)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|