module Virtus::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 MyAttribute < Virtus::Attribute::Object
  accept_options :foo, :bar
end

@return [self]

@api public

# 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_options() click to toggle source

Returns an array of valid options

@example

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

@return [Array]

the array of valid option names

@api public

# File lib/virtus/support/options.rb, line 33
def accepted_options
  @accepted_options ||= []
end
options() click to toggle source

Returns default options hash for a given attribute class

@example

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

@return [Hash]

a hash of default option values

@api public

# 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

Protected Instance Methods

add_accepted_options(new_options) click to toggle source

Adds new options that an attribute class can accept

@param [#to_ary] new_options

new options to be added

@return [self]

@api private

# File lib/virtus/support/options.rb, line 93
def add_accepted_options(new_options)
  accepted_options.concat(new_options)
  self
end
define_option_method(option) click to toggle source

Adds a reader/writer method for the give option name

@return [undefined]

@api private

# File lib/virtus/support/options.rb, line 61
    def define_option_method(option)
      class_eval "        def self.#{option}(value = Undefined)           # def self.primitive(value = Undefined)
          @#{option} = nil unless defined?(@#{option})  #   @primitive = nil unless defined?(@primitive)
          return @#{option} if value.equal?(Undefined)  #   return @primitive if value.equal?(Undefined)
          @#{option} = value                            #   @primitive = value
          self                                          #   self
        end                                             # end
", __FILE__, __LINE__ + 1
    end
set_options(new_options) click to toggle source

Sets default options

@param [#each] new_options

options to be set

@return [self]

@api private

# File lib/virtus/support/options.rb, line 80
def set_options(new_options)
  new_options.each { |pair| send(*pair) }
  self
end

Private Instance Methods

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/virtus/support/options.rb, line 107
def inherited(descendant)
  super
  descendant.add_accepted_options(accepted_options).set_options(options)
end