sorta-lens 0.1.0
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +23 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +20 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/lens/lens.rb +50 -0
- data/lib/lens/typed_lens.rb +46 -0
- data/lib/lens/untyped.rb +34 -0
- data/lib/sorta-lens.rb +13 -0
- data/lib/version.rb +7 -0
- data/sorta-lens.gemspec +27 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 60447887a149122646f86543ebb0a25f2bb6ed4af3ddd4d6592421df60c6fd47
|
4
|
+
data.tar.gz: 15ad22d511ccbbb43c18cbf7e73a463b4c7f07669280399fa5eae52bf56be60b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea4c1a0c6046b5abaf4beb8e09e8cafb182ee33ccd3ec92710bf418cd07fe3c2dd09a5d90236c852d6487a4a5affa46ffe4d424267a29c141f03e3c12fb6aec0
|
7
|
+
data.tar.gz: 938bbe6f04fc6ee83e6badc178719dd47a26576d75bd4ae6c03f75fb4d9f245d5169f705911b0c9ff5a13d5787da23c753d2333653e32b747d06522a1a0e0fd0
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Lint/DuplicateBranch:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Naming/FileName:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/Documentation:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/StringLiterals:
|
15
|
+
Enabled: true
|
16
|
+
EnforcedStyle: double_quotes
|
17
|
+
|
18
|
+
Style/StringLiteralsInInterpolation:
|
19
|
+
Enabled: true
|
20
|
+
EnforcedStyle: double_quotes
|
21
|
+
|
22
|
+
Layout/LineLength:
|
23
|
+
Max: 120
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in sorta-lens.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
group :development, :test do
|
9
|
+
gem "rake", "~> 13.0"
|
10
|
+
|
11
|
+
gem "rspec", "~> 3.0"
|
12
|
+
|
13
|
+
gem "rubocop", "~> 1.7"
|
14
|
+
|
15
|
+
gem "rubocop-rake"
|
16
|
+
gem "rubocop-rspec"
|
17
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sorta-lens (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
parallel (1.21.0)
|
12
|
+
parser (3.0.2.0)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.0.0)
|
15
|
+
rake (13.0.6)
|
16
|
+
regexp_parser (2.1.1)
|
17
|
+
rexml (3.2.5)
|
18
|
+
rspec (3.10.0)
|
19
|
+
rspec-core (~> 3.10.0)
|
20
|
+
rspec-expectations (~> 3.10.0)
|
21
|
+
rspec-mocks (~> 3.10.0)
|
22
|
+
rspec-core (3.10.1)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-expectations (3.10.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-mocks (3.10.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-support (3.10.2)
|
31
|
+
rubocop (1.21.0)
|
32
|
+
parallel (~> 1.10)
|
33
|
+
parser (>= 3.0.0.0)
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
regexp_parser (>= 1.8, < 3.0)
|
36
|
+
rexml
|
37
|
+
rubocop-ast (>= 1.9.1, < 2.0)
|
38
|
+
ruby-progressbar (~> 1.7)
|
39
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
40
|
+
rubocop-ast (1.11.0)
|
41
|
+
parser (>= 3.0.1.1)
|
42
|
+
rubocop-rake (0.6.0)
|
43
|
+
rubocop (~> 1.0)
|
44
|
+
rubocop-rspec (2.5.0)
|
45
|
+
rubocop (~> 1.19)
|
46
|
+
ruby-progressbar (1.11.0)
|
47
|
+
unicode-display_width (2.1.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
x86_64-linux
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
rake (~> 13.0)
|
54
|
+
rspec (~> 3.0)
|
55
|
+
rubocop (~> 1.7)
|
56
|
+
rubocop-rake
|
57
|
+
rubocop-rspec
|
58
|
+
sorta-lens!
|
59
|
+
|
60
|
+
BUNDLED WITH
|
61
|
+
2.2.27
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Ivan Chinenov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Sorta-lens
|
2
|
+
Simple lensing for your data extraction needs
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
Simply instantiate a Lens and start applying it to your objects:
|
6
|
+
```ruby
|
7
|
+
complex1 = { id: 451, .... status: :active }
|
8
|
+
complex2 = User.first # has property `id` and method `status`
|
9
|
+
lens = Lens.on :id, :status
|
10
|
+
lens.(complex1) # => { id: 451, status: :active }
|
11
|
+
lens.(complex2) # => { id: 1, status: :online }
|
12
|
+
```
|
13
|
+
There is also a typed version:
|
14
|
+
```ruby
|
15
|
+
complex1 = { id: 451, .... status: :active }
|
16
|
+
complex2 = { id: 9001, .... status: "online" }
|
17
|
+
lens = TypedLens.on id: Integer, status: String
|
18
|
+
lens.(complex1) # => TypedLens::TypeError (Unexpected type. Expected String got Symbol)
|
19
|
+
lens.(complex2) # => { id: 9001, status: "online" }
|
20
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "sorta/lens"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/lens/lens.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
# Lens builder
|
5
|
+
class Lens
|
6
|
+
def self.todo!
|
7
|
+
raise StandardError, "Unimplemented!"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.typed
|
11
|
+
new.typed
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.indifferent
|
15
|
+
new.indifferent
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.on(...)
|
19
|
+
new.on(...)
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@typed = false
|
24
|
+
@indifferent = false
|
25
|
+
end
|
26
|
+
|
27
|
+
def typed
|
28
|
+
@typed = true
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def indifferent
|
33
|
+
@indifferent = true
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def on(...)
|
38
|
+
case [@indifferent, @typed]
|
39
|
+
when [true, true]
|
40
|
+
todo!
|
41
|
+
when [true, false]
|
42
|
+
todo!
|
43
|
+
when [false, false]
|
44
|
+
Untyped.new(...)
|
45
|
+
when [false, true]
|
46
|
+
Typed.new(...)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
class Lens
|
5
|
+
# Extracts data from a given object
|
6
|
+
class Typed
|
7
|
+
def self.on(...)
|
8
|
+
new(...)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(**kwargs)
|
12
|
+
@kwargs = kwargs
|
13
|
+
validate_arguments
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(object)
|
17
|
+
@kwargs.each_with_object({}) do |(sym, _ty), acc|
|
18
|
+
val = extract(sym, object)
|
19
|
+
acc[sym] = typecheck(sym, val)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate_arguments
|
24
|
+
@kwargs.each do |(sym, ty)|
|
25
|
+
unless sym.is_a?(Symbol) && ty.is_a?(Class)
|
26
|
+
raise ArgumentError,
|
27
|
+
"Unexpected argument (#{sym.class} => #{ty.class}), must be (Symbol => Class)"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def extract(sym, object)
|
33
|
+
object.send(sym)
|
34
|
+
rescue NoMethodError
|
35
|
+
object[sym]
|
36
|
+
end
|
37
|
+
|
38
|
+
def typecheck(sym, val)
|
39
|
+
ty = @kwargs[sym]
|
40
|
+
return val if val.is_a?(ty)
|
41
|
+
|
42
|
+
raise TypeError.new val.class, ty
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/lens/untyped.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sorta
|
4
|
+
class Lens
|
5
|
+
# Extracts data from a given object
|
6
|
+
class Untyped
|
7
|
+
def self.on(...)
|
8
|
+
new(...)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(*args)
|
12
|
+
@args = args
|
13
|
+
validate_arguments
|
14
|
+
end
|
15
|
+
|
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]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_arguments
|
25
|
+
@args.each do |sym|
|
26
|
+
unless sym.is_a?(Symbol)
|
27
|
+
raise ArgumentError,
|
28
|
+
"Unexpected argument #{sym.class}, must be Symbol"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/sorta-lens.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lens/lens"
|
4
|
+
require_relative "lens/typed_lens"
|
5
|
+
require_relative "lens/untyped"
|
6
|
+
require_relative "version"
|
7
|
+
module Sorta
|
8
|
+
class TypeError < RuntimeError
|
9
|
+
def initialize(expected_ty, actual_ty)
|
10
|
+
super "Unexpected type. Expected #{expected_ty} got #{actual_ty}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/version.rb
ADDED
data/sorta-lens.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "sorta-lens"
|
7
|
+
spec.version = Sorta::Lens::VERSION
|
8
|
+
spec.authors = ["Ivan Chinenov"]
|
9
|
+
spec.email = ["ichinenov@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Simple lensing for your data extraction needs"
|
12
|
+
spec.homepage = "https://github.com/sorta-rb/sorta-lens"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/sorta-rb/sorta-lens"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorta-lens
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Chinenov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-09-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- ichinenov@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/lens/lens.rb
|
30
|
+
- lib/lens/typed_lens.rb
|
31
|
+
- lib/lens/untyped.rb
|
32
|
+
- lib/sorta-lens.rb
|
33
|
+
- lib/version.rb
|
34
|
+
- sorta-lens.gemspec
|
35
|
+
homepage: https://github.com/sorta-rb/sorta-lens
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/sorta-rb/sorta-lens
|
40
|
+
source_code_uri: https://github.com/sorta-rb/sorta-lens
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.7.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.1.4
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Simple lensing for your data extraction needs
|
60
|
+
test_files: []
|