Module: Virtus::Options

Included in:
Attribute
Defined in:
lib/virtus/support/options.rb

Overview

A module that adds class and instance level options

Instance Method Summary collapse

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)


47
48
49
50
51
52
# File 'lib/virtus/support/options.rb', line 47

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



33
34
35
# File 'lib/virtus/support/options.rb', line 33

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



16
17
18
19
20
21
# File 'lib/virtus/support/options.rb', line 16

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