Module: WrapIt::Enums

Extended by:
DerivedAttributes, ClassMethods
Included in:
Base
Defined in:
lib/wrap_it/enums.rb

Overview

Adds enums functionality

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.enum(name, values, opts = {}) {|value| ... } ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Adds enum. When element created, creation arguments will be scanned for Symbol, that included contains in values. If it founded, enum takes this value. Also creation options inspected. If its contains name: value key-value pair with valid value, this pair removed from options and enum takes this value.

If you set html_class option to true, with each enum change, HTML class, composed from html_class_prefix and enum value will be added to element. If you want to override this prefix, specify it with html_class_prefix option. By default, enum changes are not affected to html classes.

This method also adds getter and setter for this enum.

Examples:

class Button < WrapIt::Base
  enum :style, %i(red green black), html_class_prefix: 'btn-'
end

btn = Button.new(template, :green)
btn.render # => '<div class="btn-green">'
btn = Button.new(template, style: :red)
btn.render # => '<div class="btn-red">'

Options Hash (opts):

  • :html_class_prefix (String, Symbol)

    prefix of HTML class that will automatically added to element if enum changes its value.

  • :html_class (Boolean)

    whether this enum changes should affect to html class.

  • :aliases (Symbol, Array<Symbol>)

    list of enum aliases. Warning! Values are not converted - pass only Symbols here.

  • :default (String, Symbol)

    default value for enum, if nil or wrong value given. Converted to Symbol.

Yields:

  • (value)

    Runs block when enum value changed, gives it to block.

Yield Parameters:

  • value (Symbol)

    New enum value.

Yield Returns:

  • (void)