Skip to content

Commit

Permalink
Refactor configurator to use attributes concern
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Oct 1, 2023
1 parent 1c9e78f commit 8c128a3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
10 changes: 3 additions & 7 deletions lib/kangaru/configurators/configurator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Kangaru
module Configurators
class Configurator
include Concerns::AttributesConcern

using Patches::Inflections

def self.name
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions sig/kangaru/configurators/configurator.rbs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 0 additions & 26 deletions spec/kangaru/configurators/configurator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit 8c128a3

Please sign in to comment.