Module: Coercible::Options

Included in:
Coercer::Object
Defined in:
lib/project/support/options.rb

Overview

A module that adds class and instance level options

Constant Summary collapse

Undefined =
Class.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(descendant) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hook called when descendant was extended

Parameters:

  • descendant (Class, Module)

Returns:

  • (undefined)


14
15
16
# File 'lib/project/support/options.rb', line 14

def self.extended(descendant)
  descendant.extend(DescendantsTracker)
end

Instance Method Details

#accept_options(*new_options) ⇒ self

Defines which options are valid for a given attribute class

Examples:

class MyAttribute < Virtus::Attribute::Object
  accept_options :foo, :bar
end

Returns:

  • (self)


59
60
61
62
63
64
# File 'lib/project/support/options.rb', line 59

def accept_options(*new_options)
  add_accepted_options(new_options)
  new_options.each { |option| define_option_method(option) }
  descendants.each { |descendant| descendant.add_accepted_options(new_options) }
  self
end

#accepted_optionsArray

Returns an array of valid options

Examples:

Virtus::Attribute::String.accepted_options
# => [:primitive, :accessor, :reader, :writer]

Returns:

  • (Array)

    the array of valid option names



45
46
47
# File 'lib/project/support/options.rb', line 45

def accepted_options
  @accepted_options ||= []
end

#optionsHash

Returns default options hash for a given attribute class

Examples:

Virtus::Attribute::String.options
# => {:primitive => String}

Returns:

  • (Hash)

    a hash of default option values



28
29
30
31
32
33
# File 'lib/project/support/options.rb', line 28

def options
  accepted_options.each_with_object({}) do |option_name, options|
    option_value         = send(option_name)
    options[option_name] = option_value unless option_value.nil?
  end
end