From 8c128a3b24438d81aaff9e9270f6cbfe4509379e Mon Sep 17 00:00:00 2001 From: Chris Welham <71787007+apexatoll@users.noreply.github.com> Date: Sun, 1 Oct 2023 23:12:07 +0100 Subject: [PATCH] Refactor configurator to use attributes concern --- lib/kangaru/configurators/configurator.rb | 10 +++---- sig/kangaru/configurators/configurator.rbs | 5 ++-- .../configurators/configurator_spec.rb | 26 ------------------- 3 files changed, 5 insertions(+), 36 deletions(-) diff --git a/lib/kangaru/configurators/configurator.rb b/lib/kangaru/configurators/configurator.rb index 866d704..e2be83d 100644 --- a/lib/kangaru/configurators/configurator.rb +++ b/lib/kangaru/configurators/configurator.rb @@ -1,6 +1,8 @@ module Kangaru module Configurators class Configurator + include Concerns::AttributesConcern + using Patches::Inflections def self.name @@ -10,14 +12,8 @@ def self.name .to_sym end - def self.settings - @settings ||= instance_methods.grep(/\w=$/).map do |setting| - setting.to_s.delete_suffix("=").to_sym - end - end - def serialise - self.class.settings.to_h do |setting| + self.class.attributes.to_h do |setting| [setting, send(setting)] end.compact end diff --git a/sig/kangaru/configurators/configurator.rbs b/sig/kangaru/configurators/configurator.rbs index 7343089..ce47654 100644 --- a/sig/kangaru/configurators/configurator.rbs +++ b/sig/kangaru/configurators/configurator.rbs @@ -1,12 +1,11 @@ module Kangaru module Configurators class Configurator - self.@settings: Array[Symbol] + include Concerns::AttributesConcern + extend Concerns::AttributesConcern::ClassMethods def self.name: -> Symbol - def self.settings: -> Array[Symbol] - def serialise: -> Hash[Symbol, untyped] end end diff --git a/spec/kangaru/configurators/configurator_spec.rb b/spec/kangaru/configurators/configurator_spec.rb index 2a9c20b..c40e617 100644 --- a/spec/kangaru/configurators/configurator_spec.rb +++ b/spec/kangaru/configurators/configurator_spec.rb @@ -13,32 +13,6 @@ def self.to_s = "FoobarConfigurator" end end - describe ".settings" do - subject(:settings) { configurator_class.settings } - - context "when no attr_accessors are set" do - let(:configurator_class) do - Class.new(described_class) - end - - it "returns an empty array" do - expect(settings).to be_empty - end - end - - context "when attr_accessors are set" do - let(:configurator_class) do - Class.new(described_class) do - attr_accessor :foo, :bar, :baz - end - end - - it "returns the expected settings" do - expect(settings).to contain_exactly(:foo, :bar, :baz) - end - end - end - describe "#serialise" do subject(:hash) { configurator.serialise }