omg-attrs 0.1.1 → 0.1.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/lib/ext/array.rb +20 -0
- data/lib/ext/hash.rb +27 -0
- data/lib/ext/object.rb +31 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02ce08d9a45a81a1e6e21af0744aadcac77ce59013fc4337860002a7001551e4
|
4
|
+
data.tar.gz: 3a8048e24bec33c37d70c1ac73c0c8b6c62a2ced7fd8dde8042fef4904ab6f4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d060b165eb56a7dff83434896cbacbdb6ca0d031cabef97cfbf15b722c3ea4147b6b004918a544cbb72cf9e221626328cf0f5a8c9c4222ca674dfe2ae1a4ec1
|
7
|
+
data.tar.gz: 822fb4378d650d8083c5bcd92b7c8057f80853a1b046270ceaa34d03c6b6ef097b24bc41fb75210008353fd4335a03bfa38d095906bc49d98188367d53ba6589
|
data/lib/ext/array.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Ext
|
2
|
+
module Array
|
3
|
+
##
|
4
|
+
# @param [Hash]
|
5
|
+
# @return [Object] First object that matches passed in key/value pairs
|
6
|
+
# @example
|
7
|
+
# [{ a: 1 }, { b: 2 }].find_by(a: 1) => { a: 1 }
|
8
|
+
def find_by(**attrs)
|
9
|
+
find { |item| item.match?(**attrs) }
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# @return [Array<Object>] all items that match all key/value pairs
|
14
|
+
def where(**attrs)
|
15
|
+
filter { |item| item.match?(**attrs) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Array.include Ext::Array
|
data/lib/ext/hash.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Ext
|
2
|
+
module Hash
|
3
|
+
def method_missing(method_name, *args, &block)
|
4
|
+
if key?(method_name.to_s)
|
5
|
+
self.class.define_method(method_name) do
|
6
|
+
self[method_name.to_s]
|
7
|
+
end
|
8
|
+
|
9
|
+
send(method_name)
|
10
|
+
elsif key?(method_name.to_sym)
|
11
|
+
self.class.define_method(method_name) do
|
12
|
+
self[method_name.to_sym]
|
13
|
+
end
|
14
|
+
|
15
|
+
send(method_name)
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to_missing?(method_name, *args, &block)
|
22
|
+
key?(method_name.to_s) || key?(method_name.to_sym) || super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Hash.include Ext::Hash
|
data/lib/ext/object.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Ext
|
2
|
+
module Object
|
3
|
+
##
|
4
|
+
# @return [Boolean] whether all key/value pairs match
|
5
|
+
def match?(**args)
|
6
|
+
args.all? do |key, value|
|
7
|
+
key_value_match?(key, value)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def key_value_match?(key, value)
|
12
|
+
if respond_to?(:[])
|
13
|
+
attribute = self[key]
|
14
|
+
return true if attribute == value
|
15
|
+
return true if attribute.respond_to?(value) && attribute.send(value)
|
16
|
+
end
|
17
|
+
|
18
|
+
if respond_to?(key)
|
19
|
+
attribute = send(key)
|
20
|
+
return true if attribute == value
|
21
|
+
return true if attribute.respond_to?(value) && attribute.send(value)
|
22
|
+
end
|
23
|
+
|
24
|
+
return false
|
25
|
+
rescue TypeError, NoMethodError, NameError, ArgumentError
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
::Object.include Ext::Object
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: omg-attrs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Greenfield
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Makes it easy to slice and dice objects and collections of objects
|
14
14
|
email:
|
@@ -18,6 +18,9 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/attrs.rb
|
21
|
+
- lib/ext/array.rb
|
22
|
+
- lib/ext/hash.rb
|
23
|
+
- lib/ext/object.rb
|
21
24
|
homepage: https://github.com/omgreenfield/attrs
|
22
25
|
licenses:
|
23
26
|
- MIT
|