tektite_ruby_utils 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +111 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tektite_ruby_utils.rb +6 -0
- data/lib/tektite_ruby_utils/present.rb +142 -0
- data/lib/tektite_ruby_utils/version.rb +3 -0
- data/tektite_ruby_utils.gemspec +28 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ced93f4b862080bb3a4cd663847b1edef5d5c187
|
4
|
+
data.tar.gz: 9de3e436c331aaa6f78b47a3fe1461c060df472f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d2b2e35f9018b6681e1675a6de3a6e5cee8426aa9374bb37f4e7f7dfc113f239a27188e46725cdb8bd47a4bac151596aeed0838dbd15328e0615eec8d3beb03
|
7
|
+
data.tar.gz: 3d11e63dcd249bbb0f1804c6cd779da24707c45f29810c86bacf88eb0343049a298dd7b034617325ea812098747c738f2cf74a2d15e14179adb238e6a8b8c2ec
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Xavier Bick
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
# Tektite's Ruby Utils
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/tektite_ruby_utils) [](https://travis-ci.org/tektite-software/ruby_utils) [](https://codeclimate.com/github/tektite-software/ruby_utils) [](https://codeclimate.com/github/tektite-software/ruby_utils/coverage) [](http://inch-ci.org/github/tektite-software/ruby_utils)
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
This gem contains a number of utilities, extensions, and helpers for Ruby to make developing applications easier and to enable better security and integration with other services. Enjoy! Suggestions are most welcome.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'tektite_ruby_utils'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install tektite_ruby_utils
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Below is a list of everything this gem includes.
|
28
|
+
|
29
|
+
### PresentClass and `present`
|
30
|
+
|
31
|
+
`PresentClass` adds a new class and related helpers designed to be the opposite of `NilClass`, and includes `present` in the global namespace as the opposite of `nil`. It is used to represent that a value is __not nil__ without exposing the value itself. Unlike simple using `!nil?`, PresentClass returns an object that can be configured to carry variable amounts of data about the value behind it, such as the object's type and class. `PRESENT` is also included as a constant, which is a frozen instance of PresentClass that `present` returns.
|
32
|
+
|
33
|
+
Furthermore, the extensions includes several helpers for working with any object, Hashes, and Arrays.
|
34
|
+
|
35
|
+
This is extremely useful if for example you would like to pass information about whether certain data _exists_ to a method without telling the method what the actual values of that data are.
|
36
|
+
|
37
|
+
For any object, you can use the `present?` method:
|
38
|
+
|
39
|
+
class MyObject
|
40
|
+
attr_accessor :my_value
|
41
|
+
...
|
42
|
+
end
|
43
|
+
|
44
|
+
my_object = MyObject.new my_value: "test"
|
45
|
+
|
46
|
+
my_object.my_value.present?
|
47
|
+
# => true
|
48
|
+
|
49
|
+
nil.present?
|
50
|
+
# => false
|
51
|
+
|
52
|
+
present.present?
|
53
|
+
# => true
|
54
|
+
|
55
|
+
present.nil?
|
56
|
+
# => false
|
57
|
+
|
58
|
+
nil
|
59
|
+
# => nil
|
60
|
+
|
61
|
+
present
|
62
|
+
# => present
|
63
|
+
|
64
|
+
For Arrays and Hashes, the `all_present?`, `each_present?`, and `mask_present` helper methods are available:
|
65
|
+
|
66
|
+
array = [1, 2, nil]
|
67
|
+
array.all_present?
|
68
|
+
# => false
|
69
|
+
array.each_present?
|
70
|
+
# => [true, true, false]
|
71
|
+
array.mask_present
|
72
|
+
# => [present, present, nil]
|
73
|
+
|
74
|
+
hash = {one: 1, two: 2, three: 3}
|
75
|
+
hash.all_present?
|
76
|
+
# => true
|
77
|
+
hash.each_present?
|
78
|
+
# => {one: true, two: true, three: true}
|
79
|
+
hash.mask_present
|
80
|
+
# => {one: present, two: present, three: present}
|
81
|
+
|
82
|
+
If you would like to return an instance of PresentClass that includes the type of the original value, just create a new instance of PresentClass directly specifying the `:type` attribute as a Class. For all other users, simply set a variable to `present` as if you were using `nil`. As a helper for this, the `present_with_type` method is available:
|
83
|
+
|
84
|
+
test_array = [1, 'two', :three]
|
85
|
+
test_array.each do |e|
|
86
|
+
puts e.present_with_type.type
|
87
|
+
puts "\n"
|
88
|
+
end
|
89
|
+
|
90
|
+
# => Fixnum
|
91
|
+
# => String
|
92
|
+
# => Symbol
|
93
|
+
|
94
|
+
## Documentation
|
95
|
+
|
96
|
+
Documentation for this gem can be found here: http://www.rubydoc.info/github/tektite-software/ruby_utils/
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
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.
|
101
|
+
|
102
|
+
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).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tektite-software/ruby_utils.
|
107
|
+
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'tektite_ruby_utils'
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# PresentClass should behave just as NilClass, but the opposite. It represents
|
2
|
+
# that a value exists but does not represent any specific vallue or type; it
|
3
|
+
# is ambiguous. present should be seen as the opposite of nil.
|
4
|
+
class PresentClass
|
5
|
+
attr_reader :type
|
6
|
+
|
7
|
+
# +type+ should be either nil (for ambiguous) or
|
8
|
+
# the class of the object represented.
|
9
|
+
def initialize(type = nil)
|
10
|
+
@type = type
|
11
|
+
end
|
12
|
+
|
13
|
+
# Makes present appear and behave like nil in the console. To see the
|
14
|
+
# object id, use +.object_id+. To see the value of an attribute
|
15
|
+
# such as +@type+, use the attribute getter method, such as .type.
|
16
|
+
def inspect
|
17
|
+
'present'
|
18
|
+
end
|
19
|
+
|
20
|
+
# To avoid confusing the present Object with the value of the
|
21
|
+
# object represented, attempting to convert the present Object
|
22
|
+
# to any other type raises an error.
|
23
|
+
|
24
|
+
# present can not be converted to an Integer.
|
25
|
+
def to_i
|
26
|
+
raise AmbiguousValueError
|
27
|
+
end
|
28
|
+
|
29
|
+
# present can not be converted to an Array.
|
30
|
+
def to_a
|
31
|
+
raise AmbiguousValueError
|
32
|
+
end
|
33
|
+
|
34
|
+
# present can not be converted to a String.
|
35
|
+
def to_s
|
36
|
+
raise AmbiguousValueError
|
37
|
+
end
|
38
|
+
|
39
|
+
# Return true if the object type is defined.
|
40
|
+
def type_known?
|
41
|
+
@type.nil? ? false : true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Error class communicating that because PresentClass is ambiguous, it should
|
46
|
+
# not be converted to other data types to avoid confusion between `present`
|
47
|
+
# and the actual value represented.
|
48
|
+
class AmbiguousValueError < StandardError
|
49
|
+
def initialize(msg = 'Value exists, but is unspecified, unknown, or secret.')
|
50
|
+
super
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Add some methods concerning present and PresentClass
|
55
|
+
# to all objects by extending Object.
|
56
|
+
class Object
|
57
|
+
# Allow .present? method on all objects
|
58
|
+
def present?
|
59
|
+
!nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
# Return a new PresentClass object with @type defined
|
63
|
+
# as the type of the object.
|
64
|
+
def present_with_type
|
65
|
+
PresentClass.new(self.class)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Aliases :present_with_class to :present_with_type
|
69
|
+
alias present_with_class present_with_type
|
70
|
+
end
|
71
|
+
|
72
|
+
# Extend Array with some helper methods.
|
73
|
+
class Array
|
74
|
+
# Returns true if all elements are present, false if one is nil
|
75
|
+
def all_present?
|
76
|
+
each do |e|
|
77
|
+
return false if e.nil?
|
78
|
+
end
|
79
|
+
true
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns an array with a boolean representing each element's presence
|
83
|
+
def each_present?
|
84
|
+
result = []
|
85
|
+
each_with_index do |e, i|
|
86
|
+
result[i] = if e.nil?
|
87
|
+
false
|
88
|
+
else
|
89
|
+
true
|
90
|
+
end
|
91
|
+
end
|
92
|
+
result
|
93
|
+
end
|
94
|
+
|
95
|
+
# Replaces non-nil elements with present
|
96
|
+
def mask_present
|
97
|
+
result = []
|
98
|
+
each_with_index do |e, i|
|
99
|
+
result[i] = (present if e.present?)
|
100
|
+
end
|
101
|
+
result
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Extend Hash with some helper methods.
|
106
|
+
class Hash
|
107
|
+
# Returns true if all values are present, otherwise false
|
108
|
+
def all_present?
|
109
|
+
each do |_key, value|
|
110
|
+
return false if value.nil?
|
111
|
+
end
|
112
|
+
true
|
113
|
+
end
|
114
|
+
|
115
|
+
# Returns a hash with the values of the original hash replaced with
|
116
|
+
# true if the value is present and false if nil.
|
117
|
+
def each_present?
|
118
|
+
result = {}
|
119
|
+
each do |key, value|
|
120
|
+
result[key] = value.present?
|
121
|
+
end
|
122
|
+
result
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns a hash with non-nil values of the original hash
|
126
|
+
# replaced with present.
|
127
|
+
def mask_present
|
128
|
+
result = {}
|
129
|
+
each do |key, value|
|
130
|
+
result[key] = value.present? ? present : nil
|
131
|
+
end
|
132
|
+
result
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Initializes a new constant with a frozen instance of PresentClass.
|
137
|
+
PRESENT = PresentClass.new.freeze
|
138
|
+
|
139
|
+
# +present+ returns PRESENT constant.
|
140
|
+
def present
|
141
|
+
PRESENT
|
142
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tektite_ruby_utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'tektite_ruby_utils'
|
8
|
+
spec.version = TektiteRubyUtils::VERSION
|
9
|
+
spec.authors = ['Tektite Software', 'Xavier Bick']
|
10
|
+
spec.email = ['fxb9500@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Extensions and utilities for Ruby'
|
13
|
+
spec.description = 'Adds additional functionality to Ruby,
|
14
|
+
such as PresentClass (opposite of NilClass).'
|
15
|
+
spec.homepage = 'https://github.com/tektite-software/ruby_utils'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`
|
19
|
+
.split("\x0")
|
20
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tektite_ruby_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tektite Software
|
8
|
+
- Xavier Bick
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-07-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.12'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.12'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '5.0'
|
56
|
+
description: |-
|
57
|
+
Adds additional functionality to Ruby,
|
58
|
+
such as PresentClass (opposite of NilClass).
|
59
|
+
email:
|
60
|
+
- fxb9500@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/tektite_ruby_utils.rb
|
74
|
+
- lib/tektite_ruby_utils/present.rb
|
75
|
+
- lib/tektite_ruby_utils/version.rb
|
76
|
+
- tektite_ruby_utils.gemspec
|
77
|
+
homepage: https://github.com/tektite-software/ruby_utils
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.5.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Extensions and utilities for Ruby
|
101
|
+
test_files: []
|
102
|
+
has_rdoc:
|