sorta-lens 0.1.0 → 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/Gemfile.lock +4 -4
- data/README.md +14 -4
- data/lib/lens/typed_lens.rb +9 -4
- data/lib/lens/untyped.rb +15 -4
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d770823bca9203f593d33ccd8fb900135b2772e3a7054f16be752f6a9ce51b8d
|
4
|
+
data.tar.gz: 10188f1777bd20e987cfc2edbf8cba95d3004e0b88eca05f2617511d7733bc86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 229febb8c6856c149995e317cc915a6869357f510474159c509b1c935a856ea2477543292a2d9f9d6eafb5e8ef094f1d4c1859bcc05b7c66e718d73b6652cc2e
|
7
|
+
data.tar.gz: e87ca53c59cf63e658f0ad59f1623cbbef48c5337b52bdaca9480386a524a1a55c0efd0bd24e295071bf13b9f2d9519e97453153ad924b07793d37f6d8348db2
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sorta-lens (0.1.
|
4
|
+
sorta-lens (0.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -28,16 +28,16 @@ GEM
|
|
28
28
|
diff-lcs (>= 1.2.0, < 2.0)
|
29
29
|
rspec-support (~> 3.10.0)
|
30
30
|
rspec-support (3.10.2)
|
31
|
-
rubocop (1.
|
31
|
+
rubocop (1.22.0)
|
32
32
|
parallel (~> 1.10)
|
33
33
|
parser (>= 3.0.0.0)
|
34
34
|
rainbow (>= 2.2.2, < 4.0)
|
35
35
|
regexp_parser (>= 1.8, < 3.0)
|
36
36
|
rexml
|
37
|
-
rubocop-ast (>= 1.
|
37
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
38
38
|
ruby-progressbar (~> 1.7)
|
39
39
|
unicode-display_width (>= 1.4.0, < 3.0)
|
40
|
-
rubocop-ast (1.
|
40
|
+
rubocop-ast (1.12.0)
|
41
41
|
parser (>= 3.0.1.1)
|
42
42
|
rubocop-rake (0.6.0)
|
43
43
|
rubocop (~> 1.0)
|
data/README.md
CHANGED
@@ -1,12 +1,22 @@
|
|
1
|
-
# Sorta-lens
|
1
|
+
# Sorta-lens [](https://badge.fury.io/rb/sorta-lens) [](https://github.com/sorta-rb/sorta-lens/actions/workflows/main.yml)
|
2
2
|
Simple lensing for your data extraction needs
|
3
3
|
|
4
|
+
## Installation
|
5
|
+
Add it to your gemfile:
|
6
|
+
```Gemfile
|
7
|
+
gem 'sorta-lens', '~> 0.1.0'
|
8
|
+
```
|
9
|
+
or install globally for use in scripts or IRB
|
10
|
+
```sh
|
11
|
+
gem install sorta-lens
|
12
|
+
```
|
13
|
+
|
4
14
|
## Usage
|
5
15
|
Simply instantiate a Lens and start applying it to your objects:
|
6
16
|
```ruby
|
7
17
|
complex1 = { id: 451, .... status: :active }
|
8
18
|
complex2 = User.first # has property `id` and method `status`
|
9
|
-
lens = Lens.on :id, :status
|
19
|
+
lens = Sorta::Lens.on :id, :status
|
10
20
|
lens.(complex1) # => { id: 451, status: :active }
|
11
21
|
lens.(complex2) # => { id: 1, status: :online }
|
12
22
|
```
|
@@ -14,7 +24,7 @@ There is also a typed version:
|
|
14
24
|
```ruby
|
15
25
|
complex1 = { id: 451, .... status: :active }
|
16
26
|
complex2 = { id: 9001, .... status: "online" }
|
17
|
-
lens =
|
27
|
+
lens = Sorta::Lens.typed.on id: Integer, status: String
|
18
28
|
lens.(complex1) # => TypedLens::TypeError (Unexpected type. Expected String got Symbol)
|
19
29
|
lens.(complex2) # => { id: 9001, status: "online" }
|
20
|
-
```
|
30
|
+
```
|
data/lib/lens/typed_lens.rb
CHANGED
@@ -14,10 +14,13 @@ module Sorta
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def call(object)
|
17
|
-
@
|
17
|
+
@getable = object.respond_to? :[]
|
18
|
+
result = @kwargs.each_with_object({}) do |(sym, _ty), acc|
|
18
19
|
val = extract(sym, object)
|
19
20
|
acc[sym] = typecheck(sym, val)
|
20
21
|
end
|
22
|
+
@getable = nil
|
23
|
+
result
|
21
24
|
end
|
22
25
|
|
23
26
|
def validate_arguments
|
@@ -30,9 +33,11 @@ module Sorta
|
|
30
33
|
end
|
31
34
|
|
32
35
|
def extract(sym, object)
|
33
|
-
|
34
|
-
|
35
|
-
object
|
36
|
+
if @getable
|
37
|
+
object[sym]
|
38
|
+
elsif object.respond_to? sym
|
39
|
+
object.send(sym)
|
40
|
+
end
|
36
41
|
end
|
37
42
|
|
38
43
|
def typecheck(sym, val)
|
data/lib/lens/untyped.rb
CHANGED
@@ -14,10 +14,21 @@ module Sorta
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def call(object)
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
@getable = object.respond_to? :[]
|
18
|
+
result = @args.each_with_object({}) do |sym, acc|
|
19
|
+
acc[sym] = extract(sym, object)
|
20
|
+
end
|
21
|
+
@getable = nil
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def extract(sym, object)
|
26
|
+
if @getable
|
27
|
+
object[sym]
|
28
|
+
elsif object.respond_to? sym
|
29
|
+
object.send(sym)
|
30
|
+
else
|
31
|
+
raise ArgumentError, "Object #{object} does not support extracting this symbol #{sym}"
|
21
32
|
end
|
22
33
|
end
|
23
34
|
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorta-lens
|
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
|
- Ivan Chinenov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|