module Axiom::Types::Options

A module that adds class and instance level options

Public Instance Methods

accept_options(*new_options) click to toggle source

Defines which options are valid for a given attribute class

@example

class MyTypes < Axiom::Types::Object
  accept_options :foo, :bar
end

@return [self]

@api public

# File lib/axiom/types/support/options.rb, line 21
def accept_options(*new_options)
  (new_options - accepted_options).each do |new_option|
    assert_method_available(new_option)
    define_option_method(new_option)
    setup_option(new_option)
  end
  self
end

Protected Instance Methods

setup_option(new_option) click to toggle source

Set up the option in the current class and descendants

@param [Symbol] new_option

new option to be added

@return [self]

@api private

# File lib/axiom/types/support/options.rb, line 40
def setup_option(new_option)
  instance_variable_set(:"@#{new_option}", nil)
  accepted_options << new_option
  descendants.each do |descendant|
    descendant.send(__method__, new_option)
  end
  self
end

Private Instance Methods

accepted_options() click to toggle source

Returns an array of valid options

@example

Axiom::Types::String.accepted_options
# => [:primitive, :accessor, :reader, :writer]

@return [Array]

the array of valid option names

@api private

# File lib/axiom/types/support/options.rb, line 91
def accepted_options
  @accepted_options ||= []
end
assert_method_available(name) click to toggle source

Assert that the option is not already defined

@param [Symbol] name

@return [undefined]

@raise [ReservedMethodError]

raised when the method is already defined

@api private

# File lib/axiom/types/support/options.rb, line 105
def assert_method_available(name)
  return unless respond_to?(name)
  raise(
    ReservedMethodError,
    "method named `#{name.inspect}` is already defined"
  )
end
define_option_method(name) click to toggle source

Adds a reader/writer method for the give option name

@param [#to_s] name

@return [undefined]

@api private

# File lib/axiom/types/support/options.rb, line 120
def define_option_method(name)
  ivar = :"@#{name}"
  define_singleton_method(name) do |*args|
    return instance_variable_get(ivar) if args.empty?
    instance_variable_set(ivar, *args)
    self
  end
end
inherited(descendant) click to toggle source

Adds descendant to descendants array and inherits default options

@param [Class] descendant

@return [undefined]

@api private

Calls superclass method
# File lib/axiom/types/support/options.rb, line 58
def inherited(descendant)
  super
  options.each do |option, value|
    descendant.setup_option(option).public_send(option, value)
  end
end
options() click to toggle source

Returns default options hash for a given attribute class

@example

Axiom::Types::String.options
# => {:primitive => String}

@return [Hash]

a hash of default option values

@api private

# File lib/axiom/types/support/options.rb, line 75
def options
  accepted_options.each_with_object({}) do |name, options|
    options[name] = public_send(name)
  end
end