Class: Torque::PostgreSQL::Attributes::Enum
- Inherits:
-
String
- Object
- String
- Torque::PostgreSQL::Attributes::Enum
- Includes:
- Comparable
- Defined in:
- lib/torque/postgresql/attributes/enum.rb
Defined Under Namespace
Modules: Base Classes: EnumError
Constant Summary collapse
- LAZY_VALUE =
0.chr
Class Method Summary collapse
-
.connection_specification_name ⇒ Object
You can specify the connection name for each enum.
- .fetch(value) ⇒ Object (also: [])
-
.lookup(name) ⇒ Object
Find or create the class that will handle the value.
-
.new(value) ⇒ Object
Overpass new so blank values return only nil.
-
.type_name ⇒ Object
Get the type name from its class name.
-
.valid?(value) ⇒ Boolean
Check if the value is valid.
-
.values ⇒ Object
Load the list of values in a lazy way.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Allow comparison between values of the same enum.
-
#==(other) ⇒ Object
(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 ⇒ Object
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) ⇒ Object
It only accepts if the other value is valid.
-
#text(attr = nil, model = nil) ⇒ Object
Get a translated version of the value.
-
#to_i ⇒ Object
Get the index of the value.
-
#to_s ⇒ Object
Change the string result for lazy value.
Constructor Details
#initialize(value) ⇒ Enum
Override string initializer to check for a valid value
100 101 102 103 104 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 100 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 ‘-’
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 185 def method_missing(method_name, *arguments) name = method_name.to_s if name.chomp!('?') self == name.tr('_', '-') || self == name elsif name.chomp!('!') replace(name) else super end end |
Class Method Details
.connection_specification_name ⇒ Object
You can specify the connection name for each enum
24 25 26 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 24 def connection_specification_name return self == Enum ? 'primary' : superclass.connection_specification_name end |
.fetch(value) ⇒ Object Also known as: []
46 47 48 49 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 46 def fetch(value, *) return nil unless values.include?(value) send(value) end |
.lookup(name) ⇒ Object
Find or create the class that will handle the value
15 16 17 18 19 20 21 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 15 def lookup(name) const = name.to_s.camelize namespace = Torque::PostgreSQL.config.enum.namespace return namespace.const_get(const) if namespace.const_defined?(const) namespace.const_set(const, Class.new(Enum)) end |
.new(value) ⇒ Object
Overpass new so blank values return only nil
29 30 31 32 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 29 def new(value) return Lazy.new(self, LAZY_VALUE) if value.blank? super end |
.type_name ⇒ Object
Get the type name from its class name
53 54 55 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 53 def type_name @type_name ||= self.name.demodulize.underscore end |
.valid?(value) ⇒ Boolean
Check if the value is valid
58 59 60 61 62 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 58 def valid?(value) return false if self == Enum return true if value.equal?(LAZY_VALUE) self.values.include?(value.to_s) end |
.values ⇒ Object
Load the list of values in a lazy way
35 36 37 38 39 40 41 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 35 def values @values ||= self == Enum ? nil : begin conn_name = connection_specification_name conn = connection(conn_name) conn.enum_values(type_name) end end |
Instance Method Details
#<=>(other) ⇒ Object
Allow comparison between values of the same enum
107 108 109 110 111 112 113 114 115 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 107 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) ⇒ Object Also known as: eql?
Only allow value comparison with values of the same class
118 119 120 121 122 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 118 def ==(other) (self <=> other) == 0 rescue EnumError false end |
#inspect ⇒ Object
Change the inspection to show the enum name
154 155 156 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 154 def inspect nil? ? 'nil' : "#<#{self.class.name} #{super}>" end |
#nil? ⇒ Boolean Also known as: empty?
Since it can have a lazy value, nil can be true here
126 127 128 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 126 def nil? self == LAZY_VALUE end |
#replace(value) ⇒ Object
It only accepts if the other value is valid
132 133 134 135 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 132 def replace(value) raise_invalid(value) unless self.class.valid?(value) super end |
#text(attr = nil, model = nil) ⇒ Object
Get a translated version of the value
138 139 140 141 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 138 def text(attr = nil, model = nil) keys = i18n_keys(attr, model) << self.underscore.humanize ::I18n.translate(keys.shift, default: keys) end |
#to_i ⇒ Object
Get the index of the value
149 150 151 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 149 def to_i self.class.values.index(self) end |
#to_s ⇒ Object
Change the string result for lazy value
144 145 146 |
# File 'lib/torque/postgresql/attributes/enum.rb', line 144 def to_s nil? ? '' : super end |