-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuration of optional separator on output. (#149)
- Loading branch information
Showing
6 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# allow for optional configuration of RubyUnits | ||
# | ||
# Usage: | ||
# | ||
# RubyUnits.configure do |config| | ||
# config.separator = false | ||
# end | ||
module RubyUnits | ||
class << self | ||
attr_writer :configuration | ||
end | ||
|
||
def self.configuration | ||
@configuration ||= Configuration.new | ||
end | ||
|
||
def self.reset | ||
@configuration = Configuration.new | ||
end | ||
|
||
def self.configure | ||
yield configuration | ||
end | ||
|
||
# holds actual configuration values for RubyUnits | ||
class Configuration | ||
# used to separate the scalar from the unit when generating output. | ||
# set to nil to prevent adding a space to the string representation of a unit | ||
# separators other than ' ' and '' may work, but you may encounter problems | ||
attr_reader :separator | ||
|
||
def initialize | ||
self.separator = true | ||
end | ||
|
||
def separator=(value) | ||
raise ArgumentError, "configuration 'separator' may only be true or false" unless value.class == TrueClass || value.class == FalseClass | ||
@separator = value ? ' ' : nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'spec_helper' | ||
|
||
describe RubyUnits::Configuration do | ||
context '.separator is true' do | ||
it 'has a space between the scalar and the unit' do | ||
expect(RubyUnits::Unit.new('1 m').to_s).to eq '1 m' | ||
end | ||
end | ||
|
||
context '.separator is false' do | ||
around(:each) do |example| | ||
RubyUnits.configure do |config| | ||
config.separator = false | ||
end | ||
example.run | ||
RubyUnits.reset | ||
end | ||
|
||
it 'does not have a space between the scalar and the unit' do | ||
expect(RubyUnits::Unit.new('1 m').to_s).to eq '1m' | ||
expect(RubyUnits::Unit.new('14.5 lbs').to_s(:lbs)).to eq '14lbs, 8oz' | ||
expect(RubyUnits::Unit.new('220 lbs').to_s(:stone)).to eq '15stone, 10lb' | ||
expect(RubyUnits::Unit.new('14.2 ft').to_s(:ft)).to eq %(14'2") | ||
expect(RubyUnits::Unit.new('1/2 cup').to_s).to eq '1/2cu' | ||
expect(RubyUnits::Unit.new('123.55 lbs').to_s('%0.2f')).to eq '123.55lbs' | ||
end | ||
end | ||
end |