From ef67435f5e2b0937c1da262d59c6a10394203743 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sat, 15 Jun 2019 14:10:03 -0500 Subject: [PATCH] RecordProjection.for --- lib/dhall/ast.rb | 8 ++++++++ lib/dhall/binary.rb | 6 +----- lib/dhall/parser.rb | 3 +-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/dhall/ast.rb b/lib/dhall/ast.rb index 0a265e7..bd33385 100644 --- a/lib/dhall/ast.rb +++ b/lib/dhall/ast.rb @@ -798,6 +798,14 @@ module Dhall selectors Util::ArrayOf.new(::String, min: 1) end) + def self.for(record, selectors) + if selectors.empty? + EmptyRecordProjection.new(record: record) + else + new(record: record, selectors: selectors) + end + end + def as_json [10, record.as_json, *selectors] end diff --git a/lib/dhall/binary.rb b/lib/dhall/binary.rb index ba45005..f8b16eb 100644 --- a/lib/dhall/binary.rb +++ b/lib/dhall/binary.rb @@ -141,11 +141,7 @@ module Dhall class RecordProjection def self.decode(record, *selectors) - if selectors.empty? - EmptyRecordProjection.new(record: Dhall.decode(record)) - else - new(record: Dhall.decode(record), selectors: selectors) - end + self.for(Dhall.decode(record), selectors) end end diff --git a/lib/dhall/parser.rb b/lib/dhall/parser.rb index 2e2b143..53eb5f0 100644 --- a/lib/dhall/parser.rb +++ b/lib/dhall/parser.rb @@ -145,8 +145,7 @@ module Dhall selectors = captures(:selector).map(&:value) selectors.reduce(record) do |rec, sels| if sels.is_a?(Array) - return EmptyRecordProjection.new(record: rec) if sels.empty? - RecordProjection.new(record: rec, selectors: sels) + RecordProjection.for(rec, sels) else RecordSelection.new(record: rec, selector: sels) end -- 2.34.5