casual_support 3.0.1 → 3.0.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -20
- data/README.md +1 -0
- data/casual_support.gemspec +6 -4
- data/lib/casual_support.rb +11 -11
- data/lib/casual_support/boolean.rb +1 -1
- data/lib/casual_support/boolean/to_z.rb +0 -0
- data/lib/casual_support/comparable.rb +2 -2
- data/lib/casual_support/comparable/at_least.rb +0 -0
- data/lib/casual_support/comparable/at_most.rb +0 -0
- data/lib/casual_support/date.rb +2 -1
- data/lib/casual_support/date/to_hms.rb +15 -0
- data/lib/casual_support/date/to_ymd.rb +5 -11
- data/lib/casual_support/enumerable.rb +2 -2
- data/lib/casual_support/enumerable/duplicates.rb +2 -2
- data/lib/casual_support/enumerable/index_to.rb +5 -4
- data/lib/casual_support/hash.rb +2 -2
- data/lib/casual_support/hash/displace.rb +0 -0
- data/lib/casual_support/hash/putbang.rb +0 -0
- data/lib/casual_support/integer.rb +1 -1
- data/lib/casual_support/integer/to_hex.rb +3 -1
- data/lib/casual_support/numeric.rb +1 -1
- data/lib/casual_support/numeric/sign.rb +0 -0
- data/lib/casual_support/string.rb +12 -12
- data/lib/casual_support/string/after.rb +2 -2
- data/lib/casual_support/string/after_last.rb +2 -2
- data/lib/casual_support/string/before.rb +1 -1
- data/lib/casual_support/string/before_last.rb +2 -2
- data/lib/casual_support/string/between.rb +0 -0
- data/lib/casual_support/string/drop.rb +1 -1
- data/lib/casual_support/string/first.rb +12 -11
- data/lib/casual_support/string/from.rb +1 -1
- data/lib/casual_support/string/last.rb +12 -11
- data/lib/casual_support/string/prefix.rb +0 -0
- data/lib/casual_support/string/suffix.rb +0 -0
- data/lib/casual_support/string/to.rb +2 -2
- data/lib/casual_support/struct.rb +2 -2
- data/lib/casual_support/struct/assign_attributes.rb +1 -2
- data/lib/casual_support/struct/from_h.rb +7 -5
- data/lib/casual_support/time.rb +2 -2
- data/lib/casual_support/time/to_hms.rb +3 -1
- data/lib/casual_support/time/to_ymd.rb +3 -1
- data/lib/casual_support/version.rb +1 -1
- metadata +4 -29
- data/benchmarks/comparable/at_least.rb +0 -9
- data/benchmarks/comparable/at_most.rb +0 -9
- data/benchmarks/hash/putbang.rb +0 -13
- data/benchmarks/integer/to_hex.rb +0 -34
- data/benchmarks/string/after.rb +0 -22
- data/benchmarks/string/after_last.rb +0 -22
- data/benchmarks/string/before.rb +0 -23
- data/benchmarks/string/before_last.rb +0 -22
- data/benchmarks/string/between.rb +0 -21
- data/benchmarks/string/drop.rb +0 -18
- data/benchmarks/string/first.rb +0 -23
- data/benchmarks/string/from.rb +0 -15
- data/benchmarks/string/last.rb +0 -15
- data/benchmarks/string/prefix.rb +0 -12
- data/benchmarks/string/suffix.rb +0 -12
- data/benchmarks/string/to.rb +0 -15
- data/benchmarks/struct/assign_attributes.rb +0 -69
- data/benchmarks/time/to_hms.rb +0 -12
- data/benchmarks/time/to_ymd.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3530a25d5599a70b7b6d84c2eb83373573f09a38f0b7d071a04ea9820858eb9
|
4
|
+
data.tar.gz: acc967bb9e7a8242883be4dbadb242cda75a961b43abdbe337c06bf9b4ceb3a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a9e64d89fd2b3c920c93ecb186d3d8da6e1e7fed622bf8e9a8d85b420d63b3b0dc92b770cdff3220c6ab250af7642b9f99c634312a5f8bc45a484c2e186b29d
|
7
|
+
data.tar.gz: 5b1e4c16151b2f829315c150709cda75a17f6ba316ea73338db7df4eef2599d6f3643815b5dcc6e070c03d1e1883c4be591e81377ba01fcbdbbe57fc4c6a2a82
|
data/CHANGELOG.md
CHANGED
@@ -1,33 +1,40 @@
|
|
1
|
+
## 3.0.2
|
2
|
+
|
3
|
+
* Add `Date#to_hms` dummy method
|
4
|
+
* Loosen Active Support version constraint
|
5
|
+
* Exclude benchmarks from gem
|
6
|
+
|
7
|
+
|
1
8
|
## 3.0.1
|
2
9
|
|
3
|
-
*
|
10
|
+
* Loosen Active Support version constraint
|
4
11
|
|
5
12
|
|
6
13
|
## 3.0.0
|
7
14
|
|
8
|
-
*
|
9
|
-
*
|
10
|
-
*
|
11
|
-
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*
|
15
|
-
*
|
16
|
-
|
15
|
+
* [BREAKING] Remove block param from `Enumerable#duplicates`
|
16
|
+
* [BREAKING] Remove `Comparable#clamp`. (Native in Ruby 2.4.)
|
17
|
+
* [BREAKING] Remove `String#lchomp`. (Native in Ruby 2.5 as
|
18
|
+
`String#delete_prefix`.)
|
19
|
+
* Add `Numeric#sign`
|
20
|
+
* Add `Boolean#to_z`
|
21
|
+
* Add `Hash#displace`
|
22
|
+
* Add `Struct#assign_attributes`
|
23
|
+
* Add `Struct.from_h`
|
17
24
|
|
18
25
|
|
19
26
|
## 2.0.0
|
20
27
|
|
21
|
-
*
|
22
|
-
*
|
23
|
-
*
|
24
|
-
|
25
|
-
*
|
26
|
-
|
27
|
-
*
|
28
|
-
*
|
29
|
-
*
|
30
|
-
|
28
|
+
* [BREAKING] Rename `Comparable#constrain` to `Comparable#clamp`
|
29
|
+
* [BREAKING] Rename `Hash#set!` to `Hash#put!`
|
30
|
+
* [BREAKING] Change `String#after` and `String#after_last` to return
|
31
|
+
`nil` if delimiter is not found
|
32
|
+
* Add `String#between`
|
33
|
+
* Add `String#prefix`
|
34
|
+
* Add `String#suffix`
|
35
|
+
* Add `String#lchomp`
|
36
|
+
* Move `Numeric#at_least`, `Numeric#at_most`, and `Numeric#constrain` to
|
37
|
+
Comparable
|
31
38
|
|
32
39
|
|
33
40
|
## 1.0.0
|
data/README.md
CHANGED
@@ -25,6 +25,7 @@ itself in a few ways:
|
|
25
25
|
- [#at_least](http://www.rubydoc.info/gems/casual_support/Comparable:at_least)
|
26
26
|
- [#at_most](http://www.rubydoc.info/gems/casual_support/Comparable:at_most)
|
27
27
|
- [Date](http://www.rubydoc.info/gems/casual_support/Date)
|
28
|
+
- [#to_hms](http://www.rubydoc.info/gems/casual_support/Date:to_hms)
|
28
29
|
- [#to_ymd](http://www.rubydoc.info/gems/casual_support/Date:to_ymd)
|
29
30
|
- [Enumerable](http://www.rubydoc.info/gems/casual_support/Enumerable)
|
30
31
|
- [#duplicates](http://www.rubydoc.info/gems/casual_support/Enumerable:duplicates)
|
data/casual_support.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "casual_support/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "casual_support"
|
@@ -13,12 +13,14 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "https://github.com/jonathanhefner/casual_support"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features|benchmarks)/})
|
18
|
+
end
|
17
19
|
spec.bindir = "exe"
|
18
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
21
|
spec.require_paths = ["lib"]
|
20
22
|
|
21
|
-
spec.add_runtime_dependency
|
23
|
+
spec.add_runtime_dependency "activesupport", ">= 3.1"
|
22
24
|
|
23
25
|
spec.add_development_dependency "bundler", "~> 1.12"
|
24
26
|
spec.add_development_dependency "rake", "~> 11.1"
|
data/lib/casual_support.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
1
|
+
require_relative "casual_support/version"
|
2
|
+
require_relative "casual_support/boolean"
|
3
|
+
require_relative "casual_support/comparable"
|
4
|
+
require_relative "casual_support/date"
|
5
|
+
require_relative "casual_support/enumerable"
|
6
|
+
require_relative "casual_support/hash"
|
7
|
+
require_relative "casual_support/integer"
|
8
|
+
require_relative "casual_support/numeric"
|
9
|
+
require_relative "casual_support/string"
|
10
|
+
require_relative "casual_support/struct"
|
11
|
+
require_relative "casual_support/time"
|
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "boolean/to_z"
|
File without changes
|
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative "comparable/at_least"
|
2
|
+
require_relative "comparable/at_most"
|
File without changes
|
File without changes
|
data/lib/casual_support/date.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "date/to_hms"
|
2
|
+
require_relative "date/to_ymd"
|
@@ -1,19 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require "date"
|
2
|
+
|
2
3
|
|
3
4
|
class Date
|
4
5
|
|
5
|
-
# Formats the Date as "YYYY-MM-DD".
|
6
|
-
# +strftime("%Y-%m-%d")+.
|
6
|
+
# Alias of +Date#to_s+. Formats the Date as "YYYY-MM-DD".
|
7
7
|
#
|
8
|
-
#
|
9
|
-
# Date.new(1999, 12, 31).to_ymd # == "1999-12-31"
|
8
|
+
# Provided for parity with {Time#to_hms}.
|
10
9
|
#
|
11
10
|
# @return [String]
|
12
|
-
|
13
|
-
# Date#strftime appears to be **much** faster than Time#strftime
|
14
|
-
# (nearly 3x faster!). Thus it is used here, unlike Time#to_ymd
|
15
|
-
# which uses sprintf.
|
16
|
-
self.strftime('%Y-%m-%d'.freeze)
|
17
|
-
end
|
11
|
+
alias :to_ymd :to_s
|
18
12
|
|
19
13
|
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative "enumerable/duplicates"
|
2
|
+
require_relative "enumerable/index_to"
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Enumerable
|
2
2
|
|
3
|
-
# Returns the first duplicate of each element, preserving
|
4
|
-
# appearance.
|
3
|
+
# Returns the first-appearing duplicate of each element, preserving
|
4
|
+
# order of appearance.
|
5
5
|
#
|
6
6
|
# @example
|
7
7
|
# %w[a a b c c b a d].duplicates # == ["a", "c", "b"]
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "../hash/putbang"
|
2
2
|
|
3
3
|
module Enumerable
|
4
4
|
|
@@ -8,9 +8,10 @@ module Enumerable
|
|
8
8
|
# @example
|
9
9
|
# cache = id_list.index_to{|id| find_by_id(id) }
|
10
10
|
#
|
11
|
-
# @
|
12
|
-
# @
|
13
|
-
# @
|
11
|
+
# @yield [key]
|
12
|
+
# @yieldparam key ['K]
|
13
|
+
# @yieldreturn ['V]
|
14
|
+
# @return [Hash<'K, 'V>]
|
14
15
|
def index_to()
|
15
16
|
self.reduce({}){|h, k| h.put!(k, (yield k)) }
|
16
17
|
end
|
data/lib/casual_support/hash.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative "hash/displace"
|
2
|
+
require_relative "hash/putbang"
|
File without changes
|
File without changes
|
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "integer/to_hex"
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Integer
|
2
4
|
|
3
5
|
# Formats the Integer as a zero-padded lower-case hexadecimal string.
|
@@ -13,7 +15,7 @@ class Integer
|
|
13
15
|
# @param width [Integer]
|
14
16
|
# @return [String]
|
15
17
|
def to_hex(width = 0)
|
16
|
-
width > 1 ? self.to_s(16).rjust(width,
|
18
|
+
width > 1 ? self.to_s(16).rjust(width, "0") : self.to_s(16)
|
17
19
|
end
|
18
20
|
|
19
21
|
end
|
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "numeric/sign"
|
File without changes
|
@@ -1,12 +1,12 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
8
|
-
require_relative
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
1
|
+
require_relative "string/after"
|
2
|
+
require_relative "string/after_last"
|
3
|
+
require_relative "string/before"
|
4
|
+
require_relative "string/before_last"
|
5
|
+
require_relative "string/between"
|
6
|
+
require_relative "string/drop"
|
7
|
+
require_relative "string/first"
|
8
|
+
require_relative "string/from"
|
9
|
+
require_relative "string/last"
|
10
|
+
require_relative "string/prefix"
|
11
|
+
require_relative "string/suffix"
|
12
|
+
require_relative "string/to"
|
@@ -2,8 +2,8 @@ class String
|
|
2
2
|
|
3
3
|
# Searches for the first occurrence of a delimiter, and returns the
|
4
4
|
# portion of the String after that. If the delimiter is not found,
|
5
|
-
# returns nil. Equivalent to
|
6
|
-
# delimiters.
|
5
|
+
# returns nil. Equivalent to <code>split(delimiter, 2).drop(1)[-1]</code>
|
6
|
+
# for non-empty delimiters.
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# "http://www.example.com".after("://") # == "www.example.com"
|
@@ -2,8 +2,8 @@ class String
|
|
2
2
|
|
3
3
|
# Searches for the last occurrence of a delimiter, and returns the
|
4
4
|
# portion of the String after that. If the delimiter is not found,
|
5
|
-
# returns nil. Equivalent to
|
6
|
-
# non-empty delimiters.
|
5
|
+
# returns nil. Equivalent to <code>split(delimiter, -1).drop(1)[-1]</code>
|
6
|
+
# for non-empty delimiters.
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# "/path/to/file".after_last("/") # == "file"
|
@@ -3,7 +3,7 @@ class String
|
|
3
3
|
# Searches for the first occurrence of a delimiter, and returns the
|
4
4
|
# portion of the String before that. If the delimiter is not found,
|
5
5
|
# returns a copy of the original String. Equivalent to
|
6
|
-
#
|
6
|
+
# <code>split(delimiter, 2)[0]</code> for non-empty delimiters.
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# "http://www.example.com".before("://") # == "http"
|
@@ -3,8 +3,8 @@ class String
|
|
3
3
|
# Searches for the last occurrence of a delimiter, and returns the
|
4
4
|
# portion of the String before that. If the delimiter is not found,
|
5
5
|
# returns a copy of the original String. Equivalent to
|
6
|
-
#
|
7
|
-
# delimiters.
|
6
|
+
# <code>split(delimiter, -1)[0...-1].join(delimiter)</code> for
|
7
|
+
# existent delimiters.
|
8
8
|
#
|
9
9
|
# @example
|
10
10
|
# "/path/to/file".before_last("/") # == "/path/to"
|
File without changes
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/string/access"
|
2
2
|
|
3
3
|
class String
|
4
4
|
|
@@ -7,15 +7,16 @@ class String
|
|
7
7
|
# Returns the first +limit+ characters from the beginning of the
|
8
8
|
# String.
|
9
9
|
#
|
10
|
-
# This method replaces Active Support's +String#first
|
11
|
-
# returns an empty string when given a negative +limit
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
# characters." (At most, a negative +limit+ should *keep*
|
16
|
-
# characters from the end of the String,
|
17
|
-
#
|
18
|
-
#
|
10
|
+
# This method replaces Active Support's +String#first+. However, it
|
11
|
+
# returns an empty string when given a negative +limit+, whereas
|
12
|
+
# Active Support 6.0 and previous remove +limit.abs+ characters from
|
13
|
+
# the end of the String. Returning an empty string is more intuitive
|
14
|
+
# behavior if +first+ is interpreted as "keep up to +limit+
|
15
|
+
# characters." (At most, a negative +limit+ should *keep* +limit.abs+
|
16
|
+
# characters from the end of the String, instead of *remove* that many
|
17
|
+
# characters, but returning an empty string is a conservative
|
18
|
+
# compromise.) This method is also faster than Active Support's
|
19
|
+
# implementation.
|
19
20
|
#
|
20
21
|
# @example
|
21
22
|
# "abcdef".first(0) # == ""
|
@@ -27,7 +28,7 @@ class String
|
|
27
28
|
# @param limit [Integer]
|
28
29
|
# @return [String]
|
29
30
|
def first(limit = 1)
|
30
|
-
self[0, limit] ||
|
31
|
+
self[0, limit] || ""
|
31
32
|
end
|
32
33
|
|
33
34
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/string/access"
|
2
2
|
|
3
3
|
class String
|
4
4
|
|
@@ -6,15 +6,16 @@ class String
|
|
6
6
|
|
7
7
|
# Returns the last +limit+ characters from the end of the String.
|
8
8
|
#
|
9
|
-
# This method replaces Active Support's +String#last
|
10
|
-
# returns an empty string when given a negative +limit
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
9
|
+
# This method replaces Active Support's +String#last+. However, it
|
10
|
+
# returns an empty string when given a negative +limit+, whereas
|
11
|
+
# Active Support 6.0 and previous remove +limit.abs+ characters from
|
12
|
+
# the beginning of the String. Returning an empty string is more
|
13
|
+
# intuitive behavior if +last+ is interpreted as "keep up to +limit+
|
14
|
+
# characters." (At most, a negative +limit+ should *keep* +limit.abs+
|
15
|
+
# characters from the beginning of the String, instead of *remove*
|
16
|
+
# that many characters, but returning an empty string is a
|
17
|
+
# conservative compromise.) This method is also faster than Active
|
18
|
+
# Support's implementation.
|
18
19
|
#
|
19
20
|
# @example
|
20
21
|
# "abcdef".last(0) # == ""
|
@@ -26,7 +27,7 @@ class String
|
|
26
27
|
# @param limit [Integer]
|
27
28
|
# @return [String]
|
28
29
|
def last(limit = 1)
|
29
|
-
|
30
|
+
self[[length - limit, 0].max, limit] || ""
|
30
31
|
end
|
31
32
|
|
32
33
|
end
|
File without changes
|
File without changes
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/core_ext/string/access"
|
2
2
|
|
3
3
|
class String
|
4
4
|
|
@@ -19,7 +19,7 @@ class String
|
|
19
19
|
# @return [String]
|
20
20
|
def to(position)
|
21
21
|
position += self.length if position < 0
|
22
|
-
self[0, position + 1] ||
|
22
|
+
self[0, position + 1] || ""
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative "struct/assign_attributes"
|
2
|
+
require_relative "struct/from_h"
|
@@ -9,8 +9,7 @@ class Struct
|
|
9
9
|
# Point = Struct.new(:x, :y, :z)
|
10
10
|
# point = Point.new(10, 20, 30)
|
11
11
|
#
|
12
|
-
# point.assign_attributes(y: 30, z: 50) # ==
|
13
|
-
# point.to_h # == { x: 10, y: 30, z: 50 }
|
12
|
+
# point.assign_attributes(y: 30, z: 50).to_h # == { x: 10, y: 30, z: 50 }
|
14
13
|
#
|
15
14
|
# @param new_attributes [Hash<Symbol, Object>, Hash<String, Object>]
|
16
15
|
# @return [self]
|
@@ -1,19 +1,21 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "assign_attributes"
|
2
2
|
|
3
3
|
class Struct
|
4
4
|
|
5
5
|
# Constructs an instance of a subclass of Struct, and assigns the
|
6
6
|
# values of the given attribute Hash to the instance. This method is
|
7
7
|
# intended for use only with subclasses of Struct which do not alter
|
8
|
-
# the default signature of the +initialize+ method.
|
9
|
-
#
|
8
|
+
# the default signature of the +initialize+ method.
|
9
|
+
#
|
10
|
+
# @see Struct#assign_attributes
|
10
11
|
#
|
11
12
|
# @example
|
12
13
|
# Point = Struct.new(:x, :y, :z)
|
13
|
-
#
|
14
|
+
#
|
15
|
+
# Point.from_h(x: 10, y: 20, z: 30) # == Point.new(10, 20, 30)
|
14
16
|
#
|
15
17
|
# @param attributes [Hash<Symbol, Object>, Hash<String, Object>]
|
16
|
-
# @return [
|
18
|
+
# @return [Struct]
|
17
19
|
def self.from_h(attributes)
|
18
20
|
raise "Struct.from_h is for use only with subclasses of Struct" if self == Struct
|
19
21
|
self.new.assign_attributes(attributes)
|
data/lib/casual_support/time.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative
|
2
|
-
require_relative
|
1
|
+
require_relative "time/to_hms"
|
2
|
+
require_relative "time/to_ymd"
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Time
|
2
4
|
|
3
5
|
# Formats the Time as "HH:MM:SS". Equivalent to
|
@@ -11,7 +13,7 @@ class Time
|
|
11
13
|
# Date#strftime appears to be **much** faster than Time#strftime
|
12
14
|
# (nearly 3x faster!). If Time#strftime becomes optimized to that
|
13
15
|
# level in the future, it should be used instead of sprintf.
|
14
|
-
sprintf(
|
16
|
+
sprintf("%02d:%02d:%02d", hour, min, sec)
|
15
17
|
end
|
16
18
|
|
17
19
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Time
|
2
4
|
|
3
5
|
# Formats the Time as "YYYY-MM-DD". Equivalent to
|
@@ -11,7 +13,7 @@ class Time
|
|
11
13
|
# Date#strftime appears to be **much** faster than Time#strftime
|
12
14
|
# (nearly 3x faster!). If Time#strftime becomes optimized to that
|
13
15
|
# level in the future, it should be used instead of sprintf.
|
14
|
-
sprintf(
|
16
|
+
sprintf("%04d-%02d-%02d", year, month, day)
|
15
17
|
end
|
16
18
|
|
17
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: casual_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Hefner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -17,9 +17,6 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.1'
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '6.0'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +24,6 @@ dependencies:
|
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '3.1'
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '6.0'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: bundler
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,25 +108,6 @@ files:
|
|
114
108
|
- LICENSE.txt
|
115
109
|
- README.md
|
116
110
|
- Rakefile
|
117
|
-
- benchmarks/comparable/at_least.rb
|
118
|
-
- benchmarks/comparable/at_most.rb
|
119
|
-
- benchmarks/hash/putbang.rb
|
120
|
-
- benchmarks/integer/to_hex.rb
|
121
|
-
- benchmarks/string/after.rb
|
122
|
-
- benchmarks/string/after_last.rb
|
123
|
-
- benchmarks/string/before.rb
|
124
|
-
- benchmarks/string/before_last.rb
|
125
|
-
- benchmarks/string/between.rb
|
126
|
-
- benchmarks/string/drop.rb
|
127
|
-
- benchmarks/string/first.rb
|
128
|
-
- benchmarks/string/from.rb
|
129
|
-
- benchmarks/string/last.rb
|
130
|
-
- benchmarks/string/prefix.rb
|
131
|
-
- benchmarks/string/suffix.rb
|
132
|
-
- benchmarks/string/to.rb
|
133
|
-
- benchmarks/struct/assign_attributes.rb
|
134
|
-
- benchmarks/time/to_hms.rb
|
135
|
-
- benchmarks/time/to_ymd.rb
|
136
111
|
- casual_support.gemspec
|
137
112
|
- lib/casual_support.rb
|
138
113
|
- lib/casual_support/boolean.rb
|
@@ -141,6 +116,7 @@ files:
|
|
141
116
|
- lib/casual_support/comparable/at_least.rb
|
142
117
|
- lib/casual_support/comparable/at_most.rb
|
143
118
|
- lib/casual_support/date.rb
|
119
|
+
- lib/casual_support/date/to_hms.rb
|
144
120
|
- lib/casual_support/date/to_ymd.rb
|
145
121
|
- lib/casual_support/enumerable.rb
|
146
122
|
- lib/casual_support/enumerable/duplicates.rb
|
@@ -191,8 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
191
167
|
- !ruby/object:Gem::Version
|
192
168
|
version: '0'
|
193
169
|
requirements: []
|
194
|
-
|
195
|
-
rubygems_version: 2.7.6
|
170
|
+
rubygems_version: 3.0.1
|
196
171
|
signing_key:
|
197
172
|
specification_version: 4
|
198
173
|
summary: Utility extensions to core objects, a la Active Support
|
data/benchmarks/hash/putbang.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
Benchmark.inputs([[1], [*1..10], [*1..100]]) do |job|
|
6
|
-
job.report('reduce{ put! }') do |ary|
|
7
|
-
ary.reduce({}){|h, i| h.put!(i, i) }
|
8
|
-
end
|
9
|
-
job.report('reduce{ merge! }') do |ary|
|
10
|
-
ary.reduce({}){|h, i| h.merge!(i => i) }
|
11
|
-
end
|
12
|
-
job.compare!
|
13
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class Integer
|
6
|
-
|
7
|
-
def to_hex_alt1(width = nil)
|
8
|
-
h = self.to_s(16)
|
9
|
-
width ? h.rjust(width, '0'.freeze) : h
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_hex_alt2(width = 0)
|
13
|
-
h = self.to_s(16)
|
14
|
-
width > h.length ? h.rjust(width, '0'.freeze) : h
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
Benchmark.inputs([*0..8]) do |job|
|
21
|
-
job.report('Integer#to_hex'){|width| (210).to_hex(width) }
|
22
|
-
job.report('Integer#to_hex_alt1'){|width| (210).to_hex_alt1(width) }
|
23
|
-
job.report('Integer#to_hex_alt2'){|width| (210).to_hex_alt2(width) }
|
24
|
-
job.report('sprintf'){|width| sprintf("%0#{width}x", 210) }
|
25
|
-
job.compare!
|
26
|
-
end
|
27
|
-
|
28
|
-
Benchmark.inputs([nil]) do |job|
|
29
|
-
job.report('Integer#to_hex'){|width| (210).to_hex }
|
30
|
-
job.report('Integer#to_hex_alt1'){|width| (210).to_hex_alt1 }
|
31
|
-
job.report('Integer#to_hex_alt2'){|width| (210).to_hex_alt2 }
|
32
|
-
job.report('Integer#to_s'){|width| (210).to_s(16) }
|
33
|
-
job.compare!
|
34
|
-
end
|
data/benchmarks/string/after.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class String
|
6
|
-
|
7
|
-
def after_alt1(delim)
|
8
|
-
i = self.index(delim)
|
9
|
-
i && self.drop(i + delim.length)
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
STRING = 'abcnoonxyz'
|
16
|
-
|
17
|
-
Benchmark.inputs(STRING.chars << '_') do |job|
|
18
|
-
job.report('String#after'){|delim| STRING.after(delim) }
|
19
|
-
job.report('String#after_alt1'){|delim| STRING.after_alt1(delim) }
|
20
|
-
job.report('String#split'){|delim| STRING.split(delim, 2)[1] }
|
21
|
-
job.compare!
|
22
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class String
|
6
|
-
|
7
|
-
def after_last_alt1(delim)
|
8
|
-
i = self.rindex(delim)
|
9
|
-
i && self.drop(i + delim.length)
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
STRING = 'abcnoonxyz'
|
16
|
-
|
17
|
-
Benchmark.inputs(STRING.chars << '_') do |job|
|
18
|
-
job.report('String#after_last'){|delim| STRING.after_last(delim) }
|
19
|
-
job.report('String#after_last_alt1'){|delim| STRING.after_last_alt1(delim) }
|
20
|
-
job.report('String#split'){|delim| STRING.split(delim, -1).drop(1).last }
|
21
|
-
job.compare!
|
22
|
-
end
|
data/benchmarks/string/before.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class String
|
6
|
-
|
7
|
-
def before_alt1(delim)
|
8
|
-
i = self.index(delim)
|
9
|
-
i ? self.first(i) : self.dup
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
STRING = 'abcnoonxyz'
|
17
|
-
|
18
|
-
Benchmark.inputs(STRING.chars << '_') do |job|
|
19
|
-
job.report('String#before'){|delim| STRING.before(delim) }
|
20
|
-
job.report('String#before_alt1'){|delim| STRING.before_alt1(delim) }
|
21
|
-
job.report('String#split'){|delim| STRING.split(delim, 2).first }
|
22
|
-
job.compare!
|
23
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class String
|
6
|
-
|
7
|
-
def before_last_alt1(delim)
|
8
|
-
i = self.rindex(delim)
|
9
|
-
i ? self.first(i) : self.dup
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
STRING = 'abcnoonxyz'
|
16
|
-
|
17
|
-
Benchmark.inputs(STRING.chars << '_') do |job|
|
18
|
-
job.report('String#before_last'){|delim| STRING.before_last(delim) }
|
19
|
-
job.report('String#before_last_alt1'){|delim| STRING.before_last_alt1(delim) }
|
20
|
-
job.report('String#split'){|delim| STRING.split(delim, -1)[0..-2].join(delim) }
|
21
|
-
job.compare!
|
22
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class String
|
6
|
-
|
7
|
-
def between_alt1(open, close)
|
8
|
-
s = self.after(open)
|
9
|
-
s && s.before(close) # fails to return nil if `close` is missing
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
STRING = 'key="111-delim-222-delim-333"'
|
16
|
-
|
17
|
-
Benchmark.inputs(['"', '-delim-', '!']) do |job|
|
18
|
-
job.report('String#between'){|delim| STRING.between(delim, delim) }
|
19
|
-
job.report('String#between_alt1'){|delim| STRING.between_alt1(delim, delim) }
|
20
|
-
job.compare!
|
21
|
-
end
|
data/benchmarks/string/drop.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class String
|
6
|
-
def drop_alt1(n)
|
7
|
-
self[n.constrain(0, self.length), self.length]
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
|
12
|
-
STRING = 'abcdef'
|
13
|
-
|
14
|
-
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
15
|
-
job.report('String#drop'){|n| STRING.drop(n) }
|
16
|
-
job.report('String#drop_alt1'){|n| STRING.drop_alt1(n) }
|
17
|
-
job.compare!
|
18
|
-
end
|
data/benchmarks/string/first.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require 'active_support/all'
|
3
|
-
class String
|
4
|
-
alias_method :old_first, :first
|
5
|
-
end
|
6
|
-
require_relative '../../lib/casual_support'
|
7
|
-
|
8
|
-
|
9
|
-
class String
|
10
|
-
def first_alt1(limit = 1)
|
11
|
-
limit <= 0 ? '' : self[0, limit]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
|
16
|
-
STRING = 'abcdef'
|
17
|
-
|
18
|
-
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
19
|
-
job.report('casual_support String#first'){|n| STRING.first(n) }
|
20
|
-
job.report('casual_support String#first_alt1'){|n| STRING.first_alt1(n) }
|
21
|
-
job.report('active_support String#first'){|n| STRING.old_first(n) }
|
22
|
-
job.compare!
|
23
|
-
end
|
data/benchmarks/string/from.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require 'active_support/all'
|
3
|
-
class String
|
4
|
-
alias_method :old_from, :from
|
5
|
-
end
|
6
|
-
require_relative '../../lib/casual_support'
|
7
|
-
|
8
|
-
|
9
|
-
STRING = 'abcdef'
|
10
|
-
|
11
|
-
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
12
|
-
job.report('casual_support String#from'){|n| STRING.from(n) }
|
13
|
-
job.report('active_support String#from'){|n| STRING.old_from(n) }
|
14
|
-
job.compare!
|
15
|
-
end
|
data/benchmarks/string/last.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require 'active_support/all'
|
3
|
-
class String
|
4
|
-
alias_method :old_last, :last
|
5
|
-
end
|
6
|
-
require_relative '../../lib/casual_support'
|
7
|
-
|
8
|
-
|
9
|
-
STRING = 'abcdef'
|
10
|
-
|
11
|
-
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
12
|
-
job.report('casual_support String#last'){|n| STRING.last(n) }
|
13
|
-
job.report('active_support String#last'){|n| STRING.old_last(n) }
|
14
|
-
job.compare!
|
15
|
-
end
|
data/benchmarks/string/prefix.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
PREFIX = 'abc'
|
6
|
-
PREFIX_REGEX = /^(?!abc)/
|
7
|
-
|
8
|
-
Benchmark.inputs(['xyz', 'abxyz', 'abcxyz', '']) do |job|
|
9
|
-
job.report('String#prefix'){|s| s.prefix(PREFIX) }
|
10
|
-
job.report('String#gsub'){|s| s.gsub(PREFIX_REGEX, PREFIX) }
|
11
|
-
job.compare!
|
12
|
-
end
|
data/benchmarks/string/suffix.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
SUFFIX = 'xyz'
|
6
|
-
SUFFIX_REGEX = /^(?!xyz)/
|
7
|
-
|
8
|
-
Benchmark.inputs(['abc', 'abcyz', 'abcxyz', '']) do |job|
|
9
|
-
job.report('String#suffix'){|s| s.suffix(SUFFIX) }
|
10
|
-
job.report('String#gsub'){|s| s.gsub(SUFFIX_REGEX, SUFFIX) }
|
11
|
-
job.compare!
|
12
|
-
end
|
data/benchmarks/string/to.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require 'active_support/all'
|
3
|
-
class String
|
4
|
-
alias_method :old_to, :to
|
5
|
-
end
|
6
|
-
require_relative '../../lib/casual_support'
|
7
|
-
|
8
|
-
|
9
|
-
STRING = 'abcdef'
|
10
|
-
|
11
|
-
Benchmark.inputs([*0..(STRING.length + 1)]) do |job|
|
12
|
-
job.report('casual_support String#to'){|n| STRING.to(n) }
|
13
|
-
job.report('active_support String#to'){|n| STRING.old_to(n) }
|
14
|
-
job.compare!
|
15
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
class Struct
|
6
|
-
|
7
|
-
def assign_attributes_alt1(new_attributes)
|
8
|
-
self.members.each do |m|
|
9
|
-
if new_attributes.key?(m)
|
10
|
-
self[m] = new_attributes[m]
|
11
|
-
elsif new_attributes.key?(m.to_s)
|
12
|
-
self[m] = new_attributes[m.to_s]
|
13
|
-
end
|
14
|
-
end
|
15
|
-
self
|
16
|
-
end
|
17
|
-
|
18
|
-
def assign_attributes_alt2(new_attributes)
|
19
|
-
self.members.each do |m|
|
20
|
-
if new_attributes.key?(m)
|
21
|
-
self[m] = new_attributes[m]
|
22
|
-
elsif new_attributes.key?(m_s = m.to_s)
|
23
|
-
self[m] = new_attributes[m_s]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
self
|
27
|
-
end
|
28
|
-
|
29
|
-
def assign_attributes_alt3(new_attributes)
|
30
|
-
new_attributes.each do |k, v|
|
31
|
-
self[k.to_sym] = v if self.members.include?(k.to_sym)
|
32
|
-
end
|
33
|
-
self
|
34
|
-
end
|
35
|
-
|
36
|
-
def assign_attributes_alt4(new_attributes)
|
37
|
-
new_attributes.each do |k, v|
|
38
|
-
m = k.to_sym
|
39
|
-
self[m] = v if self.members.include?(m)
|
40
|
-
end
|
41
|
-
self
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
Subject = Struct.new(:attr1, :attr2, :attr3)
|
48
|
-
subject = Subject.new(1, 2, 3)
|
49
|
-
|
50
|
-
input_attributes = [
|
51
|
-
{ attr1: 11 },
|
52
|
-
{ attr1: 111, attr2: 222 },
|
53
|
-
{ attr1: 1111, attr2: 2222, attr3: 3333 },
|
54
|
-
{ attr1: 11111, attr2: 22222, attr3: 33333, attr9: 99999 },
|
55
|
-
{ "attr1" => 11 },
|
56
|
-
{ "attr1" => 111, "attr2" => 222 },
|
57
|
-
{ "attr1" => 1111, "attr2" => 2222, "attr3" => 3333 },
|
58
|
-
{ "attr1" => 11111, "attr2" => 22222, "attr3" => 33333, "attr9" => 99999 },
|
59
|
-
]
|
60
|
-
|
61
|
-
|
62
|
-
Benchmark.inputs(input_attributes) do |job|
|
63
|
-
job.report('Struct#assign_attributes'){|attrs| subject.assign_attributes(attrs) }
|
64
|
-
job.report('Struct#assign_attributes_alt1'){|attrs| subject.assign_attributes_alt1(attrs) }
|
65
|
-
job.report('Struct#assign_attributes_alt2'){|attrs| subject.assign_attributes_alt2(attrs) }
|
66
|
-
job.report('Struct#assign_attributes_alt3'){|attrs| subject.assign_attributes_alt3(attrs) }
|
67
|
-
job.report('Struct#assign_attributes_alt4'){|attrs| subject.assign_attributes_alt4(attrs) }
|
68
|
-
job.compare!
|
69
|
-
end
|
data/benchmarks/time/to_hms.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
FORMAT = '%H:%M:%S'
|
6
|
-
|
7
|
-
Benchmark.inputs([Time.new(1999, 12, 31, 23, 59, 59)]) do |job|
|
8
|
-
job.report('Time#to_hms'){|t| t.to_hms }
|
9
|
-
job.report('Time#strftime'){|t| t.strftime('%H:%M:%S') }
|
10
|
-
job.report('Time#strftime(CONST)'){|t| t.strftime(FORMAT) }
|
11
|
-
job.compare!
|
12
|
-
end
|
data/benchmarks/time/to_ymd.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'benchmark/inputs'
|
2
|
-
require_relative '../../lib/casual_support'
|
3
|
-
|
4
|
-
|
5
|
-
FORMAT = '%Y-%m-%d'
|
6
|
-
|
7
|
-
Benchmark.inputs([Time.new(1999, 12, 31, 23, 59, 59)]) do |job|
|
8
|
-
job.report('Time#to_ymd'){|t| t.to_ymd }
|
9
|
-
job.report('Time#strftime'){|t| t.strftime('%Y-%m-%d') }
|
10
|
-
job.report('Time#strftime(CONST)'){|t| t.strftime(FORMAT) }
|
11
|
-
job.compare!
|
12
|
-
end
|