From e2c399cf49f8eda66fc07110c27f9fd1f0e743b6 Mon Sep 17 00:00:00 2001 From: thadeu Date: Tue, 7 May 2024 03:32:41 +0000 Subject: [PATCH] remove Given class --- lib/zx.rb | 5 ++- lib/zx/given.rb | 77 --------------------------------------- spec/lib/zx/given_spec.rb | 53 --------------------------- 3 files changed, 4 insertions(+), 131 deletions(-) delete mode 100644 lib/zx/given.rb delete mode 100644 spec/lib/zx/given_spec.rb diff --git a/lib/zx.rb b/lib/zx.rb index a671d21..beb05b3 100644 --- a/lib/zx.rb +++ b/lib/zx.rb @@ -2,7 +2,6 @@ require 'zx/version' require 'zx/fmap' -require 'zx/given' require 'zx/reflect' require 'zx/result' @@ -23,4 +22,8 @@ def Try(default = nil, options = {}) rescue StandardError => e Failure[default || options.fetch(:or, nil)] end + + def Given(input) + Try { input } + end end diff --git a/lib/zx/given.rb b/lib/zx/given.rb deleted file mode 100644 index 8aae891..0000000 --- a/lib/zx/given.rb +++ /dev/null @@ -1,77 +0,0 @@ -# frozen_string_literal: true - -module Zx - class Given - class FailureError < StandardError; end - - def initialize(input = nil) - @original_input = input - @input = input - @success = true - @exception = false - @type = nil - end - - attr_accessor :input, :type - - def error - @input unless type == :ok - end - - def success? - !!@success - end - - def failure? - !success? - end - - def and_then(&block) - block.call(@input) - - self - rescue StandardError => e - @success = false - @input = e.message - @type = :error - - self - end - - def and_then!(&block) - self.class.new(block.call(@input)) - end - - def on_success(&block) - return self if failure? - - and_then(&block) - end - - def on_failure(&block) - return self if success? - - and_then(&block) - end - - def on(ontype, &block) - case ontype.to_sym - when :success then on_success(&block) - when :failure then on_failure(&block) - end - end - - def unwrap - @input - end - - module Methods - def Given(input) - Given.new(input) - end - end - end - - include Given::Methods - extend Given::Methods -end diff --git a/spec/lib/zx/given_spec.rb b/spec/lib/zx/given_spec.rb deleted file mode 100644 index 50bcd33..0000000 --- a/spec/lib/zx/given_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -Downcase = ->(str) { str.downcase } - -RSpec.describe Given do - describe '#and_then' do - it 'dont morphing input' do - attributes = { first_name: 'THADEU', last_name: 'JUNIOR' } - - result = Given(attributes) - .and_then { |attrs| attrs[:first_name] = Downcase[attrs[:first_name]] } - .and_then { |attrs| attrs[:last_name] = Downcase[attrs[:last_name]] } - .unwrap - - expect(result[:first_name]).to eq('thadeu') - expect(result[:last_name]).to eq('junior') - end - - it 'morphing input' do - attributes = { first_name: 'THADEU', last_name: 'JUNIOR' } - - result = Given(attributes) - .and_then { |attrs| attrs[:first_name] = Downcase[attrs[:first_name]] } - .and_then { |attrs| attrs[:last_name] = Downcase[attrs[:last_name]] } - .and_then! { |attrs| attrs.slice(:first_name) } - .unwrap - - expect(result[:first_name]).to eq('thadeu') - expect(result[:last_name]).not_to eq('junior') - end - - it 'using inside class' do - mailer = UserMailer.new - result = mailer.deliver(first_name: 'thadeu') - - expect(result).to be_success - expect(result.unwrap).to eq(first_name: 'thadeu') - end - - it 'using inside class raised' do - mailer = UserMailer.new - - result = mailer - .deliver(first_name: 'thadeu') - .and_then { raise StandardError, 'be error' } - - expect(result).to be_failure - expect(result.unwrap).to eq('be error') - end - end -end