Sha256: eb678cf6c5702fcc8af4c47424f3ba3bd25d0bcf6ac9958233d6ca73b917a60f
Contents?: true
Size: 1006 Bytes
Versions: 6
Compression:
Stored size: 1006 Bytes
Contents
module EnumIsh class Enum attr_accessor :name attr_accessor :mapping attr_accessor :setting def initialize(name, mapping, setting) @name = name @mapping = init_mapping(mapping) @setting = init_setting(setting) end def to_raw(value) if value.is_a?(Array) value.map { |v| @mapping.fetch(v, v) } else @mapping.fetch(value, value) end end def to_sym(value) inverted = @mapping.invert if value.is_a?(Array) value.map { |v| inverted[v] } else inverted[value] end end private def init_mapping(mapping) if mapping.is_a?(Array) mapping.map do |v| k = v.to_s.to_sym v = v.to_s if v.is_a?(Symbol) [k, v] end.to_h else mapping end end def init_setting(setting) [:text, :options].each do |key| setting[key] = true unless setting.key?(key) end setting end end end
Version data entries
6 entries across 6 versions & 1 rubygems