From 785c14459feb5d643513c3f98f983f406becc441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Tue, 21 Nov 2023 11:02:59 -0300 Subject: [PATCH 1/2] feat: implement Array.wrap to standardize array handling --- lib/lenna/router/response.rb | 19 ++++++++++--------- lib/lennarb.rb | 4 ++++ lib/lennarb/array_extensions.rb | 17 +++++++++++++++++ lib/lennarb/version.rb | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 lib/lennarb/array_extensions.rb diff --git a/lib/lenna/router/response.rb b/lib/lenna/router/response.rb index cb321fd..4240fb5 100644 --- a/lib/lenna/router/response.rb +++ b/lib/lenna/router/response.rb @@ -315,17 +315,18 @@ def put_body(value) # # @return [void] def put_header(key, value) - key => ::String - value => ::String | ::Array + raise ::ArgumentError, 'key must be a string' unless key.is_a?(::String) + + unless value.is_a?(::String) || value.is_a?(::Array) + raise ::ArgumentError, 'value must be a string or an array' + end header_value = @headers[key] - case value - in ::String then @headers[key] = [*header_value, value].uniq.join(', ') - in ::Array then @headers[key] = [*header_value, *value].uniq.join(', ') - end - rescue ::NoMatchingPatternError - raise ::ArgumentError, 'header must be a string or an array' + new_values = ::Array.wrap(value) + existing_values = ::Array.wrap(header_value) + + @headers[key] = (existing_values + new_values).uniq.join(', ') end # @param key [String] the header name @@ -352,7 +353,7 @@ def finish! [@status, @headers, @body] end - + # This method will set the response default html content type. # # @return [void] diff --git a/lib/lennarb.rb b/lib/lennarb.rb index 9f646b0..409b4c3 100644 --- a/lib/lennarb.rb +++ b/lib/lennarb.rb @@ -1,4 +1,8 @@ # frozen_string_literal: true +# Extension for Array class +require 'lennarb/array_extensions' + +# Base class for Lennarb require 'lenna/base' require 'lennarb/version' diff --git a/lib/lennarb/array_extensions.rb b/lib/lennarb/array_extensions.rb new file mode 100644 index 0000000..289f7f0 --- /dev/null +++ b/lib/lennarb/array_extensions.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Lennarb + module ArrayExtensions + def wrap(object) + if object.nil? + [] + elsif object.respond_to?(:to_ary) + object.to_ary || [object] + else + [object] + end + end + end +end + +Array.extend(Lennarb::ArrayExtensions) diff --git a/lib/lennarb/version.rb b/lib/lennarb/version.rb index 7bb6e51..67c4945 100644 --- a/lib/lennarb/version.rb +++ b/lib/lennarb/version.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Lennarb - VERSION = '0.1.0' + VERSION = '0.1.1' public_constant :VERSION end From e9081c957d40a86aca7d42a5196c5a7361671fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arist=C3=B3teles=20Coutinho?= Date: Tue, 21 Nov 2023 11:03:21 -0300 Subject: [PATCH 2/2] feat: implement Array.wrap to standardize array handling --- CHANGELOG.md | 18 +++++++++--------- Gemfile.lock | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0a5663..144e9e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,26 +4,26 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [Released] -### Added -- New feature for the next release. +## [0.1.1] - 2023-23-11 ### Added -- Initial release of the project. +- Introduced `Array.wrap` extension to the `Array` class for more reliable conversion of objects to arrays within the Lennarb router environment. This method ensures consistent array wrapping of single objects and `nil` values. ### Changed -- Example change to an existing feature. +- Refactored the `put_header` method to use the `Array.wrap` method for more predictable header value handling. +- Renamed methods to have a consistent `assign_` prefix to standardize the API interface: + - `put_header` to `assign_header` + - `write_body` to `assign_body` + - `set_params` to `assign_params` + - `update_status` to `assign_status` ### Deprecated -- Example feature that will be removed in future releases. ### Removed -- Example feature that was removed. ### Fixed -- Example bug fix. ### Security -- Example security fix. diff --git a/Gemfile.lock b/Gemfile.lock index 0db57cb..ad2c235 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - lennarb (0.1.0) + lennarb (0.1.1) colorize (~> 1.1) puma (~> 6.4) rack (~> 3.0, >= 3.0.8)