omg-attrs 0.1.1 → 0.1.3

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: b664a869fd5bf0f6fe29303ca872c4449e6734f6c0e4ad69a72f4933d9481585
4
+ data.tar.gz: 16eba8246a6b120ddcefb111678ececa6d5169ecfa93650cf92c97f20e94b6f2
5
5
  SHA512:
6
- metadata.gz: 521c7d6cf9f04759159669879341e6de02aa9996e05c52492dfeed32d14efb02f896eeb82ab80f6ac67a8fe6672656bad57f265f1d0058ac003b7a4a0cc73be5
7
- data.tar.gz: 838d9945288f976fd547b7f817672dc391c95875c60d0743bb6022ebaf78c6fe49aa71de33eba721dd2e532d80199cae750a0177e3cc4c26244e7758854e3839
6
+ metadata.gz: e2bd0c5207615193671bf330177ce372ca37e18bb1bbb8d0c8c10b50d8a89488525c6c5e704502ba9506a9e6382655889597bd28185e3fb06869a6fb0b50906a
7
+ data.tar.gz: 9fd8b8a982cb099053a904c71f56f61634875118ba1f1f4dc1fbaa8b9e10f7ade63a1c068f085cade9c0d59ce82b340a9a00a1d37bf174b81e11100767bf0f99
data/lib/ext/array.rb ADDED
@@ -0,0 +1,23 @@
1
+ require_relative 'object'
2
+
3
+ module Ext
4
+ module Array
5
+ ##
6
+ # @param [Hash]
7
+ # @return [Object] First object that matches passed in key/value pairs
8
+ # @example
9
+ # [{ a: 1 }, { b: 2 }].find_by(a: 1) => { a: 1 }
10
+ def find_by(**attrs)
11
+ find { |item| item.match?(**attrs) }
12
+ end
13
+
14
+ ##
15
+ # @return [Array<Object>] all items that match all key/value pairs
16
+ def where(**attrs)
17
+ filter { |item| item.match?(**attrs) }
18
+ end
19
+ end
20
+ end
21
+
22
+ Array.include Ext::Array
23
+ Array.include Ext::Object
data/lib/ext/hash.rb ADDED
@@ -0,0 +1,30 @@
1
+ require_relative 'object'
2
+
3
+ module Ext
4
+ module Hash
5
+ def method_missing(method_name, *args, &block)
6
+ if key?(method_name.to_s)
7
+ self.class.define_method(method_name) do
8
+ self[method_name.to_s]
9
+ end
10
+
11
+ send(method_name)
12
+ elsif key?(method_name.to_sym)
13
+ self.class.define_method(method_name) do
14
+ self[method_name.to_sym]
15
+ end
16
+
17
+ send(method_name)
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ def respond_to_missing?(method_name, *args, &block)
24
+ key?(method_name.to_s) || key?(method_name.to_sym) || super
25
+ end
26
+ end
27
+ end
28
+
29
+ Hash.include Ext::Hash
30
+ Hash.include Ext::Object
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,16 +1,21 @@
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.3
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-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Makes it easy to slice and dice objects and collections of objects
13
+ description: |
14
+ - Adds `Object#attrs` method for querying nested attributes
15
+ - Adds `Array#where` to return a subset of elements matching a condition
16
+ - Adds `Array#find_by` to return the first element matching a condition
17
+ - Adds `Object#match?` to check if an object matches a condition
18
+ - Adds `Hash#method_missing` to allow {a: 1}.a instead of {a: 1}[:a]
14
19
  email:
15
20
  - mattgreenfield1@gmail.com
16
21
  executables: []
@@ -18,6 +23,9 @@ extensions: []
18
23
  extra_rdoc_files: []
19
24
  files:
20
25
  - lib/attrs.rb
26
+ - lib/ext/array.rb
27
+ - lib/ext/hash.rb
28
+ - lib/ext/object.rb
21
29
  homepage: https://github.com/omgreenfield/attrs
22
30
  licenses:
23
31
  - MIT
@@ -42,5 +50,5 @@ requirements: []
42
50
  rubygems_version: 3.5.17
43
51
  signing_key:
44
52
  specification_version: 4
45
- summary: Allows all objects to call `.attrs` which acts as a recursive `.slice` method
53
+ summary: Gives objects methods to query nested attributes
46
54
  test_files: []