Module: WrapIt::Enums::ClassMethods
- Included in:
- Base, WrapIt::Enums
- Defined in:
- lib/wrap_it/enums.rb
Overview
WrapIt::Enums class methods
Instance Method Summary collapse
Instance Method Details
#enum(name, values, opts = {}) {|value| ... } ⇒ void
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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/wrap_it/enums.rb', line 80 def enum(name, values, opts = {}, &block) opts.symbolize_keys! name = name.to_sym opts.merge!(block: block, name: name, values: values) opts.key?(:default) && opts[:default] = opts[:default].to_sym if opts.delete(:html_class) == true || opts.key?(:html_class_prefix) opts[:html_class_prefix].is_a?(Symbol) && opts[:html_class_prefix] = opts[:html_class_prefix].to_s prefix = html_class_prefix opts[:html_class_prefix].is_a?(String) && prefix = opts[:html_class_prefix] opts[:regexp] = /\A#{prefix}(?:#{values.join('|')})\z/ opts[:html_class_prefix] = prefix end define_method("#{name}") { @enums[name] ||= opts[:default] } define_method("#{name}=", &Enums.setter(name, &block)) @enums ||= {} o_params = {} if opts.key?(:aliases) aliases = [opts[:aliases]].flatten.compact o_params[:if] = [name] + aliases end @enums[name] = opts option(name, **o_params) { |_, v| send("#{name}=", v) } argument(name, if: Symbol, and: values) { |_, v| send("#{name}=", v) } end |