Module: Showroom::Core::Configurable

Included in:
Showroom, Showroom::Client
Defined in:
lib/showroom/core/configurable.rb

Overview

Mixin that provides configuration DSL for the Showroom module and Client.

When extended into a module or class it adds ‘configure`, `reset!`, `options`, and `same_options?`, plus individual key accessors.

Examples:

Module-level usage

Showroom.configure do |c|
  c.store = 'example.myshopify.com'
end

Constant Summary collapse

KEYS =

Ordered list of all supported configuration keys.

%i[
  store
  user_agent
  per_page
  pagination_depth
  open_timeout
  timeout
  middleware
  connection_options
  debug
].freeze

Instance Method Summary collapse

Instance Method Details

#configure {|self| ... } ⇒ self

Yields self for block-style configuration.

Yields:

  • (self)

Returns:

  • (self)


41
42
43
44
# File 'lib/showroom/core/configurable.rb', line 41

def configure
  yield self
  self
end

#optionsHash{Symbol => Object}

Returns a frozen hash snapshot of the current configuration.

Returns:

  • (Hash{Symbol => Object})


56
57
58
# File 'lib/showroom/core/configurable.rb', line 56

def options
  KEYS.to_h { |key| [key, send(key)] }.freeze
end

#per_page=(value) ⇒ void

This method returns an undefined value.

Clamps per_page so it never exceeds Default::MAX_PER_PAGE.

Parameters:

  • value (Integer)


72
73
74
# File 'lib/showroom/core/configurable.rb', line 72

def per_page=(value)
  @per_page = [value.to_i, Default::MAX_PER_PAGE].min
end

#reset!void

This method returns an undefined value.

Resets all keys to their defaults from Default.



49
50
51
# File 'lib/showroom/core/configurable.rb', line 49

def reset!
  KEYS.each { |key| send(:"#{key}=", Default.public_send(key)) }
end

#same_options?(other_options) ⇒ Boolean

Returns true when other_options matches the current configuration.

Parameters:

  • other_options (Hash)

Returns:

  • (Boolean)


64
65
66
# File 'lib/showroom/core/configurable.rb', line 64

def same_options?(other_options)
  options == other_options
end

#storeString?

Returns:

  • (String, nil)


35
# File 'lib/showroom/core/configurable.rb', line 35

KEYS.each { |key| attr_accessor key }

#store=(value) ⇒ Object

Parameters:

  • value (String, nil)


35
# File 'lib/showroom/core/configurable.rb', line 35

KEYS.each { |key| attr_accessor key }