Class: Enum
- Inherits:
-
Object
- Object
- Enum
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/extra/enum.rb,
lib/inat/data/types/extras.rb
Direct Known Subclasses
INat::App::Config::MessageLevel, INat::App::Config::UpdateMode, INat::Data::Types::ConservationStatus, INat::Data::Types::GeoPrivacy, INat::Data::Types::IconicTaxa, INat::Data::Types::IdentificationCategory, INat::Data::Types::LicenseCode, INat::Data::Types::ProjectAdminRole, INat::Data::Types::ProjectType, INat::Data::Types::QualityGrade, INat::Data::Types::Rank
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Class Method Summary collapse
- .[](name_or_order) ⇒ Object
- .aliases ⇒ Object
- .ddl ⇒ Object
- .each ⇒ Object
- .from_db(src) ⇒ Object
- .get(name_or_order) ⇒ Object private
- .item(name, order = nil, description: nil, data: nil) ⇒ Object private
- .item_alias(**params) ⇒ Object private
- .items(*names, **names_with_order) ⇒ Object private
- .old_parse ⇒ Object
- .parse(src) ⇒ Object
- .to_a ⇒ Object
- .to_h ⇒ Object
- .values ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(name, order, description, data) ⇒ Enum
constructor
A new instance of Enum.
- #inspect ⇒ Object
- #intern ⇒ Object
- #pred ⇒ Object
- #succ ⇒ Object
- #to_db ⇒ Object
- #to_i ⇒ Object
- #to_query ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, order, description, data) ⇒ Enum
Returns a new instance of Enum.
141 142 143 144 145 146 |
# File 'lib/extra/enum.rb', line 141 def initialize name, order, description, data @name = name @order = order @description = description @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
139 140 141 |
# File 'lib/extra/enum.rb', line 139 def data @data end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
139 140 141 |
# File 'lib/extra/enum.rb', line 139 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
139 140 141 |
# File 'lib/extra/enum.rb', line 139 def name @name end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
139 140 141 |
# File 'lib/extra/enum.rb', line 139 def order @order end |
Class Method Details
.[](name_or_order) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/extra/enum.rb', line 108 def [] name_or_order return name_or_order if self === name_or_order case name_or_order when Symbol, Integer @by_name[name_or_order] || @by_order[name_or_order] when Range Range::new get(name_or_order.begin), get(name_or_order.end), name_or_order.exclude_end? else raise TypeError, "Invalid key: #{ name_or_order.inspect }!", caller end end |
.aliases ⇒ Object
75 76 77 78 |
# File 'lib/extra/enum.rb', line 75 def aliases @aliases ||= {} @aliases.dup.freeze end |
.ddl ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/inat/data/types/extras.rb', line 28 def ddl if any? { |v| Integer === v.data } { name: :TEXT, data: :INTEGER } else :TEXT end end |
.each ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/extra/enum.rb', line 90 def each if block_given? @values.sort_by(&:order).each do |item| yield item end else to_enum :each end end |
.from_db(src) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/inat/data/types/extras.rb', line 39 def from_db src return nil if src == nil return self[src.intern] if String === src || Symbol === src name = src[:name]&.intern return nil if name == nil self[name] end |
.get(name_or_order) ⇒ Object (private)
101 102 103 104 105 106 |
# File 'lib/extra/enum.rb', line 101 private def get name_or_order return nil if name == nil result = @by_name[name_or_order] || @by_order[name_or_order] raise ArgumentError, "Invalid name or order: #{ name_or_order.inspect }!", caller if result == nil return result end |
.item(name, order = nil, description: nil, data: nil) ⇒ Object (private)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/extra/enum.rb', line 16 private def item name, order = nil, description: nil, data: nil raise TypeError, "Name of enum value must be a Symbol!", caller unless Symbol === name raise TypeError, "Order of enum value must be a Integer!", caller unless nil === order || Integer === order raise TypeError, "Description of enum value must be a String!", caller unless nil === description || String === description @values ||= [] @by_name ||= {} @by_order ||= {} order ||= (@values.map(&:order).max || 0) + 1 value = new name, order, description, data value.freeze @values << value @by_name[name] = value @by_order[order] = value const_name = if name[0] != name[0].upcase name.upcase else name end const_name = const_name.to_s.gsub '-', '_' self.const_set const_name, value return value end |
.item_alias(**params) ⇒ Object (private)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/extra/enum.rb', line 51 private def item_alias **params @aliases ||= {} params.each do |name, value| raise TypeError, "Alias name must be a Symbol!", caller unless Symbol === name value = self[value] if Symbol === value raise TypeError, "Alias value must be a Symbol or #{ self.name }!", caller unless self === value @aliases[name] = value @by_name[name] = value const_name = if name[0] != name[0].upcase name.upcase else name end const_name = const_name.to_s.gsub '-', '_' self.const_set const_name, value end return aliases end |
.items(*names, **names_with_order) ⇒ Object (private)
40 41 42 43 44 45 46 47 48 |
# File 'lib/extra/enum.rb', line 40 private def items *names, **names_with_order names.each do |name| item name end names_with_order.each do |name, order| item name, order end return values end |
.old_parse ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/inat/data/types/extras.rb', line 13 def parse src, case_sensitive: true return src if self === src raise TypeError, "Source must be a String!", caller unless String === src src = src.strip prefix = self.name + '::' src = src[prefix.length ..] if src.start_with?(prefix) return nil if src.empty? key = if case_sensitive @by_name.keys.find { |v| v.to_s == src } else src = src.downcase @by_name.keys.find { |v| v.to_s.downcase == src } end raise NameError, "Invalid name: #{ src.inspect }!", caller if key.nil? @by_name[key] end |
.parse(src) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/extra/enum.rb', line 120 def parse src, case_sensitive: true return src if self === src raise TypeError, "Source must be a String!", caller unless String === src src = src.strip prefix = self.name + '::' src = src[prefix.length ..] if src.start_with?(prefix) return nil if src.empty? key = if case_sensitive @by_name.keys.find { |v| v.to_s == src } else src = src.downcase @by_name.keys.find { |v| v.to_s.downcase == src } end raise NameError, "Invalid name: #{ src.inspect }!", caller if key.nil? @by_name[key] end |
.to_a ⇒ Object
80 81 82 |
# File 'lib/extra/enum.rb', line 80 def to_a values end |
.to_h ⇒ Object
84 85 86 |
# File 'lib/extra/enum.rb', line 84 def to_h @by_name.dup.freeze end |
.values ⇒ Object
70 71 72 73 |
# File 'lib/extra/enum.rb', line 70 def values @values ||= [] @values.sort_by(&:order).dup.freeze end |
Instance Method Details
#<=>(other) ⇒ Object
175 176 177 178 |
# File 'lib/extra/enum.rb', line 175 def <=> other return nil if !(self.class === other) @order <=> other.order end |
#inspect ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/extra/enum.rb', line 160 def inspect const_name = @name add = '' if @name[0] != @name[0].upcase const_name = "#{@name.upcase}" add = " = #{ @name.inspect }" end add += " @data=#{ @data.inspect }" if @data add += " @description=#{ @description.inspect }" if @description const_name = const_name.to_s.gsub '-', '_' "\#<#{ self.class.name }::#{ const_name }#{ add }>" end |
#intern ⇒ Object
148 149 150 |
# File 'lib/extra/enum.rb', line 148 def intern @name end |
#pred ⇒ Object
184 185 186 |
# File 'lib/extra/enum.rb', line 184 def pred self.class.values.select { |v| v.order < @order }.last end |
#succ ⇒ Object
180 181 182 |
# File 'lib/extra/enum.rb', line 180 def succ self.class.values.select { |v| v.order > @order }.first end |
#to_db ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/inat/data/types/extras.rb', line 49 def to_db if self.class.any? { |v| Integer === v.data } { name: name.to_s, data: data } else name.to_s end end |
#to_i ⇒ Object
156 157 158 |
# File 'lib/extra/enum.rb', line 156 def to_i @order end |
#to_query ⇒ Object
60 61 62 |
# File 'lib/inat/data/types/extras.rb', line 60 def to_query to_s end |
#to_s ⇒ Object
152 153 154 |
# File 'lib/extra/enum.rb', line 152 def to_s @name.to_s end |