Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ semantics
## 2.1.1

* Fix `Time` converter

## 2.2.0

* Add `Array` converter
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ All converters return nil if conversion could not be made.
- Symbol
- Time
- Boolean
- Array

### String type converter options

Expand Down
5 changes: 5 additions & 0 deletions lib/tainbox/type_converter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/values/time_zone'

class Tainbox::TypeConverter
Expand Down Expand Up @@ -72,3 +73,7 @@ def convert
!!value
end
end

Tainbox.define_converter(Array) do
Array.wrap(value) rescue nil
end
2 changes: 1 addition & 1 deletion lib/tainbox/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Tainbox
VERSION = '2.1.2'
VERSION = '2.2.0'
end
30 changes: 30 additions & 0 deletions spec/tainbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,34 @@ def name
expect(oliver.name).to eq('John')
end
end

describe 'array converter' do
subject { person.phone_numbers }

let(:person) do
Class.new do
include Tainbox

attribute :phone_numbers, Array
end.new(phone_numbers: phone_numbers)
end

context 'without value' do
let(:phone_numbers) { nil }

it { is_expected.to eq([]) }
end

context 'with array as a value' do
let(:phone_numbers) { [12345, 67890] }

it { is_expected.to eq(phone_numbers) }
end

context 'with string as a value' do
let(:phone_numbers) { '+1 234-500' }

it { is_expected.to eq([phone_numbers]) }
end
end
end