activesupport-refinements 0.0.1
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 +17 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +1 -0
- data/activesupport-refinements.gemspec +21 -0
- data/lib/active_support/refinements/core_ext/array.rb +7 -0
- data/lib/active_support/refinements/core_ext/array/access.rb +56 -0
- data/lib/active_support/refinements/core_ext/array/conversions.rb +224 -0
- data/lib/active_support/refinements/core_ext/array/extract_options.rb +31 -0
- data/lib/active_support/refinements/core_ext/array/grouping.rb +101 -0
- data/lib/active_support/refinements/core_ext/array/prepend_and_append.rb +9 -0
- data/lib/active_support/refinements/core_ext/array/uniq_by.rb +21 -0
- data/lib/active_support/refinements/core_ext/array/wrap.rb +48 -0
- data/lib/active_support/refinements/core_ext/benchmark.rb +7 -0
- data/lib/active_support/refinements/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/refinements/core_ext/big_decimal/conversions.rb +32 -0
- data/lib/active_support/refinements/core_ext/class.rb +4 -0
- data/lib/active_support/refinements/core_ext/class/attribute.rb +119 -0
- data/lib/active_support/refinements/core_ext/class/attribute_accessors.rb +172 -0
- data/lib/active_support/refinements/core_ext/class/delegating_attributes.rb +42 -0
- data/lib/active_support/refinements/core_ext/class/subclasses.rb +44 -0
- data/lib/active_support/refinements/core_ext/date.rb +5 -0
- data/lib/active_support/refinements/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/refinements/core_ext/date/calculations.rb +123 -0
- data/lib/active_support/refinements/core_ext/date/conversions.rb +86 -0
- data/lib/active_support/refinements/core_ext/date/zones.rb +17 -0
- data/lib/active_support/refinements/core_ext/date_and_time/calculations.rb +232 -0
- data/lib/active_support/refinements/core_ext/date_time.rb +4 -0
- data/lib/active_support/refinements/core_ext/date_time/acts_like.rb +15 -0
- data/lib/active_support/refinements/core_ext/date_time/calculations.rb +143 -0
- data/lib/active_support/refinements/core_ext/date_time/conversions.rb +93 -0
- data/lib/active_support/refinements/core_ext/date_time/zones.rb +26 -0
- data/lib/active_support/refinements/core_ext/enumerable.rb +82 -0
- data/lib/active_support/refinements/core_ext/exception.rb +5 -0
- data/lib/active_support/refinements/core_ext/file.rb +1 -0
- data/lib/active_support/refinements/core_ext/file/atomic.rb +60 -0
- data/lib/active_support/refinements/core_ext/hash.rb +8 -0
- data/lib/active_support/refinements/core_ext/hash/conversions.rb +161 -0
- data/lib/active_support/refinements/core_ext/hash/deep_merge.rb +29 -0
- data/lib/active_support/refinements/core_ext/hash/diff.rb +15 -0
- data/lib/active_support/refinements/core_ext/hash/except.rb +17 -0
- data/lib/active_support/refinements/core_ext/hash/indifferent_access.rb +24 -0
- data/lib/active_support/refinements/core_ext/hash/keys.rb +140 -0
- data/lib/active_support/refinements/core_ext/hash/reverse_merge.rb +24 -0
- data/lib/active_support/refinements/core_ext/hash/slice.rb +42 -0
- data/lib/active_support/refinements/core_ext/integer.rb +3 -0
- data/lib/active_support/refinements/core_ext/integer/inflections.rb +31 -0
- data/lib/active_support/refinements/core_ext/integer/multiple.rb +12 -0
- data/lib/active_support/refinements/core_ext/integer/time.rb +43 -0
- data/lib/active_support/refinements/core_ext/kernel.rb +4 -0
- data/lib/active_support/refinements/core_ext/kernel/agnostics.rb +13 -0
- data/lib/active_support/refinements/core_ext/kernel/debugger.rb +12 -0
- data/lib/active_support/refinements/core_ext/kernel/reporting.rb +97 -0
- data/lib/active_support/refinements/core_ext/kernel/singleton_class.rb +8 -0
- data/lib/active_support/refinements/core_ext/load_error.rb +27 -0
- data/lib/active_support/refinements/core_ext/logger.rb +86 -0
- data/lib/active_support/refinements/core_ext/module.rb +10 -0
- data/lib/active_support/refinements/core_ext/module/aliasing.rb +69 -0
- data/lib/active_support/refinements/core_ext/module/anonymous.rb +21 -0
- data/lib/active_support/refinements/core_ext/module/attr_internal.rb +40 -0
- data/lib/active_support/refinements/core_ext/module/attribute_accessors.rb +68 -0
- data/lib/active_support/refinements/core_ext/module/delegation.rb +172 -0
- data/lib/active_support/refinements/core_ext/module/deprecation.rb +27 -0
- data/lib/active_support/refinements/core_ext/module/introspection.rb +80 -0
- data/lib/active_support/refinements/core_ext/module/qualified_const.rb +54 -0
- data/lib/active_support/refinements/core_ext/module/reachable.rb +10 -0
- data/lib/active_support/refinements/core_ext/module/remove_method.rb +14 -0
- data/lib/active_support/refinements/core_ext/name_error.rb +20 -0
- data/lib/active_support/refinements/core_ext/numeric.rb +3 -0
- data/lib/active_support/refinements/core_ext/numeric/bytes.rb +46 -0
- data/lib/active_support/refinements/core_ext/numeric/conversions.rb +137 -0
- data/lib/active_support/refinements/core_ext/numeric/time.rb +81 -0
- data/lib/active_support/refinements/core_ext/object.rb +14 -0
- data/lib/active_support/refinements/core_ext/object/acts_like.rb +12 -0
- data/lib/active_support/refinements/core_ext/object/blank.rb +107 -0
- data/lib/active_support/refinements/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/refinements/core_ext/object/deep_dup.rb +48 -0
- data/lib/active_support/refinements/core_ext/object/duplicable.rb +92 -0
- data/lib/active_support/refinements/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/refinements/core_ext/object/instance_variables.rb +30 -0
- data/lib/active_support/refinements/core_ext/object/to_json.rb +27 -0
- data/lib/active_support/refinements/core_ext/object/to_param.rb +60 -0
- data/lib/active_support/refinements/core_ext/object/to_query.rb +29 -0
- data/lib/active_support/refinements/core_ext/object/try.rb +72 -0
- data/lib/active_support/refinements/core_ext/object/with_options.rb +44 -0
- data/lib/active_support/refinements/core_ext/proc.rb +19 -0
- data/lib/active_support/refinements/core_ext/range.rb +3 -0
- data/lib/active_support/refinements/core_ext/range/conversions.rb +21 -0
- data/lib/active_support/refinements/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/refinements/core_ext/range/overlaps.rb +10 -0
- data/lib/active_support/refinements/core_ext/regexp.rb +7 -0
- data/lib/active_support/refinements/core_ext/string.rb +13 -0
- data/lib/active_support/refinements/core_ext/string/access.rb +106 -0
- data/lib/active_support/refinements/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/refinements/core_ext/string/conversions.rb +60 -0
- data/lib/active_support/refinements/core_ext/string/encoding.rb +10 -0
- data/lib/active_support/refinements/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/refinements/core_ext/string/filters.rb +54 -0
- data/lib/active_support/refinements/core_ext/string/indent.rb +45 -0
- data/lib/active_support/refinements/core_ext/string/inflections.rb +214 -0
- data/lib/active_support/refinements/core_ext/string/inquiry.rb +15 -0
- data/lib/active_support/refinements/core_ext/string/multibyte.rb +58 -0
- data/lib/active_support/refinements/core_ext/string/output_safety.rb +194 -0
- data/lib/active_support/refinements/core_ext/string/starts_ends_with.rb +6 -0
- data/lib/active_support/refinements/core_ext/string/strip.rb +28 -0
- data/lib/active_support/refinements/core_ext/string/xchar.rb +18 -0
- data/lib/active_support/refinements/core_ext/time.rb +5 -0
- data/lib/active_support/refinements/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/refinements/core_ext/time/calculations.rb +251 -0
- data/lib/active_support/refinements/core_ext/time/conversions.rb +65 -0
- data/lib/active_support/refinements/core_ext/time/marshal.rb +30 -0
- data/lib/active_support/refinements/core_ext/time/zones.rb +98 -0
- data/lib/active_support/refinements/core_ext/uri.rb +28 -0
- data/lib/activesupport-refinements.rb +9 -0
- data/lib/activesupport-refinements/version.rb +5 -0
- data/refine_core_ext.rb +45 -0
- data/spec/hwia_spec.rb +15 -0
- data/spec/try_spec.rb +18 -0
- metadata +182 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module TimeExt; end; module TimeExt::Conversions
|
|
2
|
+
require 'active_support/inflector/methods'
|
|
3
|
+
require 'active_support/values/time_zone'
|
|
4
|
+
|
|
5
|
+
refine Time do
|
|
6
|
+
DATE_FORMATS = {
|
|
7
|
+
:db => '%Y-%m-%d %H:%M:%S',
|
|
8
|
+
:number => '%Y%m%d%H%M%S',
|
|
9
|
+
:nsec => '%Y%m%d%H%M%S%9N',
|
|
10
|
+
:time => '%H:%M',
|
|
11
|
+
:short => '%d %b %H:%M',
|
|
12
|
+
:long => '%B %d, %Y %H:%M',
|
|
13
|
+
:long_ordinal => lambda { |time|
|
|
14
|
+
day_format = ActiveSupport::Inflector.ordinalize(time.day)
|
|
15
|
+
time.strftime("%B #{day_format}, %Y %H:%M")
|
|
16
|
+
},
|
|
17
|
+
:rfc822 => lambda { |time|
|
|
18
|
+
offset_format = time.formatted_offset(false)
|
|
19
|
+
time.strftime("%a, %d %b %Y %H:%M:%S #{offset_format}")
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
# Converts to a formatted string. See DATE_FORMATS for builtin formats.
|
|
24
|
+
#
|
|
25
|
+
# This method is aliased to <tt>to_s</tt>.
|
|
26
|
+
#
|
|
27
|
+
# time = Time.now # => Thu Jan 18 06:10:17 CST 2007
|
|
28
|
+
#
|
|
29
|
+
# time.to_formatted_s(:time) # => "06:10"
|
|
30
|
+
# time.to_s(:time) # => "06:10"
|
|
31
|
+
#
|
|
32
|
+
# time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
|
|
33
|
+
# time.to_formatted_s(:number) # => "20070118061017"
|
|
34
|
+
# time.to_formatted_s(:short) # => "18 Jan 06:10"
|
|
35
|
+
# time.to_formatted_s(:long) # => "January 18, 2007 06:10"
|
|
36
|
+
# time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
|
|
37
|
+
# time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
|
|
38
|
+
#
|
|
39
|
+
# == Adding your own time formats to +to_formatted_s+
|
|
40
|
+
# You can add your own formats to the Time::DATE_FORMATS hash.
|
|
41
|
+
# Use the format name as the hash key and either a strftime string
|
|
42
|
+
# or Proc instance that takes a time argument as the value.
|
|
43
|
+
#
|
|
44
|
+
# # config/initializers/time_formats.rb
|
|
45
|
+
# Time::DATE_FORMATS[:month_and_year] = '%B %Y'
|
|
46
|
+
# Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }
|
|
47
|
+
def to_formatted_s(format = :default)
|
|
48
|
+
if formatter = DATE_FORMATS[format]
|
|
49
|
+
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
|
50
|
+
else
|
|
51
|
+
to_default_s
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
# alias_method :to_default_s, :to_s
|
|
55
|
+
# alias_method :to_s, :to_formatted_s
|
|
56
|
+
|
|
57
|
+
# Returns the UTC offset as an +HH:MM formatted string.
|
|
58
|
+
#
|
|
59
|
+
# Time.local(2000).formatted_offset # => "-06:00"
|
|
60
|
+
# Time.local(2000).formatted_offset(false) # => "-0600"
|
|
61
|
+
def formatted_offset(colon = true, alternate_utc_string = nil)
|
|
62
|
+
utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Ruby 1.9.2 adds utc_offset and zone to Time, but marshaling only
|
|
2
|
+
# preserves utc_offset. Preserve zone also, even though it may not
|
|
3
|
+
# work in some edge cases.
|
|
4
|
+
if Time.local(2010).zone != Marshal.load(Marshal.dump(Time.local(2010))).zone
|
|
5
|
+
class Time
|
|
6
|
+
class << self
|
|
7
|
+
alias_method :_load_without_zone, :_load
|
|
8
|
+
def _load(marshaled_time)
|
|
9
|
+
time = _load_without_zone(marshaled_time)
|
|
10
|
+
time.instance_eval do
|
|
11
|
+
if zone = defined?(@_zone) && remove_instance_variable('@_zone')
|
|
12
|
+
ary = to_a
|
|
13
|
+
ary[0] += subsec if ary[0] == sec
|
|
14
|
+
ary[-1] = zone
|
|
15
|
+
utc? ? Time.utc(*ary) : Time.local(*ary)
|
|
16
|
+
else
|
|
17
|
+
self
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
alias_method :_dump_without_zone, :_dump
|
|
24
|
+
def _dump(*args)
|
|
25
|
+
obj = dup
|
|
26
|
+
obj.instance_variable_set('@_zone', zone)
|
|
27
|
+
obj._dump_without_zone(*args)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
module TimeExt; end; module TimeExt::Zones
|
|
2
|
+
require 'active_support/time_with_zone'
|
|
3
|
+
|
|
4
|
+
refine Time do
|
|
5
|
+
class << self
|
|
6
|
+
attr_accessor :zone_default
|
|
7
|
+
|
|
8
|
+
# Returns the TimeZone for the current request, if this has been set (via Time.zone=).
|
|
9
|
+
# If <tt>Time.zone</tt> has not been set for the current request, returns the TimeZone specified in <tt>config.time_zone</tt>.
|
|
10
|
+
def zone
|
|
11
|
+
Thread.current[:time_zone] || zone_default
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Sets <tt>Time.zone</tt> to a TimeZone object for the current request/thread.
|
|
15
|
+
#
|
|
16
|
+
# This method accepts any of the following:
|
|
17
|
+
#
|
|
18
|
+
# * A Rails TimeZone object.
|
|
19
|
+
# * An identifier for a Rails TimeZone object (e.g., "Eastern Time (US & Canada)", <tt>-5.hours</tt>).
|
|
20
|
+
# * A TZInfo::Timezone object.
|
|
21
|
+
# * An identifier for a TZInfo::Timezone object (e.g., "America/New_York").
|
|
22
|
+
#
|
|
23
|
+
# Here's an example of how you might set <tt>Time.zone</tt> on a per request basis and reset it when the request is done.
|
|
24
|
+
# <tt>current_user.time_zone</tt> just needs to return a string identifying the user's preferred time zone:
|
|
25
|
+
#
|
|
26
|
+
# class ApplicationController < ActionController::Base
|
|
27
|
+
# around_filter :set_time_zone
|
|
28
|
+
#
|
|
29
|
+
# def set_time_zone
|
|
30
|
+
# if logged_in?
|
|
31
|
+
# Time.use_zone(current_user.time_zone) { yield }
|
|
32
|
+
# else
|
|
33
|
+
# yield
|
|
34
|
+
# end
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
def zone=(time_zone)
|
|
38
|
+
Thread.current[:time_zone] = find_zone!(time_zone)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Allows override of <tt>Time.zone</tt> locally inside supplied block; resets <tt>Time.zone</tt> to existing value when done.
|
|
42
|
+
def use_zone(time_zone)
|
|
43
|
+
new_zone = find_zone!(time_zone)
|
|
44
|
+
begin
|
|
45
|
+
old_zone, ::Time.zone = ::Time.zone, new_zone
|
|
46
|
+
yield
|
|
47
|
+
ensure
|
|
48
|
+
::Time.zone = old_zone
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones.
|
|
53
|
+
def find_zone!(time_zone)
|
|
54
|
+
if !time_zone || time_zone.is_a?(ActiveSupport::TimeZone)
|
|
55
|
+
time_zone
|
|
56
|
+
else
|
|
57
|
+
# lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone)
|
|
58
|
+
unless time_zone.respond_to?(:period_for_local)
|
|
59
|
+
time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone
|
|
63
|
+
if time_zone.is_a?(ActiveSupport::TimeZone)
|
|
64
|
+
time_zone
|
|
65
|
+
else
|
|
66
|
+
ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
rescue TZInfo::InvalidTimezoneIdentifier
|
|
70
|
+
raise ArgumentError, "Invalid Timezone: #{time_zone}"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def find_zone(time_zone)
|
|
74
|
+
find_zone!(time_zone) rescue nil
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Returns the simultaneous time in <tt>Time.zone</tt>.
|
|
79
|
+
#
|
|
80
|
+
# Time.zone = 'Hawaii' # => 'Hawaii'
|
|
81
|
+
# Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
|
|
82
|
+
#
|
|
83
|
+
# This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
|
|
84
|
+
# instead of the operating system's time zone.
|
|
85
|
+
#
|
|
86
|
+
# You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
|
|
87
|
+
# and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
|
|
88
|
+
#
|
|
89
|
+
# Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
|
|
90
|
+
def in_time_zone(zone = ::Time.zone)
|
|
91
|
+
if zone
|
|
92
|
+
ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone))
|
|
93
|
+
else
|
|
94
|
+
self
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module UriExt
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require 'uri'
|
|
5
|
+
str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
|
|
6
|
+
parser = URI::Parser.new
|
|
7
|
+
|
|
8
|
+
unless str == parser.unescape(parser.escape(str))
|
|
9
|
+
URI::Parser.class_eval do
|
|
10
|
+
remove_method :unescape
|
|
11
|
+
def unescape(str, escaped = /%[a-fA-F\d]{2}/)
|
|
12
|
+
# TODO: Are we actually sure that ASCII == UTF-8?
|
|
13
|
+
# YK: My initial experiments say yes, but let's be sure please
|
|
14
|
+
enc = str.encoding
|
|
15
|
+
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
|
|
16
|
+
str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
refine URI do
|
|
22
|
+
class << self
|
|
23
|
+
def parser
|
|
24
|
+
@parser ||= URI::Parser.new
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'activesupport-refinements/version'
|
|
2
|
+
# need to load this in advance, because Module#remove_method is heavily used in AS
|
|
3
|
+
require 'active_support/core_ext/module/remove_method'
|
|
4
|
+
|
|
5
|
+
# requiring all core_exts ends up with requiring many modules from the original AS. Commenting out for now
|
|
6
|
+
# Dir[File.join(File.dirname(__FILE__), 'active_support/refinements/core_ext/*.rb')].sort.each do |path|
|
|
7
|
+
# next if File.basename(path, '.rb') == 'logger'
|
|
8
|
+
# require "active_support/refinements/core_ext/#{File.basename(path, '.rb')}"
|
|
9
|
+
# end
|
data/refine_core_ext.rb
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require 'active_support/all'
|
|
3
|
+
|
|
4
|
+
AS_SRC = '~/src/rails/activesupport'
|
|
5
|
+
|
|
6
|
+
core_ext_dir = File.join File.dirname(__FILE__), 'lib/active_support/refinements/core_ext'
|
|
7
|
+
|
|
8
|
+
# copy AS src
|
|
9
|
+
`rm -rf #{core_ext_dir} && mkdir -p #{core_ext_dir} && cp -R #{AS_SRC}/lib/active_support/core_ext/ #{core_ext_dir}`
|
|
10
|
+
|
|
11
|
+
Dir.chdir core_ext_dir do
|
|
12
|
+
Dir.glob('**/*.rb').each do |fn|
|
|
13
|
+
next if ['date_and_time/calculations.rb', # nested modules
|
|
14
|
+
'object/to_json.rb', # defines a new module
|
|
15
|
+
'string/output_safety.rb', # defines a new class
|
|
16
|
+
'module/aliasing.rb' # aliases
|
|
17
|
+
].include? fn
|
|
18
|
+
f = File.read fn
|
|
19
|
+
if f =~ /^\S*(class|module) [A-Z]/
|
|
20
|
+
p fn
|
|
21
|
+
# object/try.rb => ObjectExt::Try
|
|
22
|
+
# enumerable.rb => EnumerableExt
|
|
23
|
+
parent_module, child_module = fn.sub(/\.rb/, '').camelize.split('::')
|
|
24
|
+
module_name = child_module ? "#{parent_module}Ext::#{child_module}" : "#{parent_module}Ext"
|
|
25
|
+
# -class Object
|
|
26
|
+
# +module Object::TryExt
|
|
27
|
+
# +refine Object do
|
|
28
|
+
body = f.gsub(/^(\s)*(?:class|module) (?!QualifiedConstUtils)([A-Z][^\s]*).*/) { "#{$1}refine #{$2.chomp} do" }
|
|
29
|
+
# alias doesn't work inside Refinements
|
|
30
|
+
body = body.gsub(/^( *alias.*)/) { "##{$1}" }
|
|
31
|
+
|
|
32
|
+
File.open(fn, 'w') do |new_file|
|
|
33
|
+
new_file.write <<-EOF
|
|
34
|
+
#{"module #{parent_module}Ext; end; " if child_module}module #{module_name}
|
|
35
|
+
#{body.sub(/\n\z/, '')}
|
|
36
|
+
end
|
|
37
|
+
EOF
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# -require 'active_support/core_ext/object/blank'
|
|
42
|
+
# +require 'active_support/refinements/core_ext/object/blank'
|
|
43
|
+
`sed -i '' "s/require 'active_support\\\/core_ext\\\//require 'active_support\\\/refinements\\\/core_ext\\\//" *.rb **/*.rb`
|
|
44
|
+
end
|
|
45
|
+
end
|
data/spec/hwia_spec.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'active_support/refinements/core_ext/hash/indifferent_access'
|
|
2
|
+
|
|
3
|
+
describe 'Hash#with_indifferent_access' do
|
|
4
|
+
context 'when using HashExt::IndifferentAccess' do
|
|
5
|
+
it 'a Hash has #with_indifferent_access method' do
|
|
6
|
+
Module.new { using HashExt::IndifferentAccess }.module_eval { {a: 1}.with_indifferent_access['a'] }.should == 1
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
context 'when not using HashExt::IndifferentAccess' do
|
|
11
|
+
it 'has no #try method' do
|
|
12
|
+
expect { {a: 1}.with_indifferent_access }.to raise_error NoMethodError
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
data/spec/try_spec.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'active_support/refinements/core_ext/object/try'
|
|
2
|
+
|
|
3
|
+
describe 'Object#try' do
|
|
4
|
+
context 'when using ObjectExt::Try' do
|
|
5
|
+
it 'an Object has #try method' do
|
|
6
|
+
Module.new { using ObjectExt::Try }.module_eval { 'hello'.try(:reverse) }.should == 'olleh'
|
|
7
|
+
end
|
|
8
|
+
it 'nil has #try method' do
|
|
9
|
+
Module.new { using ObjectExt::Try }.module_eval { nil.try(:to_s) }.should be_nil
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context 'when not using ObjectExt::Try' do
|
|
14
|
+
it 'has no #try method' do
|
|
15
|
+
expect { :hello.try(:to_s) }.to raise_error NoMethodError
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: activesupport-refinements
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Akira Matsuda
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-10-31 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
description: Side-effect-free ActiveSupport using Refinements
|
|
31
|
+
email:
|
|
32
|
+
- ronnie@dio.jp
|
|
33
|
+
executables: []
|
|
34
|
+
extensions: []
|
|
35
|
+
extra_rdoc_files: []
|
|
36
|
+
files:
|
|
37
|
+
- .gitignore
|
|
38
|
+
- Gemfile
|
|
39
|
+
- LICENSE.txt
|
|
40
|
+
- README.md
|
|
41
|
+
- Rakefile
|
|
42
|
+
- activesupport-refinements.gemspec
|
|
43
|
+
- lib/active_support/refinements/core_ext/array.rb
|
|
44
|
+
- lib/active_support/refinements/core_ext/array/access.rb
|
|
45
|
+
- lib/active_support/refinements/core_ext/array/conversions.rb
|
|
46
|
+
- lib/active_support/refinements/core_ext/array/extract_options.rb
|
|
47
|
+
- lib/active_support/refinements/core_ext/array/grouping.rb
|
|
48
|
+
- lib/active_support/refinements/core_ext/array/prepend_and_append.rb
|
|
49
|
+
- lib/active_support/refinements/core_ext/array/uniq_by.rb
|
|
50
|
+
- lib/active_support/refinements/core_ext/array/wrap.rb
|
|
51
|
+
- lib/active_support/refinements/core_ext/benchmark.rb
|
|
52
|
+
- lib/active_support/refinements/core_ext/big_decimal.rb
|
|
53
|
+
- lib/active_support/refinements/core_ext/big_decimal/conversions.rb
|
|
54
|
+
- lib/active_support/refinements/core_ext/class.rb
|
|
55
|
+
- lib/active_support/refinements/core_ext/class/attribute.rb
|
|
56
|
+
- lib/active_support/refinements/core_ext/class/attribute_accessors.rb
|
|
57
|
+
- lib/active_support/refinements/core_ext/class/delegating_attributes.rb
|
|
58
|
+
- lib/active_support/refinements/core_ext/class/subclasses.rb
|
|
59
|
+
- lib/active_support/refinements/core_ext/date.rb
|
|
60
|
+
- lib/active_support/refinements/core_ext/date/acts_like.rb
|
|
61
|
+
- lib/active_support/refinements/core_ext/date/calculations.rb
|
|
62
|
+
- lib/active_support/refinements/core_ext/date/conversions.rb
|
|
63
|
+
- lib/active_support/refinements/core_ext/date/zones.rb
|
|
64
|
+
- lib/active_support/refinements/core_ext/date_and_time/calculations.rb
|
|
65
|
+
- lib/active_support/refinements/core_ext/date_time.rb
|
|
66
|
+
- lib/active_support/refinements/core_ext/date_time/acts_like.rb
|
|
67
|
+
- lib/active_support/refinements/core_ext/date_time/calculations.rb
|
|
68
|
+
- lib/active_support/refinements/core_ext/date_time/conversions.rb
|
|
69
|
+
- lib/active_support/refinements/core_ext/date_time/zones.rb
|
|
70
|
+
- lib/active_support/refinements/core_ext/enumerable.rb
|
|
71
|
+
- lib/active_support/refinements/core_ext/exception.rb
|
|
72
|
+
- lib/active_support/refinements/core_ext/file.rb
|
|
73
|
+
- lib/active_support/refinements/core_ext/file/atomic.rb
|
|
74
|
+
- lib/active_support/refinements/core_ext/hash.rb
|
|
75
|
+
- lib/active_support/refinements/core_ext/hash/conversions.rb
|
|
76
|
+
- lib/active_support/refinements/core_ext/hash/deep_merge.rb
|
|
77
|
+
- lib/active_support/refinements/core_ext/hash/diff.rb
|
|
78
|
+
- lib/active_support/refinements/core_ext/hash/except.rb
|
|
79
|
+
- lib/active_support/refinements/core_ext/hash/indifferent_access.rb
|
|
80
|
+
- lib/active_support/refinements/core_ext/hash/keys.rb
|
|
81
|
+
- lib/active_support/refinements/core_ext/hash/reverse_merge.rb
|
|
82
|
+
- lib/active_support/refinements/core_ext/hash/slice.rb
|
|
83
|
+
- lib/active_support/refinements/core_ext/integer.rb
|
|
84
|
+
- lib/active_support/refinements/core_ext/integer/inflections.rb
|
|
85
|
+
- lib/active_support/refinements/core_ext/integer/multiple.rb
|
|
86
|
+
- lib/active_support/refinements/core_ext/integer/time.rb
|
|
87
|
+
- lib/active_support/refinements/core_ext/kernel.rb
|
|
88
|
+
- lib/active_support/refinements/core_ext/kernel/agnostics.rb
|
|
89
|
+
- lib/active_support/refinements/core_ext/kernel/debugger.rb
|
|
90
|
+
- lib/active_support/refinements/core_ext/kernel/reporting.rb
|
|
91
|
+
- lib/active_support/refinements/core_ext/kernel/singleton_class.rb
|
|
92
|
+
- lib/active_support/refinements/core_ext/load_error.rb
|
|
93
|
+
- lib/active_support/refinements/core_ext/logger.rb
|
|
94
|
+
- lib/active_support/refinements/core_ext/module.rb
|
|
95
|
+
- lib/active_support/refinements/core_ext/module/aliasing.rb
|
|
96
|
+
- lib/active_support/refinements/core_ext/module/anonymous.rb
|
|
97
|
+
- lib/active_support/refinements/core_ext/module/attr_internal.rb
|
|
98
|
+
- lib/active_support/refinements/core_ext/module/attribute_accessors.rb
|
|
99
|
+
- lib/active_support/refinements/core_ext/module/delegation.rb
|
|
100
|
+
- lib/active_support/refinements/core_ext/module/deprecation.rb
|
|
101
|
+
- lib/active_support/refinements/core_ext/module/introspection.rb
|
|
102
|
+
- lib/active_support/refinements/core_ext/module/qualified_const.rb
|
|
103
|
+
- lib/active_support/refinements/core_ext/module/reachable.rb
|
|
104
|
+
- lib/active_support/refinements/core_ext/module/remove_method.rb
|
|
105
|
+
- lib/active_support/refinements/core_ext/name_error.rb
|
|
106
|
+
- lib/active_support/refinements/core_ext/numeric.rb
|
|
107
|
+
- lib/active_support/refinements/core_ext/numeric/bytes.rb
|
|
108
|
+
- lib/active_support/refinements/core_ext/numeric/conversions.rb
|
|
109
|
+
- lib/active_support/refinements/core_ext/numeric/time.rb
|
|
110
|
+
- lib/active_support/refinements/core_ext/object.rb
|
|
111
|
+
- lib/active_support/refinements/core_ext/object/acts_like.rb
|
|
112
|
+
- lib/active_support/refinements/core_ext/object/blank.rb
|
|
113
|
+
- lib/active_support/refinements/core_ext/object/conversions.rb
|
|
114
|
+
- lib/active_support/refinements/core_ext/object/deep_dup.rb
|
|
115
|
+
- lib/active_support/refinements/core_ext/object/duplicable.rb
|
|
116
|
+
- lib/active_support/refinements/core_ext/object/inclusion.rb
|
|
117
|
+
- lib/active_support/refinements/core_ext/object/instance_variables.rb
|
|
118
|
+
- lib/active_support/refinements/core_ext/object/to_json.rb
|
|
119
|
+
- lib/active_support/refinements/core_ext/object/to_param.rb
|
|
120
|
+
- lib/active_support/refinements/core_ext/object/to_query.rb
|
|
121
|
+
- lib/active_support/refinements/core_ext/object/try.rb
|
|
122
|
+
- lib/active_support/refinements/core_ext/object/with_options.rb
|
|
123
|
+
- lib/active_support/refinements/core_ext/proc.rb
|
|
124
|
+
- lib/active_support/refinements/core_ext/range.rb
|
|
125
|
+
- lib/active_support/refinements/core_ext/range/conversions.rb
|
|
126
|
+
- lib/active_support/refinements/core_ext/range/include_range.rb
|
|
127
|
+
- lib/active_support/refinements/core_ext/range/overlaps.rb
|
|
128
|
+
- lib/active_support/refinements/core_ext/regexp.rb
|
|
129
|
+
- lib/active_support/refinements/core_ext/string.rb
|
|
130
|
+
- lib/active_support/refinements/core_ext/string/access.rb
|
|
131
|
+
- lib/active_support/refinements/core_ext/string/behavior.rb
|
|
132
|
+
- lib/active_support/refinements/core_ext/string/conversions.rb
|
|
133
|
+
- lib/active_support/refinements/core_ext/string/encoding.rb
|
|
134
|
+
- lib/active_support/refinements/core_ext/string/exclude.rb
|
|
135
|
+
- lib/active_support/refinements/core_ext/string/filters.rb
|
|
136
|
+
- lib/active_support/refinements/core_ext/string/indent.rb
|
|
137
|
+
- lib/active_support/refinements/core_ext/string/inflections.rb
|
|
138
|
+
- lib/active_support/refinements/core_ext/string/inquiry.rb
|
|
139
|
+
- lib/active_support/refinements/core_ext/string/multibyte.rb
|
|
140
|
+
- lib/active_support/refinements/core_ext/string/output_safety.rb
|
|
141
|
+
- lib/active_support/refinements/core_ext/string/starts_ends_with.rb
|
|
142
|
+
- lib/active_support/refinements/core_ext/string/strip.rb
|
|
143
|
+
- lib/active_support/refinements/core_ext/string/xchar.rb
|
|
144
|
+
- lib/active_support/refinements/core_ext/time.rb
|
|
145
|
+
- lib/active_support/refinements/core_ext/time/acts_like.rb
|
|
146
|
+
- lib/active_support/refinements/core_ext/time/calculations.rb
|
|
147
|
+
- lib/active_support/refinements/core_ext/time/conversions.rb
|
|
148
|
+
- lib/active_support/refinements/core_ext/time/marshal.rb
|
|
149
|
+
- lib/active_support/refinements/core_ext/time/zones.rb
|
|
150
|
+
- lib/active_support/refinements/core_ext/uri.rb
|
|
151
|
+
- lib/activesupport-refinements.rb
|
|
152
|
+
- lib/activesupport-refinements/version.rb
|
|
153
|
+
- refine_core_ext.rb
|
|
154
|
+
- spec/hwia_spec.rb
|
|
155
|
+
- spec/try_spec.rb
|
|
156
|
+
homepage: https://github.com/amatsuda/activesupport-refinements
|
|
157
|
+
licenses: []
|
|
158
|
+
post_install_message:
|
|
159
|
+
rdoc_options: []
|
|
160
|
+
require_paths:
|
|
161
|
+
- lib
|
|
162
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
163
|
+
none: false
|
|
164
|
+
requirements:
|
|
165
|
+
- - ! '>='
|
|
166
|
+
- !ruby/object:Gem::Version
|
|
167
|
+
version: '0'
|
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
|
+
none: false
|
|
170
|
+
requirements:
|
|
171
|
+
- - ! '>='
|
|
172
|
+
- !ruby/object:Gem::Version
|
|
173
|
+
version: '0'
|
|
174
|
+
requirements: []
|
|
175
|
+
rubyforge_project:
|
|
176
|
+
rubygems_version: 1.8.24
|
|
177
|
+
signing_key:
|
|
178
|
+
specification_version: 3
|
|
179
|
+
summary: ActiveSupport + Ruby 2.0 refinements
|
|
180
|
+
test_files:
|
|
181
|
+
- spec/hwia_spec.rb
|
|
182
|
+
- spec/try_spec.rb
|