From f95caaf028bb4e4c3fff29ce32c6343acf5b8175 Mon Sep 17 00:00:00 2001 From: Anton Petrunich Date: Tue, 29 Nov 2016 14:47:41 +0200 Subject: [PATCH] Fix incorrect scope of Vcard.configure and Vcard.configuration methods --- lib/vcard.rb | 4 ++-- lib/vcard/configuration.rb | 14 ++++++++++++-- test/configuration_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 test/configuration_test.rb diff --git a/lib/vcard.rb b/lib/vcard.rb index 960670a..e3a9585 100644 --- a/lib/vcard.rb +++ b/lib/vcard.rb @@ -287,11 +287,11 @@ def self.outer_inner(fields) #:nodoc: return outer, inner end - def configuration + def self.configuration @configuration ||= Configuration.new end - def configure + def self.configure yield configuration end diff --git a/lib/vcard/configuration.rb b/lib/vcard/configuration.rb index 42246fc..b5ecaf0 100644 --- a/lib/vcard/configuration.rb +++ b/lib/vcard/configuration.rb @@ -8,8 +8,18 @@ class Configuration alias_method :ignore_invalid_vcards?, :ignore_invalid_vcards def initialize - @raise_on_invalid = true - @ignore_invalid_vcard = true + set_default_values + end + + def reset + set_default_values + end + + private + + def set_default_values + @raise_on_invalid_line = true + @ignore_invalid_vcards = true end end diff --git a/test/configuration_test.rb b/test/configuration_test.rb new file mode 100644 index 0000000..1c750f8 --- /dev/null +++ b/test/configuration_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class ConfigurationTest < Test::Unit::TestCase + def test_should_be_an_instance_of_configuration + assert Vcard.configuration.is_a?(::Vcard::Configuration) + end + + def test_have_default_values + Vcard.configuration.reset + assert_equal(Vcard.configuration.raise_on_invalid_line, true) + assert_equal(Vcard.configuration.ignore_invalid_vcards, true) + end + + def test_allow_configuration_with_block + Vcard.configuration.reset + Vcard.configure do |config| + config.raise_on_invalid_line = false + config.ignore_invalid_vcards = false + end + assert_equal(Vcard.configuration.raise_on_invalid_line, false) + assert_equal(Vcard.configuration.ignore_invalid_vcards, false) + end +end \ No newline at end of file