Skip to content

Commit

Permalink
Allow the bank to function as a coder. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Nov 6, 2023
1 parent 1500301 commit 24a908b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 33 deletions.
11 changes: 11 additions & 0 deletions lib/latinum/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ def parse(string, default_name: nil)
end
end

def load(input)
if input.is_a?(String)
input = input.strip
return parse(input) unless input.empty?
end
end

def dump(resource)
resource.to_s if resource
end

private def parse_named_resource(name, value)
if formatter = @formatters[name]
return Resource.new(formatter.parse(value), name)
Expand Down
10 changes: 4 additions & 6 deletions lib/latinum/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ def self.parse(string, default_name: nil)
# Load a string representation of a resource.
# @parameter string [String | Nil] e.g. "5 NZD" or nil.
# @returns [Resource | Nil] The Resource that represents the parsed string.
def self.load(string)
if string
# Remove any whitespaces
string = string.strip

parse(string) unless string.empty?
def self.load(input)
if input.is_a?(String)
input = input.strip
return parse(input) unless input.empty?
end
end

Expand Down
31 changes: 31 additions & 0 deletions test/latinum/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@
end
end

with '.load and .dump' do
it "should load and dump resources" do
resource = Latinum::Resource.load("10 NZD")
string_representation = Latinum::Resource.dump(resource)

loaded_resource = bank.load(string_representation)

expect(loaded_resource).to be == loaded_resource
end

it "should load and dump nil correctly" do
expect(bank.load(nil)).to be == nil
expect(bank.dump(nil)).to be == nil
end

it "should handle empty strings correctly" do
expect(bank.load("")).to be == nil
end

it "should handle whitespace strings correctly" do
expect(bank.load(" ")).to be == nil
end

it "should load and dump resources correctly" do
resource = Latinum::Resource.new(10, 'NZD')

expect(bank.load("10.0 NZD")).to be == resource
expect(bank.dump(resource)).to be == "10.0 NZD"
end
end

it "should format the amounts correctly" do
resource = Latinum::Resource.new("10", "NZD")

Expand Down
56 changes: 29 additions & 27 deletions test/latinum/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@
require 'latinum/resource'

describe Latinum::Resource do
it "should load and dump resources" do
resource = Latinum::Resource.load("10 NZD")
string_representation = Latinum::Resource.dump(resource)

loaded_resource = Latinum::Resource.load(string_representation)

expect(loaded_resource).to be == loaded_resource
end

it "should load and dump nil correctly" do
expect(Latinum::Resource.load(nil)).to be == nil
expect(Latinum::Resource.dump(nil)).to be == nil
end

it "should handle empty strings correctly" do
expect(Latinum::Resource.load("")).to be == nil
end

it "should handle whitespace strings correctly" do
expect(Latinum::Resource.load(" ")).to be == nil
end

it "should load and dump resources correctly" do
resource = Latinum::Resource.new(10, 'NZD')

expect(Latinum::Resource.load("10.0 NZD")).to be == resource
expect(Latinum::Resource.dump(resource)).to be == "10.0 NZD"
with '.load and .dump' do
it "should load and dump resources" do
resource = Latinum::Resource.load("10 NZD")
string_representation = Latinum::Resource.dump(resource)

loaded_resource = Latinum::Resource.load(string_representation)

expect(loaded_resource).to be == loaded_resource
end

it "should load and dump nil correctly" do
expect(Latinum::Resource.load(nil)).to be == nil
expect(Latinum::Resource.dump(nil)).to be == nil
end

it "should handle empty strings correctly" do
expect(Latinum::Resource.load("")).to be == nil
end

it "should handle whitespace strings correctly" do
expect(Latinum::Resource.load(" ")).to be == nil
end

it "should load and dump resources correctly" do
resource = Latinum::Resource.new(10, 'NZD')

expect(Latinum::Resource.load("10.0 NZD")).to be == resource
expect(Latinum::Resource.dump(resource)).to be == "10.0 NZD"
end
end

it "should inspect nicely" do
Expand Down

0 comments on commit 24a908b

Please sign in to comment.