-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# core.digs | ||
|
||
Plural form of core.dig, returns an array with multiple | ||
digs at once | ||
|
||
``` | ||
core.digs: Object -> [Any] | ||
on_missing: null|fail = fail | ||
defn: [[String|Integer]] = [] | ||
``` | ||
|
||
This lens can be used to extract multiple values along some `defn` | ||
paths in the input object. | ||
|
||
## Example | ||
|
||
Applying the following lens: | ||
|
||
```yaml | ||
--- | ||
core.digs: | ||
defn: | ||
- ['foo'] | ||
- ['hobbies', 1, 'name'] | ||
``` | ||
to the following input: | ||
```yaml | ||
--- | ||
foo: Hello | ||
hobbies: | ||
- { name: 'Programming' } | ||
- { name: 'Databases' } | ||
``` | ||
will return: | ||
```yaml | ||
--- | ||
- Hello | ||
- Databases | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Monolens | ||
module Core | ||
class Digs | ||
include Lens | ||
|
||
signature(Type::Diggable, Type::Any, { | ||
defn: [ | ||
Type::Array.of(Type::Array.of(Type::Any.of(Type::Integer, Type::String))), | ||
true | ||
], | ||
on_missing: [Type::Strategy.missing(%w{fail null}), false] | ||
}) | ||
|
||
def call(arg, world = {}) | ||
to_dig = option(:defn, []) | ||
to_dig.map {|digging| | ||
digging.inject(arg) do |memo, part| | ||
dig_on(part, memo, world) | ||
end | ||
} | ||
end | ||
|
||
private | ||
|
||
def path | ||
option(:defn, []).join('.') | ||
end | ||
|
||
def dig_on(attr, arg, world) | ||
if arg.is_a?(::Array) | ||
index = attr.to_i | ||
on_missing(world) if index >= arg.size | ||
arg[index] | ||
elsif arg.is_a?(::Hash) | ||
actual, value = fetch_on(attr, arg) | ||
on_missing(world) unless actual | ||
value | ||
elsif arg | ||
if attr.is_a?(::Integer) | ||
is_array!(arg, world) | ||
else | ||
is_hash!(arg, world) | ||
end | ||
else | ||
on_missing(world) | ||
end | ||
end | ||
|
||
def on_missing(world) | ||
strategy = option(:on_missing, :fail) | ||
case strategy.to_sym | ||
when :fail | ||
fail!("Unable to find #{path}", world) | ||
when :null | ||
nil | ||
else | ||
raise Monolens::Error, "Unexpected missing strategy `#{strategy}`" | ||
end | ||
end | ||
private :on_missing | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'spec_helper' | ||
|
||
describe Monolens, "core.digs" do | ||
let(:lens) do | ||
Monolens.lens('core.digs' => { | ||
defn: [ | ||
['foo'], | ||
['hobbies', 1, 'name'], | ||
] | ||
}) | ||
end | ||
|
||
it 'works' do | ||
input = { | ||
hobbies: [ | ||
{ name: 'programming' }, | ||
{ name: 'music' } | ||
], | ||
foo: 'Hello' | ||
} | ||
expected = ['Hello', 'music'] | ||
expect(lens.call(input)).to eql(expected) | ||
end | ||
|
||
describe 'error handling' do | ||
let(:lens) do | ||
Monolens.lens({ | ||
'array.map' => { | ||
lenses: { | ||
'core.digs' => { | ||
on_missing: on_missing, | ||
defn: [ | ||
['foo'], | ||
['hobbies', 1, 'name'], | ||
] | ||
}.compact | ||
} | ||
} | ||
}) | ||
end | ||
|
||
subject do | ||
begin | ||
lens.call(input) | ||
rescue Monolens::LensError => ex | ||
ex | ||
end | ||
end | ||
|
||
context 'default behavior' do | ||
let(:on_missing) do | ||
nil | ||
end | ||
|
||
let(:input) do | ||
[{ | ||
hobbies: [ | ||
{ name: 'programming' } | ||
] | ||
}] | ||
end | ||
|
||
it 'fails as expected' do | ||
expect(subject).to be_a(Monolens::LensError) | ||
expect(subject.location).to eql([0]) | ||
end | ||
end | ||
|
||
context 'on_missing: null' do | ||
let(:on_missing) do | ||
:null | ||
end | ||
|
||
let(:input) do | ||
[{ | ||
hobbies: [ | ||
{ name: 'programming' } | ||
] | ||
}] | ||
end | ||
|
||
it 'works' do | ||
expect(subject).to eql([[nil, nil]]) | ||
end | ||
end | ||
end | ||
end |