sorta-lens 0.1.0 → 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: 60447887a149122646f86543ebb0a25f2bb6ed4af3ddd4d6592421df60c6fd47
4
- data.tar.gz: 15ad22d511ccbbb43c18cbf7e73a463b4c7f07669280399fa5eae52bf56be60b
3
+ metadata.gz: d770823bca9203f593d33ccd8fb900135b2772e3a7054f16be752f6a9ce51b8d
4
+ data.tar.gz: 10188f1777bd20e987cfc2edbf8cba95d3004e0b88eca05f2617511d7733bc86
5
5
  SHA512:
6
- metadata.gz: ea4c1a0c6046b5abaf4beb8e09e8cafb182ee33ccd3ec92710bf418cd07fe3c2dd09a5d90236c852d6487a4a5affa46ffe4d424267a29c141f03e3c12fb6aec0
7
- data.tar.gz: 938bbe6f04fc6ee83e6badc178719dd47a26576d75bd4ae6c03f75fb4d9f245d5169f705911b0c9ff5a13d5787da23c753d2333653e32b747d06522a1a0e0fd0
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.0)
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.21.0)
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.9.1, < 2.0)
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.11.0)
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 [![Gem Version](https://badge.fury.io/rb/sorta-lens.svg)](https://badge.fury.io/rb/sorta-lens) [![Ruby](https://github.com/sorta-rb/sorta-lens/actions/workflows/main.yml/badge.svg?branch=main)](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 = TypedLens.on id: Integer, status: String
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
+ ```
@@ -14,10 +14,13 @@ module Sorta
14
14
  end
15
15
 
16
16
  def call(object)
17
- @kwargs.each_with_object({}) do |(sym, _ty), acc|
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
- object.send(sym)
34
- rescue NoMethodError
35
- object[sym]
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
- @args.each_with_object({}) do |sym, acc|
18
- acc[sym] = object.send(sym)
19
- rescue NoMethodError
20
- acc[sym] = object[sym]
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sorta
4
4
  class Lens
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
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.0
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-27 00:00:00.000000000 Z
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: