From 4b7f02c67e1a73cee04ea9c73ce8bc887b93d656 Mon Sep 17 00:00:00 2001 From: Sampson Crowley Date: Fri, 3 May 2019 12:10:34 -0600 Subject: [PATCH] fix components config --- lib/inky.rb | 2 +- lib/inky/configuration.rb | 23 +++++++- spec/configuration_spec.rb | 114 ++++++++++++++++++++++++++++++------- 3 files changed, 115 insertions(+), 24 deletions(-) diff --git a/lib/inky.rb b/lib/inky.rb index a97bfb6..19b0369 100644 --- a/lib/inky.rb +++ b/lib/inky.rb @@ -21,7 +21,7 @@ def initialize(options = {}) spacer: 'spacer', wrapper: 'wrapper', menu_item: 'item' - }.merge(options[:components] || {}) + }.merge(::Inky.configuration.components).merge(options[:components] || {}) self.component_lookup = components.invert diff --git a/lib/inky/configuration.rb b/lib/inky/configuration.rb index 90026e3..99b23a6 100644 --- a/lib/inky/configuration.rb +++ b/lib/inky/configuration.rb @@ -7,6 +7,8 @@ def self.configuration # Set Inky's configuration # @param config [Inky::Configuration] def self.configuration=(config) + raise TypeError, "Not an Inky::Configuration" unless config.is_a?(Configuration) + @configuration = config end @@ -23,11 +25,30 @@ def self.configure end class Configuration - attr_accessor :template_engine, :column_count + attr_reader :template_engine, :column_count, :components def initialize @template_engine = :erb @column_count = 12 + @components = {} + end + + def template_engine=(value) + raise TypeError, "#{value.inspect} (#{value.class}) does not respond to 'to_sym'" unless value.respond_to?(:to_sym) + + @template_engine = value.to_sym + end + + def components=(value) + raise TypeError, "#{value.inspect} (#{value.class}) does not respond to 'to_hash'" unless value.respond_to?(:to_hash) + + @components = value.to_hash + end + + def column_count=(value) + raise TypeError, "#{value.inspect} (#{value.class}) does not respond to 'to_int'" unless value.respond_to?(:to_int) + + @column_count = value.to_int end end end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index d2e6615..5a8b116 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -1,44 +1,114 @@ require 'spec_helper' -RSpec.describe "Configuration" do - around do |spec| - Inky.configure do |config| - old = config.template_engine - spec.run - config.template_engine = old +RSpec.describe "Inky.configuration" do + it "returns an Inky::Configuration object" do + expect(Inky.configuration).to be_an_instance_of Inky::Configuration + end + + describe "=" do + it "accepts an Inky::Configuration" do + current_config = Inky.configuration + config = Inky::Configuration.new + + Inky.configuration = config + expect(Inky.configuration).to_not eq(current_config) + expect(Inky.configuration).to eq(config) + + expect { Inky.configuration = {} }.to raise_error(TypeError, "Not an Inky::Configuration") + expect(Inky.configuration).to eq(config) + + Inky.configuration = current_config + expect(Inky.configuration).to eq(current_config) end end - it "default value is :erb" do - Inky::Configuration.new.template_engine = :erb + describe "&block" do + it "yields the current configuration" do + current_config = Inky.configuration + new_config = Inky::Configuration.new + + Inky.configuration = new_config + + Inky.configuration do |config| + expect(config).to be_an_instance_of Inky::Configuration + expect(config).to_not eq(current_config) + expect(config).to eq(new_config) + end + + Inky.configuration = current_config + end + end +end + +RSpec.describe "Configuration" do + describe "#template_engine" do + it "default value is :erb" do + expect(Inky::Configuration.new.template_engine).to eq(:erb) + end end - describe "#configuration=" do - it "can set template_engine" do + describe "#template_engine=" do + it "sets/updates the template_engine" do config = Inky::Configuration.new config.template_engine = :haml expect(config.template_engine).to eq(:haml) end - it "can set column_count" do + it "accepts symbols" do config = Inky::Configuration.new - config.column_count = 4 - expect(config.column_count).to eq(4) + value = [] + expect { config.template_engine = value }.to raise_error(TypeError, "#{value.inspect} (#{value.class}) does not respond to 'to_sym'") + expect(config.template_engine).to eq(:erb) end end - describe "#configuration=" do - before do - Inky.configure do |config| - config.template_engine = :haml - end + describe "#column_count" do + it "default value is 12" do + expect(Inky::Configuration.new.column_count).to eq(12) end + end - it "returns :haml as configured template_engine" do - template_engine = Inky.configuration.template_engine + describe "#column_count=" do + it "sets/updates the column_count" do + config = Inky::Configuration.new + config.column_count = 24 + expect(config.column_count).to eq(24) + end - expect(template_engine).to be_a(Symbol) - expect(template_engine).to eq(:haml) + it "accepts integers" do + config = Inky::Configuration.new + value = :haml + expect { config.column_count = value }.to raise_error(TypeError, "#{value.inspect} (#{value.class}) does not respond to 'to_int'") + expect(config.column_count).to eq(12) + end + end + + describe "#components" do + it "defaults to an empty hash" do + config = Inky::Configuration.new + expect(config.components).to eq({}) + end + end + + describe "#components=" do + it "can set overriden component tags" do + config = Inky::Configuration.new + config.components = { button: 'inky-button' } + expect(config.components).to eq(button: 'inky-button') + end + + it "will not set an invalid components override" do + config = Inky::Configuration.new + [ + nil, + 1, + "{}", + false, + true + ].each do |value| + expect { config.components = value }.to raise_error(TypeError, "#{value.inspect} (#{value.class}) does not respond to 'to_hash'") + expect(config.components).to eq({}) + end end end end