Skip to content

Commit

Permalink
Move ConfigurationLogic inside Configuration
Browse files Browse the repository at this point in the history
Another documentation-related fix. ConfigurationLogic is meant to be
used exclusively with Configuration; it makes sense then to keep it
entirely within the Configuration class.
  • Loading branch information
zachahn committed Jan 25, 2020
1 parent 78ef4a5 commit 06e293d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
103 changes: 59 additions & 44 deletions lib/super/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module Super
# @yield [Configuration]
# @return [Configuration]
def self.configuration
@configuration ||= Configuration.new

Expand All @@ -9,78 +11,91 @@ def self.configuration
@configuration
end

module ConfigurationLogic
def self.included(base)
base.extend(ClassMethods)
end

def initialize
self.class.defaults.each do |key, value|
if value.respond_to?(:call)
value = value.call
end

public_send("#{key}=", value)
# Allows setting global configuration
#
# ```ruby
# Super.configuration do |c|
# c.title = "My Admin Site"
# end
# ```
class Configuration
module ConfigurationLogic # @api private
def self.included(base)
base.extend(ClassMethods)
end
end

def configured?(attr)
instance_variable_defined?("@#{attr}")
end
def initialize
self.class.defaults.each do |key, value|
if value.respond_to?(:call)
value = value.call
end

module ClassMethods
def defaults
@defaults ||= {}
public_send("#{key}=", value)
end
end

def wraps
@wraps ||= {}
def configured?(attr)
instance_variable_defined?("@#{attr}")
end

def configure(attr, wrap: nil, enum: nil, **kwargs)
if kwargs.key?(:default)
defaults[attr] = kwargs[:default]
module ClassMethods
def defaults
@defaults ||= {}
end

define_method(attr) do
if !configured?(attr)
raise Error::UnconfiguredConfiguration, "unconfigured: #{attr}"
def wraps
@wraps ||= {}
end

def configure(attr, wrap: nil, enum: nil, **kwargs)
if kwargs.key?(:default)
defaults[attr] = kwargs[:default]
end

result = instance_variable_get("@#{attr}")
define_method(attr) do
if !configured?(attr)
raise Error::UnconfiguredConfiguration, "unconfigured: #{attr}"
end

if wrap.nil?
result
else
wrap.call(result)
end
end
result = instance_variable_get("@#{attr}")

define_method("#{attr}=") do |value|
if enum.is_a?(Array)
if !enum.include?(value)
raise Error::InvalidConfiguration,
"tried to set `#{attr}` to `#{value.inspect}`, " \
"expected: #{enum.join(", ")}"
if wrap.nil?
result
else
wrap.call(result)
end
end

instance_variable_set("@#{attr}", value)
value
define_method("#{attr}=") do |value|
if enum.is_a?(Array)
if !enum.include?(value)
raise Error::InvalidConfiguration,
"tried to set `#{attr}` to `#{value.inspect}`, " \
"expected: #{enum.join(", ")}"
end
end

instance_variable_set("@#{attr}", value)
value
end
end
end
end
end

class Configuration
include ConfigurationLogic

# @!attribute [rw]
configure :title
# @!attribute [rw]
configure :index_resources_per_page, default: 20
# @!attribute [rw]
configure :controller_namespace, default: "admin"
# @!attribute [rw]
configure :route_namespace, default: :admin, wrap: -> (val) { [val].flatten }
# @!attribute [rw]
configure :asset_handler, default: -> { Super::Assets.auto }

# @api private
def path_parts(*parts)
route_namespace + parts
end
Expand Down
2 changes: 1 addition & 1 deletion test/lib/super/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ConfigurationTest < ActiveSupport::TestCase
class Testing
include Super::ConfigurationLogic
include Super::Configuration::ConfigurationLogic

configure :no_modifiers
configure :has_default, default: "i am the default"
Expand Down

0 comments on commit 06e293d

Please sign in to comment.