Class: EnumExt::EnumWrapper

Inherits:
Object
  • Object
show all
Includes:
Annotated
Defined in:
lib/enum_ext/enum_wrapper.rb

Overview

This is an wrapper class for a basic enum. Since enum values will be freezed right after the definition, we can’t enrich enum directly functionality We can only wrap it with our own object and delegate enum base base functionality internally

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Annotated

#describe, #describe_basic, #describe_enum_i, #describe_humanizations, #describe_long, #describe_mass_assign_enum, #describe_multi_enum_scopes, #describe_short, #describe_supersets, #describe_translations

Constructor Details

#initialize(enum_values, base_class, enum_name, **options) ⇒ EnumWrapper

Returns a new instance of EnumWrapper.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/enum_ext/enum_wrapper.rb', line 13

def initialize(enum_values, base_class, enum_name, **options)
  @enum_values = enum_values
  @supersets = ActiveSupport::HashWithIndifferentAccess.new
  @supersets_raw = ActiveSupport::HashWithIndifferentAccess.new

  @t_options_raw = ActiveSupport::HashWithIndifferentAccess.new
  @localizations = ActiveSupport::HashWithIndifferentAccess.new

  @base_class = base_class
  @enum_name = enum_name
  @suffix = options[:suffix] || options[:_suffix]
  @prefix = options[:prefix] || options[:_prefix]
end

Instance Attribute Details

#base_classObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def base_class
  @base_class
end

#enum_nameObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def enum_name
  @enum_name
end

#enum_valuesObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def enum_values
  @enum_values
end

#localizationsObject (readonly) Also known as: t

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def localizations
  @localizations
end

#prefixObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def prefix
  @prefix
end

#suffixObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def suffix
  @suffix
end

#supersetsObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def supersets
  @supersets
end

#supersets_rawObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def supersets_raw
  @supersets_raw
end

#t_options_rawObject (readonly)

supersets is storing exact definitions, if you need a raw mapping use class.statuses.superset_statuses



8
9
10
# File 'lib/enum_ext/enum_wrapper.rb', line 8

def t_options_raw
  @t_options_raw
end

Instance Method Details

#allObject



37
38
39
40
41
42
43
44
# File 'lib/enum_ext/enum_wrapper.rb', line 37

def all
  {
    **enum_values,
    supersets: {
      **send(:supersets_raw)
    }
  }
end

#superset_to_enum(*enum_or_sets) ⇒ Object

ext_sets_to_kinds( :ready_for_shipment, :delivery_set ) –>

[:ready_for_shipment, :on_delivery, :delivered]


29
30
31
32
33
34
35
# File 'lib/enum_ext/enum_wrapper.rb', line 29

def superset_to_enum( *enum_or_sets )
  return [] if enum_or_sets.blank?
  enum_or_sets_strs = enum_or_sets.map(&:to_s)

  next_level_deeper = supersets.slice( *enum_or_sets_strs ).values.flatten
  (enum_or_sets_strs & enum_values.keys | send(:superset_to_enum, *next_level_deeper)).uniq
end

#t_optionsObject



50
51
52
# File 'lib/enum_ext/enum_wrapper.rb', line 50

def t_options
  evaluate_localizations(localizations)
end

#t_options_iObject



46
47
48
# File 'lib/enum_ext/enum_wrapper.rb', line 46

def t_options_i
  evaluate_localizations_to_i(localizations)
end

#transform_enum_label(label) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/enum_ext/enum_wrapper.rb', line 56

def transform_enum_label(label)
  _prefix = if prefix
    prefix == true ? "#{enum_name}_" : "#{prefix}_"
  end

  _suffix = if suffix
    suffix == true ? "_#{enum_name}" : "_#{suffix}"
  end

  "#{_prefix}#{label}#{_suffix}"
end