From e60cd10d06d1319fc7330bc56e367a249b5e0aaf Mon Sep 17 00:00:00 2001 From: Bernard Lambeau Date: Thu, 9 Jun 2022 16:04:27 +0200 Subject: [PATCH] Add core.literal lens. --- CHANGELOG.md | 4 ++++ README.md | 1 + lib/monolens/core.rb | 6 ++++++ lib/monolens/core/literal.rb | 11 +++++++++++ spec/monolens/core/test_literal.rb | 13 +++++++++++++ 5 files changed, 35 insertions(+) create mode 100644 lib/monolens/core/literal.rb create mode 100644 spec/monolens/core/test_literal.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index ee276b1..7aeefec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.3 + +* Add core.literal lens. + ## 0.5.2 - 2022-05-20 * Add check.notEmpty lens that throw an error if the input is diff --git a/README.md b/README.md index 9dd0b78..0038730 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ result = lens.call(input) core.dig - Extract from the input value (object or array) using a path. core.chain - Applies a chain of lenses to an input value core.mapping - Converts the input value via a key:value mapping +core.literal - Returns a constant value takens as lens definition str.strip - Remove leading and trailing spaces of an input string str.split - Splits the input string as an array diff --git a/lib/monolens/core.rb b/lib/monolens/core.rb index f124b83..be72558 100644 --- a/lib/monolens/core.rb +++ b/lib/monolens/core.rb @@ -10,6 +10,11 @@ def dig(options) end module_function :dig + def literal(options) + Literal.new(options) + end + module_function :literal + def mapping(options) Mapping.new(options) end @@ -21,3 +26,4 @@ def mapping(options) require_relative 'core/chain' require_relative 'core/dig' require_relative 'core/mapping' +require_relative 'core/literal' diff --git a/lib/monolens/core/literal.rb b/lib/monolens/core/literal.rb new file mode 100644 index 0000000..778b589 --- /dev/null +++ b/lib/monolens/core/literal.rb @@ -0,0 +1,11 @@ +module Monolens + module Core + class Literal + include Lens + + def call(arg, world = {}) + option(:defn) + end + end + end +end diff --git a/spec/monolens/core/test_literal.rb b/spec/monolens/core/test_literal.rb new file mode 100644 index 0000000..95db806 --- /dev/null +++ b/spec/monolens/core/test_literal.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Monolens, "core.literal" do + let(:lens) do + Monolens.lens('core.literal' => { defn: 'hello' }) + end + + it 'works' do + input = {} + expected = 'hello' + expect(lens.call(input)).to eql(expected) + end +end