Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/flexor/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Flexor
VERSION = "0.1.3".freeze
VERSION = "0.1.4".freeze
end
13 changes: 2 additions & 11 deletions lib/flexor/vivification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ class Flexor
##
# Methods for recursively converting raw Hashes and Arrays into Flexor objects.
module Vivification
FLOAT = /[0-9.-]+/
INTEGER = /^(?<!0)[0-9-]+/
DATELIKE = /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}/

private

def vivify(hash)
Expand All @@ -22,13 +18,8 @@ def vivify(hash)

def vivify_value(value)
case value
in Hash then self.class.new(value, root: false)
in Array then vivify_array(value)
in INTEGER then value.to_i
in FLOAT then value.to_f
in DATELIKE
require "datetime"
DateTime.parse(it)
when Hash then self.class.new(value, root: false)
when Array then vivify_array(value)
else value
end
end
Expand Down
10 changes: 0 additions & 10 deletions patterns_spec.rb

This file was deleted.

42 changes: 42 additions & 0 deletions spec/flexor/flexor_constructor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,48 @@
end
end

context "with string values that resemble numbers or dates" do
it "preserves a string of digits as a string" do
store = described_class.new({ zip: "10001" })
expect(store.zip).to eq "10001"
end

it "preserves a leading-zero string without stripping the zeroes" do
store = described_class.new({ zip: "01970" })
expect(store.zip).to eq "01970"
end

it "preserves a currency string instead of zeroing it" do
store = described_class.new({ price: "$19.99" })
expect(store.price).to eq "$19.99"
end

it "preserves a string that merely begins with digits" do
store = described_class.new({ address: "123 Main Street" })
expect(store.address).to eq "123 Main Street"
end

it "preserves prose that happens to contain a number" do
store = described_class.new({ note: "call me at 3pm" })
expect(store.note).to eq "call me at 3pm"
end

it "preserves a hyphenated identifier" do
store = described_class.new({ phone: "555-1234" })
expect(store.phone).to eq "555-1234"
end

it "preserves a decimal-looking string" do
store = described_class.new({ version: "1.2.3" })
expect(store.version).to eq "1.2.3"
end

it "preserves an ISO-8601 timestamp string" do
store = described_class.new({ recorded_at: "2023-01-15T10:30:00" })
expect(store.recorded_at).to eq "2023-01-15T10:30:00"
end
end

context "with a non-hash argument" do
it "raises ArgumentError for strings" do
expect { described_class.new("string") }.to raise_error(ArgumentError)
Expand Down
14 changes: 14 additions & 0 deletions spec/flexor/flexor_writing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@
end
end

describe "assigning a string value that resembles a number" do
subject { described_class.new }

it "preserves the string via the method setter" do
subject.address = "123 Main Street"
expect(subject.address).to eq "123 Main Street"
end

it "preserves the string via the hash accessor" do
subject[:address] = "123 Main Street"
expect(subject[:address]).to eq "123 Main Street"
end
end

describe "#set_raw" do
subject { described_class.new }

Expand Down
Loading