get_better 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/enumerable.rb +96 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '00929cdacdfc73c86875e74ceb8be78ac609d130'
|
4
|
+
data.tar.gz: 95a0b58824bd6f692d20fb74f338e85e207fafa7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 075f4b19a8cdaf6f05bea6510e82432e18ea9c7a7c7bd4e5316aa0450e96b80706453937ed5735b90d56e35b03b1a302fcc697028c9551dfc1e5b6e36d087c7b
|
7
|
+
data.tar.gz: d1d4ef1557a7fe941c6f2e8f1574d2a89e31d3511a8eaf488edab3ffb2e4878460a531d2480587188ef75731d71ba176fefefef8c0c043a2398a3c9379ddda44
|
data/lib/enumerable.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
def get(params = {})
|
4
|
+
find do |record|
|
5
|
+
params.all? do |column_and_operator, value|
|
6
|
+
get_by_column_and_operator(record.is_a?(Hash) ? record.to_struct : record, column_and_operator, value)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_all(params = {})
|
12
|
+
find_all do |record|
|
13
|
+
params.all? do |column_and_operator, value|
|
14
|
+
get_by_column_and_operator(record.is_a?(Hash) ? record.to_struct : record, column_and_operator, value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_or(params = {})
|
20
|
+
find do |record|
|
21
|
+
params.any? do |column_and_operator, value|
|
22
|
+
get_by_column_and_operator(record.is_a?(Hash) ? record.to_struct : record, column_and_operator, value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_all_or(params = {})
|
28
|
+
find_all do |record|
|
29
|
+
params.any? do |column_and_operator, value|
|
30
|
+
get_by_column_and_operator(record.is_a?(Hash) ? record.to_struct : record, column_and_operator, value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_by_column_and_operator(record, column_and_operator, value)
|
36
|
+
column, operator = split_column_and_operator(column_and_operator.to_s)
|
37
|
+
column_value = column.nil? ? record : record.send(column)
|
38
|
+
case operator
|
39
|
+
when "eq" then column_value == value
|
40
|
+
when "not_eq" then column_value != value
|
41
|
+
when "lt" then column_value < value
|
42
|
+
when "gt" then column_value > value
|
43
|
+
when "lteq" then column_value <= value
|
44
|
+
when "gteq" then column_value >= value
|
45
|
+
when "ieq" then column_value.to_s.downcase == value.to_s.downcase
|
46
|
+
when "not_ieq" then column_value.to_s.downcase != value.to_s.downcase
|
47
|
+
when "in", "not_in"
|
48
|
+
if value.is_a?(Array)
|
49
|
+
is_in = column_value.in?(value)
|
50
|
+
operator == "in" ? is_in : !is_in
|
51
|
+
else
|
52
|
+
raise(ArgumentError, "get with an '##{operator}' operator expects an array")
|
53
|
+
end
|
54
|
+
when "matches", "does_not_match"
|
55
|
+
does_match =
|
56
|
+
if value.is_a?(String)
|
57
|
+
value.include?(column_value)
|
58
|
+
elsif value.is_a?(Regexp)
|
59
|
+
column_value =~ value
|
60
|
+
else
|
61
|
+
raise(ArgumentError, "get with an '#{operator}' operator expects a string or regex")
|
62
|
+
end
|
63
|
+
operator == "matches" ? does_match : !does_match
|
64
|
+
else
|
65
|
+
raise(ArgumentError, "Unknown operator")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def split_column_and_operator(column_and_operator)
|
70
|
+
operators = {
|
71
|
+
does_not_match: /_does_not_match\z/,
|
72
|
+
matches: /_matches\z/,
|
73
|
+
not_in: /_not_in\z/,
|
74
|
+
not_ieq: /_not_ieq\z/,
|
75
|
+
not_eq: /_not_eq\z/,
|
76
|
+
lteq: /_lteq\z/,
|
77
|
+
gteq: /_gteq\z/,
|
78
|
+
in: /_in\z/,
|
79
|
+
ieq: /_ieq\z/,
|
80
|
+
eq: /_eq\z/,
|
81
|
+
lt: /_lt\z/,
|
82
|
+
gt: /_gt\z/,
|
83
|
+
}.with_indifferent_access
|
84
|
+
# If column_and_operator is one of the operators (i.e not_eq). Prevents not from getting assigned as column.
|
85
|
+
if column_and_operator.in?(operators.keys)
|
86
|
+
[nil, column_and_operator]
|
87
|
+
else
|
88
|
+
operator, operator_regex = operators.find { |operator, regex| column_and_operator =~ regex }
|
89
|
+
if !operator || !operator_regex
|
90
|
+
[column_and_operator, "eq"]
|
91
|
+
else
|
92
|
+
[column_and_operator.remove(operator_regex), operator]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: get_better
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Woodruff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/enumerable.rb
|
20
|
+
homepage:
|
21
|
+
licenses: []
|
22
|
+
metadata:
|
23
|
+
source_code_uri: https://github.com/brandonwoodruff92/getter
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.5.2.3
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Useful methods for getting objects from enumerable objects using various
|
44
|
+
operators
|
45
|
+
test_files: []
|