omg-attrs 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46eb3e016278c743e4eef8261a80305e04df785d995a3296ddabc657abd7d1c0
4
- data.tar.gz: 4ea3a9f88a0b4d606a81bdfa8b1a8834f95af409be473f570388cd202dc2805f
3
+ metadata.gz: 02ce08d9a45a81a1e6e21af0744aadcac77ce59013fc4337860002a7001551e4
4
+ data.tar.gz: 3a8048e24bec33c37d70c1ac73c0c8b6c62a2ced7fd8dde8042fef4904ab6f4f
5
5
  SHA512:
6
- metadata.gz: 521c7d6cf9f04759159669879341e6de02aa9996e05c52492dfeed32d14efb02f896eeb82ab80f6ac67a8fe6672656bad57f265f1d0058ac003b7a4a0cc73be5
7
- data.tar.gz: 838d9945288f976fd547b7f817672dc391c95875c60d0743bb6022ebaf78c6fe49aa71de33eba721dd2e532d80199cae750a0177e3cc4c26244e7758854e3839
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.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-08-13 00:00:00.000000000 Z
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