casual_support 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8914e689b15b07fbb5628b8745f9b1de4b5d65f3
4
+ data.tar.gz: 105d5b5411cb70b844f5ceb3c7ea17bd652e2d3f
5
+ SHA512:
6
+ metadata.gz: 78de00d5d5e5d3c970f3747ec4131aa1fbd83ae361825d46f558e39198fd16fa72402d626a4d48da9c744baf578d1c0ff422403a5682ecfbaf0fde9ead0d5807
7
+ data.tar.gz: 1503f2ba9311fc72509d78f09bf91ae6a12b533465a135f6ef457160e21a97ead6e1f23ce72659b8bebb579e6e071228a6316947307f980ced38b94c7abed360
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.12.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in casual_support.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ # casual_support
2
+
3
+ Utility methods as extensions to Ruby core objects. Stuff I want in Active Support.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'casual_support'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install casual_support
21
+
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "casual_support"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'casual_support/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "casual_support"
8
+ spec.version = CasualSupport::VERSION
9
+ spec.authors = ["Jonathan Hefner"]
10
+ spec.email = ["jonathan.hefner@gmail.com"]
11
+
12
+ spec.summary = %q{Utility methods as core extensions.}
13
+ spec.description = %q{Utility methods as extensions to Ruby core objects. Stuff I want in Active Support.}
14
+ spec.homepage = "https://github.com/jonathanhefner/casual_support"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'activesupport', '>= 3.1', '< 5.1'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'casual_support/version'
2
+ require 'casual_support/date'
3
+ require 'casual_support/enumerable'
4
+ require 'casual_support/hash'
5
+ require 'casual_support/integer'
6
+ require 'casual_support/numeric'
7
+ require 'casual_support/string'
8
+ require 'casual_support/time'
@@ -0,0 +1 @@
1
+ require 'casual_support/date/to_ymd'
@@ -0,0 +1,16 @@
1
+ require 'date'
2
+
3
+ class Date
4
+
5
+ # Formats date as "YYYY-MM-DD" (e.g. "1999-12-31"). Shortcut for
6
+ # +strftime("%Y-%m-%d")+.
7
+ #
8
+ # @return [String] the date formatted as "YYYY-MM-DD"
9
+ def to_ymd
10
+ # Date#strftime appears to be **much** faster than Time#strftime
11
+ # (nearly 3x faster!). Thus it used here, unlike Time#to_ymd which
12
+ # uses sprintf.
13
+ self.strftime('%Y-%m-%d'.freeze)
14
+ end
15
+
16
+ end
@@ -0,0 +1,2 @@
1
+ require 'casual_support/enumerable/duplicates'
2
+ require 'casual_support/enumerable/index_to'
@@ -0,0 +1,24 @@
1
+ module Enumerable
2
+
3
+ # Returns the first duplicate of each element, preserving order of
4
+ # appearance. If a block is given, it will use the return
5
+ # value of the block for element comparison.
6
+ #
7
+ # Examples:
8
+ # %w[a a b c d c b a].duplicates == %w[a c b]
9
+ # %w[A a B b].duplicates(&:upcase) == %w[a b]
10
+ #
11
+ # @yield [elem] computes an indentifying value
12
+ # @yieldparam elem element from the +Enumerable+
13
+ # @yieldreturn value used for duplicate comparison
14
+ # @return [Enumerable] duplicate elements of the +Enumerable+
15
+ def duplicates
16
+ seen = Hash.new(0)
17
+ if block_given?
18
+ self.select{|elem| (seen[yield(elem)] += 1) == 2 }
19
+ else
20
+ self.select{|elem| (seen[elem] += 1) == 2 }
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,16 @@
1
+ require 'casual_support/hash/setbang'
2
+
3
+ module Enumerable
4
+
5
+ # Converts an +Enumerable+ into a +Hash+ by using its elements as keys
6
+ # and extracting values from each.
7
+ #
8
+ # @yield [elem] extracts a value
9
+ # @yieldparam elem element from the +Enumerable+
10
+ # @yieldreturn value to associate with the +elem+ key
11
+ # @return [Hash] hash with the +Enumerable+'s elements as keys
12
+ def index_to()
13
+ self.reduce({}){|h, k| h.set!(k, (yield k)) }
14
+ end
15
+
16
+ end
@@ -0,0 +1 @@
1
+ require 'casual_support/hash/setbang'
@@ -0,0 +1,16 @@
1
+ class Hash
2
+
3
+ # Sets a value like +Hash#[]=+, but returns the modified Hash instead
4
+ # of the value. Useful with +#reduce+, and 2x faster than using
5
+ # an equivalent +Hash#merge!+ (see
6
+ # benchmark[https://github.com/JuanitoFatas/fast-ruby#hashmerge-vs-hash-code]).
7
+ #
8
+ # @param key
9
+ # @param value
10
+ # @return [Hash] the modified hash
11
+ def set!(key, value)
12
+ self[key] = value
13
+ self
14
+ end
15
+
16
+ end
@@ -0,0 +1 @@
1
+ require 'casual_support/integer/to_hex'
@@ -0,0 +1,20 @@
1
+ class Integer
2
+
3
+ # Converts integer to a zero-padded lower-case hexadecimal string.
4
+ # If the length of the raw hexadecimal string exceeds the desired
5
+ # width, the string will be returned unaltered (without padding or
6
+ # truncation).
7
+ #
8
+ # Without the need for zero-padding, this method is ~7% slower than
9
+ # just +to_s(16)+. However, when zero-padding is required, it is
10
+ # ~230% faster than +sprintf("%0#{width}x", self)+, and ~30% faster
11
+ # than +sprintf(FIXED_FORMAT_STRING, self)+.
12
+ #
13
+ # @param [Integer] width desired width of the zero-padded string
14
+ # @return [String] hexadecimal string
15
+ def to_hex(width = nil)
16
+ h = self.to_s(16)
17
+ width ? h.rjust(width, '0'.freeze) : h
18
+ end
19
+
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'casual_support/numeric/at_least_most'
2
+ require 'casual_support/numeric/constrain'
@@ -0,0 +1,21 @@
1
+ class Numeric
2
+
3
+ # Enforces a lower bound for a number.
4
+ #
5
+ # @param [Numeric] limit lower bound
6
+ # @return [Numeric] the number constrained by the lower bound
7
+ def at_least(limit)
8
+ self < limit ? limit : self
9
+ end
10
+
11
+ # Enforces an upper bound for a number.
12
+ #
13
+ # @param [Numeric] limit upper bound
14
+ # @return [Numeric] the number constrained by the upper bound
15
+ def at_most(limit)
16
+ self > limit ? limit : self
17
+ end
18
+
19
+ alias_method :cap, :at_most
20
+
21
+ end
@@ -0,0 +1,16 @@
1
+ class Numeric
2
+
3
+ # Constrains a number to a closed interval.
4
+ #
5
+ # @param [Numeric] low lower bound
6
+ # @param [Numeric] high upper bound
7
+ # @return [Numeric] the number constrained to the bounds
8
+ def constrain(low, high)
9
+ return low if self < low
10
+ return high if self > high
11
+ self
12
+ end
13
+
14
+ alias_method :clamp, :constrain
15
+
16
+ end
@@ -0,0 +1,4 @@
1
+ require 'casual_support/string/before_after'
2
+ require 'casual_support/string/drop'
3
+ require 'casual_support/string/first_last'
4
+ require 'casual_support/string/from_to'
@@ -0,0 +1,59 @@
1
+ class String
2
+
3
+ # Searches for the first occurrence of a delimiter, and returns the
4
+ # portion of the string before that. If the delimiter is not found,
5
+ # returns the original string.
6
+ #
7
+ # Semantically equivalent to:
8
+ # split(delim).first
9
+ #
10
+ # @param [String] delim delimiter to search for
11
+ # @return [String] portion of the string before the first +delim+
12
+ def before(delim)
13
+ i = self.index(delim)
14
+ i ? self.first(i) : self
15
+ end
16
+
17
+ # Searches for the last occurrence of a delimiter, and returns the
18
+ # portion of the string before that. If the delimiter is not found,
19
+ # returns the original string.
20
+ #
21
+ # Semantically equivalent to:
22
+ # split(delim)[0..-2].join(delim)
23
+ #
24
+ # @param [String] delim delimiter to search for
25
+ # @return [String] portion of the string before the last +delim+
26
+ def before_last(delim)
27
+ i = self.rindex(delim)
28
+ i ? self.first(i) : self
29
+ end
30
+
31
+ # Searches for the first occurrence of a delimiter, and returns the
32
+ # portion of the string after that. If the delimiter is not found,
33
+ # returns the original string.
34
+ #
35
+ # Semantically equivalent to:
36
+ # split(delim, 2).last
37
+ #
38
+ # @param [String] delim delimiter to search for
39
+ # @return [String] portion of the string after the first +delim+
40
+ def after(delim)
41
+ i = self.index(delim)
42
+ i ? self.drop(i + delim.length) : self
43
+ end
44
+
45
+ # Searches for the last occurrence of a delimiter, and returns the
46
+ # portion of the string after that. If the delimiter is not found,
47
+ # returns the original string.
48
+ #
49
+ # Semantically equivalent to:
50
+ # split(delim).last
51
+ #
52
+ # @param [String] delim delimiter to search for
53
+ # @return [String] portion of the string after the last +delim+
54
+ def after_last(delim)
55
+ i = self.rindex(delim)
56
+ i ? self.drop(i + delim.length) : self
57
+ end
58
+
59
+ end
@@ -0,0 +1,13 @@
1
+ class String
2
+
3
+ # Drops characters from the beginning of a string, and returns the
4
+ # remainder. If the number of characters to drop is greater than the
5
+ # length of the string, an empty string is returned.
6
+ #
7
+ # @param [Integer] n number of characters to drop
8
+ # @return [String] string of remaining characters
9
+ def drop(n)
10
+ self[n.constrain(0, self.length), self.length]
11
+ end
12
+
13
+ end
@@ -0,0 +1,15 @@
1
+ class String
2
+
3
+ # This replaces Active Support's +String#first+, but it returns an
4
+ # empty string when given a negative argument. It is ~75% faster.
5
+ def first(limit = 1)
6
+ limit <= 0 ? '' : self[0, limit]
7
+ end
8
+
9
+ # This replaces Active Support's +String#last+, but it returns an
10
+ # empty string when given a negative argument. It is ~75% faster.
11
+ def last(limit = 1)
12
+ limit <= 0 ? '' : self[length - limit, length]
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ class String
2
+
3
+ # This replaces Active Support's +String#from+. It is ~40% faster.
4
+ def from(position)
5
+ position += self.length if position < 0
6
+ self[position, length]
7
+ end
8
+
9
+ # This replaces Active Support's +String#to+. It is ~40% faster.
10
+ def to(position)
11
+ position += self.length if position < 0
12
+ self[0, position + 1]
13
+ end
14
+
15
+ end
@@ -0,0 +1 @@
1
+ require 'casual_support/time/ymd_hms'
@@ -0,0 +1,27 @@
1
+ class Time
2
+
3
+ # Formats time as "YYYY-MM-DD" (e.g. "1999-12-31"). Equivalent to
4
+ # +strftime("%Y-%m-%d")+, but ~6% faster (or ~18% if the format
5
+ # string is an un-frozen literal, due to GC).
6
+ #
7
+ # @return [String] the time formatted as "YYYY-MM-DD"
8
+ def to_ymd
9
+ # Date#strftime appears to be **much** faster than Time#strftime
10
+ # (nearly 3x faster!). If Time#strftime becomes optimized to that
11
+ # level in the future, it should be used instead of sprintf.
12
+ sprintf('%04d-%02d-%02d'.freeze, year, month, day)
13
+ end
14
+
15
+ # Formats time as "HH:MM:SS" (e.g. "23:59:59"). Equivalent to
16
+ # +strftime("%H:%M:%S")+, but ~10% faster (or ~20% if the format
17
+ # string is an un-frozen literal, due to GC).
18
+ #
19
+ # @return [String] the time formatted as "HH:MM:SS"
20
+ def to_hms
21
+ # Date#strftime appears to be **much** faster than Time#strftime
22
+ # (nearly 3x faster!). If Time#strftime becomes optimized to that
23
+ # level in the future, it should be used instead of sprintf.
24
+ sprintf('%02d:%02d:%02d'.freeze, hour, min, sec)
25
+ end
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ module CasualSupport
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: casual_support
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Hefner
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.12'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.12'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.0'
75
+ description: Utility methods as extensions to Ruby core objects. Stuff I want in
76
+ Active Support.
77
+ email:
78
+ - jonathan.hefner@gmail.com
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - casual_support.gemspec
92
+ - lib/casual_support.rb
93
+ - lib/casual_support/date.rb
94
+ - lib/casual_support/date/to_ymd.rb
95
+ - lib/casual_support/enumerable.rb
96
+ - lib/casual_support/enumerable/duplicates.rb
97
+ - lib/casual_support/enumerable/index_to.rb
98
+ - lib/casual_support/hash.rb
99
+ - lib/casual_support/hash/setbang.rb
100
+ - lib/casual_support/integer.rb
101
+ - lib/casual_support/integer/to_hex.rb
102
+ - lib/casual_support/numeric.rb
103
+ - lib/casual_support/numeric/at_least_most.rb
104
+ - lib/casual_support/numeric/constrain.rb
105
+ - lib/casual_support/string.rb
106
+ - lib/casual_support/string/before_after.rb
107
+ - lib/casual_support/string/drop.rb
108
+ - lib/casual_support/string/first_last.rb
109
+ - lib/casual_support/string/from_to.rb
110
+ - lib/casual_support/time.rb
111
+ - lib/casual_support/time/ymd_hms.rb
112
+ - lib/casual_support/version.rb
113
+ homepage: https://github.com/jonathanhefner/casual_support
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.8
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Utility methods as core extensions.
137
+ test_files: []
138
+ has_rdoc: