Class: Torque::PostgreSQL::Attributes::Enum
- Inherits:
-
String
- Object
- String
- Torque::PostgreSQL::Attributes::Enum
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/torque/postgresql/attributes/enum.rb,
sig/torque/postgresql/attributes/enum.rbs
Defined Under Namespace
Classes: EnumError
Constant Summary collapse
- LAZY_VALUE =
0.chr
Class Method Summary collapse
- .each {|arg0| ... } ⇒ Object
- .fetch(value) ⇒ instance? (also: [])
-
.include_on(klass, method_name = nil) ⇒ Object
Provide a method on the given class to setup which enums will be manually initialized.
- .inherited(subclass) ⇒ Object
-
.keys ⇒ Array[Symbol]
List of values as symbols.
- .length ⇒ Integer
-
.lookup(name) ⇒ singleton(Enum)
Find or create the class that will handle the value.
-
.members ⇒ Array[instance]
Different from values, it returns the list of items already casted.
-
.new(value) ⇒ Object
Overpass new so blank values return only nil.
- .sample ⇒ instance?
-
.scope(attribute, value) ⇒ ::Arel::Nodes::Equality
Build an active record scope for a given attribute against a value.
- .size ⇒ Integer
-
.texts ⇒ Array[String]
Get the list of the values translated by I18n.
-
.to_options ⇒ Array[[String, String]]
Get a list of values translated and ready for select.
-
.type_name ⇒ String
Get the type name from its class name.
-
.valid?(value) ⇒ Boolean
Check if the value is valid.
-
.values ⇒ Array[String]?
Load the list of values in a lazy way.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Allow comparison between values of the same enum.
-
#==(other) ⇒ Boolean
(also: #eql?)
Only allow value comparison with values of the same class.
-
#initialize(value) ⇒ Enum
constructor
Override string initializer to check for a valid value.
-
#inspect ⇒ String
Change the inspection to show the enum name.
-
#nil? ⇒ Boolean
(also: #empty?)
Since it can have a lazy value, nil can be true here.
-
#replace(value) ⇒ self
It only accepts if the other value is valid.
-
#text(attr = nil, model = nil) ⇒ String
Get a translated version of the value.
-
#to_i ⇒ Integer?
Get the index of the value.
-
#to_s ⇒ String
Change the string result for lazy value.
Constructor Details
#initialize(value) ⇒ Enum
Override string initializer to check for a valid value
121 122 123 124 125 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 121 def initialize(value) str_value = value.is_a?(Numeric) ? self.class.values[value.to_i] : value.to_s raise_invalid(value) unless self.class.valid?(str_value) super(str_value) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments) ⇒ Object (private)
Allow '_' to be associated to '-'
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 206 def method_missing(method_name, *arguments) name = method_name.to_s if name.chomp!('?') self == name elsif name.chomp!('!') replace(name) unless self == name else super end end |
Class Method Details
.each {|arg0| ... } ⇒ Object
17 |
# File 'sig/torque/postgresql/attributes/enum.rbs', line 17
def self.each: (*untyped) ?{ (instance) -> untyped } -> untyped
|
.fetch(value) ⇒ instance? Also known as: []
83 84 85 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 83 def fetch(value, *) new(value.to_s) if values.include?(value) end |
.include_on(klass, method_name = nil) ⇒ Object
Provide a method on the given class to setup which enums will be manually initialized
29 30 31 32 33 34 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 29 def include_on(klass, method_name = nil) method_name ||= PostgreSQL.config.enum.base_method Builder.include_on(klass, method_name, Builder::Enum) do |builder| defined_enums[builder.attribute.to_s] = builder.subtype.klass end end |
.inherited(subclass) ⇒ Object
42 43 44 45 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 42 def inherited(subclass) super subclass.instance_variable_set(:@load_values_monitor, ::Monitor.new) end |
.keys ⇒ Array[Symbol]
List of values as symbols
61 62 63 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 61 def keys values.map(&:to_sym) end |
.length ⇒ Integer
20 |
# File 'sig/torque/postgresql/attributes/enum.rbs', line 20
def self.length: () -> Integer
|
.lookup(name) ⇒ singleton(Enum)
Find or create the class that will handle the value
19 20 21 22 23 24 25 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 19 def lookup(name) const = name.to_s.camelize namespace = PostgreSQL.config.enum.namespace return namespace.const_get(const) if namespace.const_defined?(const) namespace.const_set(const, Class.new(Enum)) end |
.members ⇒ Array[instance]
Different from values, it returns the list of items already casted
66 67 68 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 66 def members values.map(&method(:new)) end |
.new(value) ⇒ Object
Overpass new so blank values return only nil
37 38 39 40 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 37 def new(value) return Lazy.new(self, LAZY_VALUE) if value.blank? super end |
.sample ⇒ instance?
18 |
# File 'sig/torque/postgresql/attributes/enum.rbs', line 18
def self.sample: (*untyped) -> instance?
|
.scope(attribute, value) ⇒ ::Arel::Nodes::Equality
Build an active record scope for a given attribute against a value
101 102 103 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 101 def scope(attribute, value) attribute.eq(value) end |
.size ⇒ Integer
19 |
# File 'sig/torque/postgresql/attributes/enum.rbs', line 19
def self.size: () -> Integer
|
.texts ⇒ Array[String]
Get the list of the values translated by I18n
71 72 73 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 71 def texts members.map(&:text) end |
.to_options ⇒ Array[[String, String]]
Get a list of values translated and ready for select
76 77 78 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 76 def texts.zip(values) end |
.type_name ⇒ String
Get the type name from its class name
89 90 91 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 89 def type_name @type_name ||= self.name.demodulize.underscore end |
.valid?(value) ⇒ Boolean
Check if the value is valid
94 95 96 97 98 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 94 def valid?(value) return false if self == Enum return true if value.equal?(LAZY_VALUE) self.values.include?(value.to_s) end |
.values ⇒ Array[String]?
Load the list of values in a lazy way
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 48 def values return if self == Enum return @values if defined?(@values) @load_values_monitor.synchronize do next if defined?(@values) @values = ::ActiveRecord::Base.with_connection { |c| c.enum_values(type_name) }.freeze end @values end |
Instance Method Details
#<=>(other) ⇒ Integer?
Allow comparison between values of the same enum
128 129 130 131 132 133 134 135 136 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 128 def <=>(other) raise_comparison(other) if other.is_a?(Enum) && other.class != self.class case other when Numeric, Enum then to_i <=> other.to_i when String, Symbol then to_i <=> self.class.values.index(other.to_s) else raise_comparison(other) end end |
#==(other) ⇒ Boolean Also known as: eql?
Only allow value comparison with values of the same class
139 140 141 142 143 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 139 def ==(other) (self <=> other) == 0 rescue EnumError false end |
#inspect ⇒ String
Change the inspection to show the enum name
175 176 177 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 175 def inspect nil? ? 'nil' : ":#{to_s}" end |
#nil? ⇒ Boolean Also known as: empty?
Since it can have a lazy value, nil can be true here
147 148 149 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 147 def nil? self == LAZY_VALUE end |
#replace(value) ⇒ self
It only accepts if the other value is valid
153 154 155 156 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 153 def replace(value) raise_invalid(value) unless self.class.valid?(value) super end |
#text(attr = nil, model = nil) ⇒ String
Get a translated version of the value
159 160 161 162 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 159 def text(attr = nil, model = nil) keys = i18n_keys(attr, model) << self.underscore.humanize ::I18n.translate(keys.shift, default: keys) end |
#to_i ⇒ Integer?
Get the index of the value
170 171 172 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 170 def to_i self.class.values.index(self) end |
#to_s ⇒ String
Change the string result for lazy value
165 166 167 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 165 def to_s nil? ? '' : super end |