code
stringlengths 26
124k
| docstring
stringlengths 23
125k
| func_name
stringlengths 1
98
| language
stringclasses 1
value | repo
stringlengths 5
53
| path
stringlengths 7
151
| url
stringlengths 50
211
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
def readable_inspect
strftime("%a, %d %b %Y")
end | Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005" | readable_inspect | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date/conversions.rb | Apache-2.0 |
def to_time(form = :local)
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
::Time.public_send(form, year, month, day)
end | Converts a Date instance to a Time, where the time is set to the beginning of the day.
The timezone can be either :local or :utc (default :local).
date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
date.to_time # => 2007-11-10 00:00:00 0800
date.to_time(:local) # => 2007-11-10 00:00:00 0800
date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
NOTE: The :local timezone is Ruby's *process* timezone, i.e. ENV['TZ'].
If the *application's* timezone is needed, then use +in_time_zone+ instead. | to_time | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date/conversions.rb | Apache-2.0 |
def yesterday
advance(days: -1)
end | Returns a new date/time representing yesterday. | yesterday | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def tomorrow
advance(days: 1)
end | Returns a new date/time representing tomorrow. | tomorrow | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def today?
to_date == ::Date.current
end | Returns true if the date/time is today. | today? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def tomorrow?
to_date == ::Date.current.tomorrow
end | Returns true if the date/time is tomorrow. | tomorrow? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def yesterday?
to_date == ::Date.current.yesterday
end | Returns true if the date/time is yesterday. | yesterday? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def past?
self < self.class.current
end | Returns true if the date/time is in the past. | past? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def future?
self > self.class.current
end | Returns true if the date/time is in the future. | future? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def before?(date_or_time)
self < date_or_time
end | Returns true if the date/time falls before <tt>date_or_time</tt>. | before? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def after?(date_or_time)
self > date_or_time
end | Returns true if the date/time falls after <tt>date_or_time</tt>. | after? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def days_ago(days)
advance(days: -days)
end | Returns a new date/time the specified number of days ago. | days_ago | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def days_since(days)
advance(days: days)
end | Returns a new date/time the specified number of days in the future. | days_since | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def weeks_ago(weeks)
advance(weeks: -weeks)
end | Returns a new date/time the specified number of weeks ago. | weeks_ago | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def weeks_since(weeks)
advance(weeks: weeks)
end | Returns a new date/time the specified number of weeks in the future. | weeks_since | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def months_ago(months)
advance(months: -months)
end | Returns a new date/time the specified number of months ago. | months_ago | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def months_since(months)
advance(months: months)
end | Returns a new date/time the specified number of months in the future. | months_since | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def years_ago(years)
advance(years: -years)
end | Returns a new date/time the specified number of years ago. | years_ago | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def years_since(years)
advance(years: years)
end | Returns a new date/time the specified number of years in the future. | years_since | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def beginning_of_month
first_hour(change(day: 1))
end | Returns a new date/time at the start of the month.
today = Date.today # => Thu, 18 Jun 2015
today.beginning_of_month # => Mon, 01 Jun 2015
+DateTime+ objects will have a time set to 0:00.
now = DateTime.current # => Thu, 18 Jun 2015 15:23:13 +0000
now.beginning_of_month # => Mon, 01 Jun 2015 00:00:00 +0000 | beginning_of_month | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def beginning_of_quarter
first_quarter_month = month - (2 + month) % 3
beginning_of_month.change(month: first_quarter_month)
end | Returns a new date/time at the start of the quarter.
today = Date.today # => Fri, 10 Jul 2015
today.beginning_of_quarter # => Wed, 01 Jul 2015
+DateTime+ objects will have a time set to 0:00.
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.beginning_of_quarter # => Wed, 01 Jul 2015 00:00:00 +0000 | beginning_of_quarter | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def end_of_quarter
last_quarter_month = month + (12 - month) % 3
beginning_of_month.change(month: last_quarter_month).end_of_month
end | Returns a new date/time at the end of the quarter.
today = Date.today # => Fri, 10 Jul 2015
today.end_of_quarter # => Wed, 30 Sep 2015
+DateTime+ objects will have a time set to 23:59:59.
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.end_of_quarter # => Wed, 30 Sep 2015 23:59:59 +0000 | end_of_quarter | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def beginning_of_year
change(month: 1).beginning_of_month
end | Returns a new date/time at the beginning of the year.
today = Date.today # => Fri, 10 Jul 2015
today.beginning_of_year # => Thu, 01 Jan 2015
+DateTime+ objects will have a time set to 0:00.
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.beginning_of_year # => Thu, 01 Jan 2015 00:00:00 +0000 | beginning_of_year | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false)
result = first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week)))
same_time ? copy_time_to(result) : result
end | Returns a new date/time representing the given day in the next week.
today = Date.today # => Thu, 07 May 2015
today.next_week # => Mon, 11 May 2015
The +given_day_in_next_week+ defaults to the beginning of the week
which is determined by +Date.beginning_of_week+ or +config.beginning_of_week+
when set.
today = Date.today # => Thu, 07 May 2015
today.next_week(:friday) # => Fri, 15 May 2015
+DateTime+ objects have their time set to 0:00 unless +same_time+ is true.
now = DateTime.current # => Thu, 07 May 2015 13:31:16 +0000
now.next_week # => Mon, 11 May 2015 00:00:00 +0000 | next_week | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def next_weekday
if next_day.on_weekend?
next_week(:monday, same_time: true)
else
next_day
end
end | Returns a new date/time representing the next weekday. | next_weekday | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def prev_week(start_day = Date.beginning_of_week, same_time: false)
result = first_hour(weeks_ago(1).beginning_of_week.days_since(days_span(start_day)))
same_time ? copy_time_to(result) : result
end | Returns a new date/time representing the given day in the previous week.
Week is assumed to start on +start_day+, default is
+Date.beginning_of_week+ or +config.beginning_of_week+ when set.
DateTime objects have their time set to 0:00 unless +same_time+ is true. | prev_week | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def prev_weekday
if prev_day.on_weekend?
copy_time_to(beginning_of_week(:friday))
else
prev_day
end
end | Returns a new date/time representing the previous weekday. | prev_weekday | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def days_to_week_start(start_day = Date.beginning_of_week)
start_day_number = DAYS_INTO_WEEK.fetch(start_day)
(wday - start_day_number) % 7
end | Returns the number of days to the start of the week on the given day.
Week is assumed to start on +start_day+, default is
+Date.beginning_of_week+ or +config.beginning_of_week+ when set. | days_to_week_start | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def beginning_of_week(start_day = Date.beginning_of_week)
result = days_ago(days_to_week_start(start_day))
acts_like?(:time) ? result.midnight : result
end | Returns a new date/time representing the start of this week on the given day.
Week is assumed to start on +start_day+, default is
+Date.beginning_of_week+ or +config.beginning_of_week+ when set.
+DateTime+ objects have their time set to 0:00. | beginning_of_week | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def end_of_week(start_day = Date.beginning_of_week)
last_hour(days_since(6 - days_to_week_start(start_day)))
end | Returns a new date/time representing the end of this week on the given day.
Week is assumed to start on +start_day+, default is
+Date.beginning_of_week+ or +config.beginning_of_week+ when set.
DateTime objects have their time set to 23:59:59. | end_of_week | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def end_of_month
last_day = ::Time.days_in_month(month, year)
last_hour(days_since(last_day - day))
end | Returns a new date/time representing the end of the month.
DateTime objects will have a time set to 23:59:59. | end_of_month | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def end_of_year
change(month: 12).end_of_month
end | Returns a new date/time representing the end of the year.
DateTime objects will have a time set to 23:59:59. | end_of_year | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def next_occurring(day_of_week)
from_now = DAYS_INTO_WEEK.fetch(day_of_week) - wday
from_now += 7 unless from_now > 0
advance(days: from_now)
end | Returns a new date/time representing the next occurrence of the specified day of week.
today = Date.today # => Thu, 14 Dec 2017
today.next_occurring(:monday) # => Mon, 18 Dec 2017
today.next_occurring(:thursday) # => Thu, 21 Dec 2017 | next_occurring | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def prev_occurring(day_of_week)
ago = wday - DAYS_INTO_WEEK.fetch(day_of_week)
ago += 7 unless ago > 0
advance(days: -ago)
end | Returns a new date/time representing the previous occurrence of the specified day of week.
today = Date.today # => Thu, 14 Dec 2017
today.prev_occurring(:monday) # => Mon, 11 Dec 2017
today.prev_occurring(:thursday) # => Thu, 07 Dec 2017 | prev_occurring | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/calculations.rb | Apache-2.0 |
def in_time_zone(zone = ::Time.zone)
time_zone = ::Time.find_zone! zone
time = acts_like?(:time) ? self : nil
if time_zone
time_with_zone(time, time_zone)
else
time || to_time
end
end | Returns the simultaneous time in <tt>Time.zone</tt> if a zone is given or
if Time.zone_default is set. Otherwise, it returns the current time.
Time.zone = 'Hawaii' # => 'Hawaii'
Time.utc(2000).in_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
Date.new(2000).in_time_zone # => Sat, 01 Jan 2000 00:00:00 HST -10:00
This method is similar to Time#localtime, except that it uses <tt>Time.zone</tt> as the local zone
instead of the operating system's time zone.
You can also pass in a TimeZone instance or string that identifies a TimeZone as an argument,
and the conversion will be based on that zone instead of <tt>Time.zone</tt>.
Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00 | in_time_zone | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/zones.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_and_time/zones.rb | Apache-2.0 |
def current
::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime
end | Returns <tt>Time.zone.now.to_datetime</tt> when <tt>Time.zone</tt> or
<tt>config.time_zone</tt> are set, otherwise returns
<tt>Time.now.to_datetime</tt>. | current | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def seconds_since_midnight
sec + (min * 60) + (hour * 3600)
end | Returns the number of seconds since 00:00:00.
DateTime.new(2012, 8, 29, 0, 0, 0).seconds_since_midnight # => 0
DateTime.new(2012, 8, 29, 12, 34, 56).seconds_since_midnight # => 45296
DateTime.new(2012, 8, 29, 23, 59, 59).seconds_since_midnight # => 86399 | seconds_since_midnight | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def seconds_until_end_of_day
end_of_day.to_i - to_i
end | Returns the number of seconds until 23:59:59.
DateTime.new(2012, 8, 29, 0, 0, 0).seconds_until_end_of_day # => 86399
DateTime.new(2012, 8, 29, 12, 34, 56).seconds_until_end_of_day # => 41103
DateTime.new(2012, 8, 29, 23, 59, 59).seconds_until_end_of_day # => 0 | seconds_until_end_of_day | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def change(options)
if new_nsec = options[:nsec]
raise ArgumentError, "Can't change both :nsec and :usec at the same time: #{options.inspect}" if options[:usec]
new_fraction = Rational(new_nsec, 1000000000)
else
new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
new_fraction = Rational(new_usec, 1000000)
end
raise ArgumentError, "argument out of range" if new_fraction >= 1
::DateTime.civil(
options.fetch(:year, year),
options.fetch(:month, month),
options.fetch(:day, day),
options.fetch(:hour, hour),
options.fetch(:min, options[:hour] ? 0 : min),
options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec) + new_fraction,
options.fetch(:offset, offset),
options.fetch(:start, start)
)
end | Returns a new DateTime where one or more of the elements have been changed
according to the +options+ parameter. The time options (<tt>:hour</tt>,
<tt>:min</tt>, <tt>:sec</tt>) reset cascadingly, so if only the hour is
passed, then minute and sec is set to 0. If the hour and minute is passed,
then sec is set to 0. The +options+ parameter takes a hash with any of these
keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>,
<tt>:min</tt>, <tt>:sec</tt>, <tt>:offset</tt>, <tt>:start</tt>.
DateTime.new(2012, 8, 29, 22, 35, 0).change(day: 1) # => DateTime.new(2012, 8, 1, 22, 35, 0)
DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, day: 1) # => DateTime.new(1981, 8, 1, 22, 35, 0)
DateTime.new(2012, 8, 29, 22, 35, 0).change(year: 1981, hour: 0) # => DateTime.new(1981, 8, 29, 0, 0, 0) | change | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def advance(options)
unless options[:weeks].nil?
options[:weeks], partial_weeks = options[:weeks].divmod(1)
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
end
unless options[:days].nil?
options[:days], partial_days = options[:days].divmod(1)
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
end
d = to_date.advance(options)
datetime_advanced_by_date = change(year: d.year, month: d.month, day: d.day)
seconds_to_advance = \
options.fetch(:seconds, 0) +
options.fetch(:minutes, 0) * 60 +
options.fetch(:hours, 0) * 3600
if seconds_to_advance.zero?
datetime_advanced_by_date
else
datetime_advanced_by_date.since(seconds_to_advance)
end
end | Uses Date to provide precise Time calculations for years, months, and days.
The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
<tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
<tt>:minutes</tt>, <tt>:seconds</tt>. | advance | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def since(seconds)
self + Rational(seconds, 86400)
end | Returns a new DateTime representing the time a number of seconds since the
instance time. Do not use this method in combination with x.months, use
months_since instead! | since | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def beginning_of_day
change(hour: 0)
end | Returns a new DateTime representing the start of the day (0:00). | beginning_of_day | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def middle_of_day
change(hour: 12)
end | Returns a new DateTime representing the middle of the day (12:00) | middle_of_day | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def end_of_day
change(hour: 23, min: 59, sec: 59, usec: Rational(999999999, 1000))
end | Returns a new DateTime representing the end of the day (23:59:59). | end_of_day | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def beginning_of_hour
change(min: 0)
end | Returns a new DateTime representing the start of the hour (hh:00:00). | beginning_of_hour | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def end_of_hour
change(min: 59, sec: 59, usec: Rational(999999999, 1000))
end | Returns a new DateTime representing the end of the hour (hh:59:59). | end_of_hour | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def beginning_of_minute
change(sec: 0)
end | Returns a new DateTime representing the start of the minute (hh:mm:00). | beginning_of_minute | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def end_of_minute
change(sec: 59, usec: Rational(999999999, 1000))
end | Returns a new DateTime representing the end of the minute (hh:mm:59). | end_of_minute | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def localtime(utc_offset = nil)
utc = new_offset(0)
Time.utc(
utc.year, utc.month, utc.day,
utc.hour, utc.min, utc.sec + utc.sec_fraction
).getlocal(utc_offset)
end | Returns a <tt>Time</tt> instance of the simultaneous time in the system timezone. | localtime | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def utc
utc = new_offset(0)
Time.utc(
utc.year, utc.month, utc.day,
utc.hour, utc.min, utc.sec + utc.sec_fraction
)
end | Returns a <tt>Time</tt> instance of the simultaneous time in the UTC timezone.
DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 UTC | utc | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def utc?
offset == 0
end | Returns +true+ if <tt>offset == 0</tt>. | utc? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def utc_offset
(offset * 86400).to_i
end | Returns the offset value in seconds. | utc_offset | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/calculations.rb | Apache-2.0 |
def to_time
preserve_timezone ? getlocal(utc_offset) : getlocal
end | Either return an instance of +Time+ with the same UTC offset
as +self+ or an instance of +Time+ representing the same time
in the local system timezone depending on the setting of
on the setting of +ActiveSupport.to_time_preserves_timezone+. | to_time | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/compatibility.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/compatibility.rb | Apache-2.0 |
def to_formatted_s(format = :default)
if formatter = ::Time::DATE_FORMATS[format]
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
else
to_default_s
end
end | Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
This method is aliased to <tt>to_s</tt>.
=== Examples
datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
datetime.to_s(:db) # => "2007-12-04 00:00:00"
datetime.to_s(:number) # => "20071204000000"
datetime.to_formatted_s(:short) # => "04 Dec 00:00"
datetime.to_formatted_s(:long) # => "December 04, 2007 00:00"
datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00"
datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
datetime.to_formatted_s(:iso8601) # => "2007-12-04T00:00:00+00:00"
== Adding your own datetime formats to to_formatted_s
DateTime formats are shared with Time. You can add your own to the
Time::DATE_FORMATS hash. Use the format name as the hash key and
either a strftime string or Proc instance that takes a time or
datetime argument as the value.
# config/initializers/time_formats.rb
Time::DATE_FORMATS[:month_and_year] = '%B %Y'
Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } | to_formatted_s | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | Apache-2.0 |
def formatted_offset(colon = true, alternate_utc_string = nil)
utc? && alternate_utc_string || ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, colon)
end | Returns a formatted string of the offset from UTC, or an alternative
string if the time zone is already UTC.
datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24))
datetime.formatted_offset # => "-06:00"
datetime.formatted_offset(false) # => "-0600" | formatted_offset | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | Apache-2.0 |
def to_f
seconds_since_unix_epoch.to_f + sec_fraction
end | Converts +self+ to a floating-point number of seconds, including fractional microseconds, since the Unix epoch. | to_f | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | Apache-2.0 |
def usec
(sec_fraction * 1_000_000).to_i
end | Returns the fraction of a second as microseconds | usec | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | Apache-2.0 |
def nsec
(sec_fraction * 1_000_000_000).to_i
end | Returns the fraction of a second as nanoseconds | nsec | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/date_time/conversions.rb | Apache-2.0 |
def to_xml(options = {})
require "active_support/builder" unless defined?(Builder::XmlMarkup)
options = options.dup
options[:indent] ||= 2
options[:root] ||= "hash"
options[:builder] ||= Builder::XmlMarkup.new(indent: options[:indent])
builder = options[:builder]
builder.instruct! unless options.delete(:skip_instruct)
root = ActiveSupport::XmlMini.rename_key(options[:root].to_s, options)
builder.tag!(root) do
each { |key, value| ActiveSupport::XmlMini.to_tag(key, value, options) }
yield builder if block_given?
end
end | Returns a string containing an XML representation of its receiver:
{ foo: 1, bar: 2 }.to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
# <hash>
# <foo type="integer">1</foo>
# <bar type="integer">2</bar>
# </hash>
To do so, the method loops over the pairs and builds nodes that depend on
the _values_. Given a pair +key+, +value+:
* If +value+ is a hash there's a recursive call with +key+ as <tt>:root</tt>.
* If +value+ is an array there's a recursive call with +key+ as <tt>:root</tt>,
and +key+ singularized as <tt>:children</tt>.
* If +value+ is a callable object it must expect one or two arguments. Depending
on the arity, the callable is invoked with the +options+ hash as first argument
with +key+ as <tt>:root</tt>, and +key+ singularized as second argument. The
callable can add nodes by using <tt>options[:builder]</tt>.
{foo: lambda { |options, key| options[:builder].b(key) }}.to_xml
# => "<b>foo</b>"
* If +value+ responds to +to_xml+ the method is invoked with +key+ as <tt>:root</tt>.
class Foo
def to_xml(options)
options[:builder].bar 'fooing!'
end
end
{ foo: Foo.new }.to_xml(skip_instruct: true)
# =>
# <hash>
# <bar>fooing!</bar>
# </hash>
* Otherwise, a node with +key+ as tag is created with a string representation of
+value+ as text node. If +value+ is +nil+ an attribute "nil" set to "true" is added.
Unless the option <tt>:skip_types</tt> exists and is true, an attribute "type" is
added as well according to the following mapping:
XML_TYPE_NAMES = {
"Symbol" => "symbol",
"Integer" => "integer",
"BigDecimal" => "decimal",
"Float" => "float",
"TrueClass" => "boolean",
"FalseClass" => "boolean",
"Date" => "date",
"DateTime" => "dateTime",
"Time" => "dateTime"
}
By default the root node is "hash", but that's configurable via the <tt>:root</tt> option.
The default XML builder is a fresh instance of <tt>Builder::XmlMarkup</tt>. You can
configure your own builder with the <tt>:builder</tt> option. The method also accepts
options like <tt>:dasherize</tt> and friends, they are forwarded to the builder. | to_xml | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/conversions.rb | Apache-2.0 |
def from_xml(xml, disallowed_types = nil)
ActiveSupport::XMLConverter.new(xml, disallowed_types).to_h
end | Returns a Hash containing a collection of pairs when the key is the node name and the value is
its content
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<foo type="integer">1</foo>
<bar type="integer">2</bar>
</hash>
XML
hash = Hash.from_xml(xml)
# => {"hash"=>{"foo"=>1, "bar"=>2}}
+DisallowedType+ is raised if the XML contains attributes with <tt>type="yaml"</tt> or
<tt>type="symbol"</tt>. Use <tt>Hash.from_trusted_xml</tt> to
parse this XML.
Custom +disallowed_types+ can also be passed in the form of an
array.
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<foo type="integer">1</foo>
<bar type="string">"David"</bar>
</hash>
XML
hash = Hash.from_xml(xml, ['integer'])
# => ActiveSupport::XMLConverter::DisallowedType: Disallowed type attribute: "integer"
Note that passing custom disallowed types will override the default types,
which are Symbol and YAML. | from_xml | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/conversions.rb | Apache-2.0 |
def from_trusted_xml(xml)
from_xml xml, []
end | Builds a Hash from XML just like <tt>Hash.from_xml</tt>, but also allows Symbol and YAML. | from_trusted_xml | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/conversions.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/conversions.rb | Apache-2.0 |
def deep_merge(other_hash, &block)
dup.deep_merge!(other_hash, &block)
end | Returns a new hash with +self+ and +other_hash+ merged recursively.
h1 = { a: true, b: { c: [1, 2, 3] } }
h2 = { a: false, b: { x: [3, 4, 5] } }
h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
Like with Hash#merge in the standard library, a block can be provided
to merge values:
h1 = { a: 100, b: 200, c: { c1: 100 } }
h2 = { b: 250, c: { c1: 200 } }
h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
# => { a: 100, b: 450, c: { c1: 300 } } | deep_merge | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_merge.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_merge.rb | Apache-2.0 |
def deep_transform_values(&block)
_deep_transform_values_in_object(self, &block)
end | Returns a new hash with all values converted by the block operation.
This includes the values from the root hash and from all
nested hashes and arrays.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_transform_values{ |value| value.to_s.upcase }
# => {person: {name: "ROB", age: "28"}} | deep_transform_values | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_transform_values.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_transform_values.rb | Apache-2.0 |
def deep_transform_values!(&block)
_deep_transform_values_in_object!(self, &block)
end | Destructively converts all values by using the block operation.
This includes the values from the root hash and from all
nested hashes and arrays. | deep_transform_values! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_transform_values.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_transform_values.rb | Apache-2.0 |
def _deep_transform_values_in_object(object, &block)
case object
when Hash
object.transform_values { |value| _deep_transform_values_in_object(value, &block) }
when Array
object.map { |e| _deep_transform_values_in_object(e, &block) }
else
yield(object)
end
end | Support methods for deep transforming nested hashes and arrays. | _deep_transform_values_in_object | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_transform_values.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/deep_transform_values.rb | Apache-2.0 |
def except!(*keys)
keys.each { |key| delete(key) }
self
end | Removes the given keys from hash and returns it.
hash = { a: true, b: false, c: nil }
hash.except!(:c) # => { a: true, b: false }
hash # => { a: true, b: false } | except! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/except.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/except.rb | Apache-2.0 |
def symbolize_keys
transform_keys { |key| key.to_sym rescue key }
end | Returns a new hash with all keys converted to symbols, as long as
they respond to +to_sym+.
hash = { 'name' => 'Rob', 'age' => '28' }
hash.symbolize_keys
# => {:name=>"Rob", :age=>"28"} | symbolize_keys | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def symbolize_keys!
transform_keys! { |key| key.to_sym rescue key }
end | Destructively converts all keys to symbols, as long as they respond
to +to_sym+. Same as +symbolize_keys+, but modifies +self+. | symbolize_keys! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def assert_valid_keys(*valid_keys)
valid_keys.flatten!
each_key do |k|
unless valid_keys.include?(k)
raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
end
end
end | Validates all keys in a hash match <tt>*valid_keys</tt>, raising
+ArgumentError+ on a mismatch.
Note that keys are treated differently than HashWithIndifferentAccess,
meaning that string and symbol keys will not match.
{ name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
{ name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
{ name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing | assert_valid_keys | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def deep_transform_keys(&block)
_deep_transform_keys_in_object(self, &block)
end | Returns a new hash with all keys converted by the block operation.
This includes the keys from the root hash and from all
nested hashes and arrays.
hash = { person: { name: 'Rob', age: '28' } }
hash.deep_transform_keys{ |key| key.to_s.upcase }
# => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}} | deep_transform_keys | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def deep_transform_keys!(&block)
_deep_transform_keys_in_object!(self, &block)
end | Destructively converts all keys by using the block operation.
This includes the keys from the root hash and from all
nested hashes and arrays. | deep_transform_keys! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def deep_symbolize_keys
deep_transform_keys { |key| key.to_sym rescue key }
end | Returns a new hash with all keys converted to symbols, as long as
they respond to +to_sym+. This includes the keys from the root hash
and from all nested hashes and arrays.
hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
hash.deep_symbolize_keys
# => {:person=>{:name=>"Rob", :age=>"28"}} | deep_symbolize_keys | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def deep_symbolize_keys!
deep_transform_keys! { |key| key.to_sym rescue key }
end | Destructively converts all keys to symbols, as long as they respond
to +to_sym+. This includes the keys from the root hash and from all
nested hashes and arrays. | deep_symbolize_keys! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def _deep_transform_keys_in_object(object, &block)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
end
when Array
object.map { |e| _deep_transform_keys_in_object(e, &block) }
else
object
end
end | Support methods for deep transforming nested hashes and arrays. | _deep_transform_keys_in_object | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/keys.rb | Apache-2.0 |
def slice!(*keys)
omit = slice(*self.keys - keys)
hash = slice(*keys)
hash.default = default
hash.default_proc = default_proc if default_proc
replace(hash)
omit
end | Replaces the hash with only the given keys.
Returns a hash containing the removed key/value pairs.
hash = { a: 1, b: 2, c: 3, d: 4 }
hash.slice!(:a, :b) # => {:c=>3, :d=>4}
hash # => {:a=>1, :b=>2} | slice! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/slice.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/slice.rb | Apache-2.0 |
def extract!(*keys)
keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
end | Removes and returns the key/value pairs matching the given keys.
hash = { a: 1, b: 2, c: 3, d: 4 }
hash.extract!(:a, :b) # => {:a=>1, :b=>2}
hash # => {:c=>3, :d=>4} | extract! | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/slice.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/hash/slice.rb | Apache-2.0 |
def multiple_of?(number)
number == 0 ? self == 0 : self % number == 0
end | Check whether the integer is evenly divisible by the argument.
0.multiple_of?(0) # => true
6.multiple_of?(5) # => false
10.multiple_of?(2) # => true | multiple_of? | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/integer/multiple.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/integer/multiple.rb | Apache-2.0 |
def concern(topic, &module_definition)
Object.concern topic, &module_definition
end | A shortcut to define a toplevel concern, not within a module.
See Module::Concerning for more. | concern | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/concern.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/concern.rb | Apache-2.0 |
def silence_warnings
with_warnings(nil) { yield }
end | Sets $VERBOSE to +nil+ for the duration of the block and back to its original
value afterwards.
silence_warnings do
value = noisy_call # no warning voiced
end
noisy_call # warning voiced | silence_warnings | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | Apache-2.0 |
def enable_warnings
with_warnings(true) { yield }
end | Sets $VERBOSE to +true+ for the duration of the block and back to its
original value afterwards. | enable_warnings | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | Apache-2.0 |
def with_warnings(flag)
old_verbose, $VERBOSE = $VERBOSE, flag
yield
ensure
$VERBOSE = old_verbose
end | Sets $VERBOSE for the duration of the block and back to its original
value afterwards. | with_warnings | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | Apache-2.0 |
def suppress(*exception_classes)
yield
rescue *exception_classes
end | Blocks and ignores any exception passed as argument if raised within the block.
suppress(ZeroDivisionError) do
1/0
puts 'This code is NOT reached'
end
puts 'This code gets executed and nothing related to ZeroDivisionError was seen' | suppress | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/kernel/reporting.rb | Apache-2.0 |
def alias_attribute(new_name, old_name)
# The following reader methods use an explicit `self` receiver in order to
# support aliases that start with an uppercase letter. Otherwise, they would
# be resolved as constants instead.
module_eval <<-STR, __FILE__, __LINE__ + 1
def #{new_name}; self.#{old_name}; end # def subject; self.title; end
def #{new_name}?; self.#{old_name}?; end # def subject?; self.title?; end
def #{new_name}=(v); self.#{old_name} = v; end # def subject=(v); self.title = v; end
STR
end | Allows you to make aliases for attributes, which includes
getter, setter, and a predicate.
class Content < ActiveRecord::Base
# has a title attribute
end
class Email < Content
alias_attribute :subject, :title
end
e = Email.find(1)
e.title # => "Superstars"
e.subject # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title # => "Megastars" | alias_attribute | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/aliasing.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/aliasing.rb | Apache-2.0 |
def attr_internal_reader(*attrs)
attrs.each { |attr_name| attr_internal_define(attr_name, :reader) }
end | Declares an attribute reader backed by an internally-named instance variable. | attr_internal_reader | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/attr_internal.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/attr_internal.rb | Apache-2.0 |
def attr_internal_writer(*attrs)
attrs.each { |attr_name| attr_internal_define(attr_name, :writer) }
end | Declares an attribute writer backed by an internally-named instance variable. | attr_internal_writer | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/attr_internal.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/attr_internal.rb | Apache-2.0 |
def attr_internal_accessor(*attrs)
attr_internal_reader(*attrs)
attr_internal_writer(*attrs)
end | Declares an attribute reader and writer backed by an internally-named instance
variable. | attr_internal_accessor | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/attr_internal.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/attr_internal.rb | Apache-2.0 |
def concerning(topic, prepend: false, &block)
method = prepend ? :prepend : :include
__send__(method, concern(topic, &block))
end | Define a new concern and mix it in. | concerning | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/concerning.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/concerning.rb | Apache-2.0 |
def concern(topic, &module_definition)
const_set topic, Module.new {
extend ::ActiveSupport::Concern
module_eval(&module_definition)
}
end | A low-cruft shortcut to define a concern.
concern :EventTracking do
...
end
is equivalent to
module EventTracking
extend ActiveSupport::Concern
...
end | concern | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/concerning.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/concerning.rb | Apache-2.0 |
def delegate(*methods, to: nil, prefix: nil, allow_nil: nil, private: nil)
unless to
raise ArgumentError, "Delegation needs a target. Supply a keyword argument 'to' (e.g. delegate :hello, to: :greeter)."
end
if prefix == true && /^[^a-z_]/.match?(to)
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
end
method_prefix = \
if prefix
"#{prefix == true ? to : prefix}_"
else
""
end
location = caller_locations(1, 1).first
file, line = location.path, location.lineno
to = to.to_s
to = "self.#{to}" if DELEGATION_RESERVED_METHOD_NAMES.include?(to)
method_def = []
method_names = []
methods.map do |method|
method_name = prefix ? "#{method_prefix}#{method}" : method
method_names << method_name.to_sym
# Attribute writer methods only accept one argument. Makes sure []=
# methods still accept two arguments.
definition = if /[^\]]=$/.match?(method)
"arg"
elsif RUBY_VERSION >= "2.7"
"..."
else
"*args, &block"
end
# The following generated method calls the target exactly once, storing
# the returned value in a dummy variable.
#
# Reason is twofold: On one hand doing less calls is in general better.
# On the other hand it could be that the target has side-effects,
# whereas conceptually, from the user point of view, the delegator should
# be doing one call.
if allow_nil
method = method.to_s
method_def <<
"def #{method_name}(#{definition})" <<
" _ = #{to}" <<
" if !_.nil? || nil.respond_to?(:#{method})" <<
" _.#{method}(#{definition})" <<
" end" <<
"end"
else
method = method.to_s
method_name = method_name.to_s
method_def <<
"def #{method_name}(#{definition})" <<
" _ = #{to}" <<
" _.#{method}(#{definition})" <<
"rescue NoMethodError => e" <<
" if _.nil? && e.name == :#{method}" <<
%( raise DelegationError, "#{self}##{method_name} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}") <<
" else" <<
" raise" <<
" end" <<
"end"
end
end
module_eval(method_def.join(";"), file, line)
private(*method_names) if private
method_names
end | Provides a +delegate+ class method to easily expose contained objects'
public methods as your own.
==== Options
* <tt>:to</tt> - Specifies the target object name as a symbol or string
* <tt>:prefix</tt> - Prefixes the new method with the target name or a custom prefix
* <tt>:allow_nil</tt> - If set to true, prevents a +Module::DelegationError+
from being raised
* <tt>:private</tt> - If set to true, changes method visibility to private
The macro receives one or more method names (specified as symbols or
strings) and the name of the target object via the <tt>:to</tt> option
(also a symbol or string).
Delegation is particularly useful with Active Record associations:
class Greeter < ActiveRecord::Base
def hello
'hello'
end
def goodbye
'goodbye'
end
end
class Foo < ActiveRecord::Base
belongs_to :greeter
delegate :hello, to: :greeter
end
Foo.new.hello # => "hello"
Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
Multiple delegates to the same target are allowed:
class Foo < ActiveRecord::Base
belongs_to :greeter
delegate :hello, :goodbye, to: :greeter
end
Foo.new.goodbye # => "goodbye"
Methods can be delegated to instance variables, class variables, or constants
by providing them as a symbols:
class Foo
CONSTANT_ARRAY = [0,1,2,3]
@@class_array = [4,5,6,7]
def initialize
@instance_array = [8,9,10,11]
end
delegate :sum, to: :CONSTANT_ARRAY
delegate :min, to: :@@class_array
delegate :max, to: :@instance_array
end
Foo.new.sum # => 6
Foo.new.min # => 4
Foo.new.max # => 11
It's also possible to delegate a method to the class by using +:class+:
class Foo
def self.hello
"world"
end
delegate :hello, to: :class
end
Foo.new.hello # => "world"
Delegates can optionally be prefixed using the <tt>:prefix</tt> option. If the value
is <tt>true</tt>, the delegate methods are prefixed with the name of the object being
delegated to.
Person = Struct.new(:name, :address)
class Invoice < Struct.new(:client)
delegate :name, :address, to: :client, prefix: true
end
john_doe = Person.new('John Doe', 'Vimmersvej 13')
invoice = Invoice.new(john_doe)
invoice.client_name # => "John Doe"
invoice.client_address # => "Vimmersvej 13"
It is also possible to supply a custom prefix.
class Invoice < Struct.new(:client)
delegate :name, :address, to: :client, prefix: :customer
end
invoice = Invoice.new(john_doe)
invoice.customer_name # => 'John Doe'
invoice.customer_address # => 'Vimmersvej 13'
The delegated methods are public by default.
Pass <tt>private: true</tt> to change that.
class User < ActiveRecord::Base
has_one :profile
delegate :first_name, to: :profile
delegate :date_of_birth, to: :profile, private: true
def age
Date.today.year - date_of_birth.year
end
end
User.new.first_name # => "Tomas"
User.new.date_of_birth # => NoMethodError: private method `date_of_birth' called for #<User:0x00000008221340>
User.new.age # => 2
If the target is +nil+ and does not respond to the delegated method a
+Module::DelegationError+ is raised. If you wish to instead return +nil+,
use the <tt>:allow_nil</tt> option.
class User < ActiveRecord::Base
has_one :profile
delegate :age, to: :profile
end
User.new.age
# => Module::DelegationError: User#age delegated to profile.age, but profile is nil
But if not having a profile yet is fine and should not be an error
condition:
class User < ActiveRecord::Base
has_one :profile
delegate :age, to: :profile, allow_nil: true
end
User.new.age # nil
Note that if the target is not +nil+ then the call is attempted regardless of the
<tt>:allow_nil</tt> option, and thus an exception is still raised if said object
does not respond to the method:
class Foo
def initialize(bar)
@bar = bar
end
delegate :name, to: :@bar, allow_nil: true
end
Foo.new("Bar").name # raises NoMethodError: undefined method `name'
The target method must be public, otherwise it will raise +NoMethodError+. | delegate | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/delegation.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/delegation.rb | Apache-2.0 |
def delegate_missing_to(target, allow_nil: nil)
target = target.to_s
target = "self.#{target}" if DELEGATION_RESERVED_METHOD_NAMES.include?(target)
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def respond_to_missing?(name, include_private = false)
# It may look like an oversight, but we deliberately do not pass
# +include_private+, because they do not get delegated.
return false if name == :marshal_dump || name == :_dump
#{target}.respond_to?(name) || super
end
def method_missing(method, *args, &block)
if #{target}.respond_to?(method)
#{target}.public_send(method, *args, &block)
else
begin
super
rescue NoMethodError
if #{target}.nil?
if #{allow_nil == true}
nil
else
raise DelegationError, "\#{method} delegated to #{target}, but #{target} is nil"
end
else
raise
end
end
end
end
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
RUBY
end | When building decorators, a common pattern may emerge:
class Partition
def initialize(event)
@event = event
end
def person
detail.person || creator
end
private
def respond_to_missing?(name, include_private = false)
@event.respond_to?(name, include_private)
end
def method_missing(method, *args, &block)
@event.send(method, *args, &block)
end
end
With <tt>Module#delegate_missing_to</tt>, the above is condensed to:
class Partition
delegate_missing_to :@event
def initialize(event)
@event = event
end
def person
detail.person || creator
end
end
The target can be anything callable within the object, e.g. instance
variables, methods, constants, etc.
The delegated method must be public on the target, otherwise it will
raise +DelegationError+. If you wish to instead return +nil+,
use the <tt>:allow_nil</tt> option.
The <tt>marshal_dump</tt> and <tt>_dump</tt> methods are exempt from
delegation due to possible interference when calling
<tt>Marshal.dump(object)</tt>, should the delegation target method
of <tt>object</tt> add or remove instance variables. | delegate_missing_to | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/delegation.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/delegation.rb | Apache-2.0 |
def deprecate(*method_names)
ActiveSupport::Deprecation.deprecate_methods(self, *method_names)
end | deprecate :foo
deprecate bar: 'message'
deprecate :foo, :bar, baz: 'warning!', qux: 'gone!'
You can also use custom deprecator instance:
deprecate :foo, deprecator: MyLib::Deprecator.new
deprecate :foo, bar: "warning!", deprecator: MyLib::Deprecator.new
\Custom deprecators must respond to <tt>deprecation_warning(deprecated_method_name, message, caller_backtrace)</tt>
method where you can implement your custom warning behavior.
class MyLib::Deprecator
def deprecation_warning(deprecated_method_name, message, caller_backtrace = nil)
message = "#{deprecated_method_name} is deprecated and will be removed from MyLibrary | #{message}"
Kernel.warn message
end
end | deprecate | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/deprecation.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/deprecation.rb | Apache-2.0 |
def module_parent_name
if defined?(@parent_name)
@parent_name
else
parent_name = name =~ /::[^:]+\z/ ? -$` : nil
@parent_name = parent_name unless frozen?
parent_name
end
end | Returns the name of the module containing this one.
M::N.module_parent_name # => "M" | module_parent_name | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/introspection.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/introspection.rb | Apache-2.0 |
def module_parent
module_parent_name ? ActiveSupport::Inflector.constantize(module_parent_name) : Object
end | Returns the module which contains this one according to its name.
module M
module N
end
end
X = M::N
M::N.module_parent # => M
X.module_parent # => M
The parent of top-level and anonymous modules is Object.
M.module_parent # => Object
Module.new.module_parent # => Object | module_parent | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/introspection.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/introspection.rb | Apache-2.0 |
def module_parents
parents = []
if module_parent_name
parts = module_parent_name.split("::")
until parts.empty?
parents << ActiveSupport::Inflector.constantize(parts * "::")
parts.pop
end
end
parents << Object unless parents.include? Object
parents
end | Returns all the parents of this module according to its name, ordered from
nested outwards. The receiver is not contained within the result.
module M
module N
end
end
X = M::N
M.module_parents # => [Object]
M::N.module_parents # => [M, Object]
X.module_parents # => [M, Object] | module_parents | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/introspection.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/introspection.rb | Apache-2.0 |
def silence_redefinition_of_method(method)
if method_defined?(method) || private_method_defined?(method)
# This suppresses the "method redefined" warning; the self-alias
# looks odd, but means we don't need to generate a unique name
alias_method method, method
end
end | Marks the named method as intended to be redefined, if it exists.
Suppresses the Ruby method redefinition warning. Prefer
#redefine_method where possible. | silence_redefinition_of_method | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/redefine_method.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/redefine_method.rb | Apache-2.0 |
def redefine_method(method, &block)
visibility = method_visibility(method)
silence_redefinition_of_method(method)
define_method(method, &block)
send(visibility, method)
end | Replaces the existing method definition, if there is one, with the passed
block as its body. | redefine_method | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/redefine_method.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/redefine_method.rb | Apache-2.0 |
def redefine_singleton_method(method, &block)
singleton_class.redefine_method(method, &block)
end | Replaces the existing singleton method definition, if there is one, with
the passed block as its body. | redefine_singleton_method | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/redefine_method.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/redefine_method.rb | Apache-2.0 |
def remove_possible_method(method)
if method_defined?(method) || private_method_defined?(method)
undef_method(method)
end
end | Removes the named method, if it exists. | remove_possible_method | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/remove_method.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/module/remove_method.rb | Apache-2.0 |
def kilobytes
self * KILOBYTE
end | Returns the number of bytes equivalent to the kilobytes provided.
2.kilobytes # => 2048 | kilobytes | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/numeric/bytes.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/numeric/bytes.rb | Apache-2.0 |
def megabytes
self * MEGABYTE
end | Returns the number of bytes equivalent to the megabytes provided.
2.megabytes # => 2_097_152 | megabytes | ruby | collabnix/kubelabs | .bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/numeric/bytes.rb | https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/activesupport-6.1.7.3/lib/active_support/core_ext/numeric/bytes.rb | Apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.