Class: Torque::PostgreSQL::Attributes::Enum

Inherits:
String
  • Object
show all
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 =

Returns:

  • (String)
0.chr

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Enum

Override string initializer to check for a valid value

Parameters:

  • value (Object)


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 '-'

Parameters:

  • method_name (Symbol)
  • arguments (Object)

Returns:

  • (Object)


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

Parameters:

  • (Object)

Yields:

Yield Parameters:

  • arg0 (instance)

Yield Returns:

  • (Object)

Returns:

  • (Object)


17
# File 'sig/torque/postgresql/attributes/enum.rbs', line 17

def self.each: (*untyped) ?{ (instance) -> untyped } -> untyped

.fetch(value) ⇒ instance? Also known as: []

Parameters:

  • value (String, Symbol)
  • (Object)

Returns:

  • (instance, nil)


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

Parameters:

  • klass (model)
  • method_name (Symbol, nil) (defaults to: nil)

Returns:

  • (Object)


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

Parameters:

  • subclass (singleton(Enum))

Returns:

  • (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

Returns:

  • (Array[Symbol])


61
62
63
# File 'lib/torque/postgresql/attributes/enum.rb', line 61

def keys
  values.map(&:to_sym)
end

.length ⇒ Integer

Returns:

  • (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

Parameters:

  • name (table_ref)

Returns:



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

Returns:

  • (Array[instance])


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

Parameters:

  • value (Object)

Returns:

  • (Object)


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?

Parameters:

  • (Object)

Returns:

  • (instance, nil)


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

Parameters:

  • attribute (arel_attribute)
  • value (String, Symbol)

Returns:

  • (::Arel::Nodes::Equality)


101
102
103
# File 'lib/torque/postgresql/attributes/enum.rb', line 101

def scope(attribute, value)
  attribute.eq(value)
end

.size ⇒ Integer

Returns:

  • (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

Returns:

  • (Array[String])


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

Returns:

  • (Array[[String, String]])


76
77
78
# File 'lib/torque/postgresql/attributes/enum.rb', line 76

def to_options
  texts.zip(values)
end

.type_name ⇒ String

Get the type name from its class name

Returns:

  • (String)


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

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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

Returns:

  • (Array[String], nil)


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

Parameters:

  • other (Object)

Returns:

  • (Integer, nil)


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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Returns:

  • (String)


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

Returns:

  • (Boolean)


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

Parameters:

  • value (String, Symbol)

Returns:

  • (self)


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

Parameters:

  • attr (column_ref, nil) (defaults to: nil)
  • model (Object) (defaults to: nil)

Returns:

  • (String)


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

Returns:

  • (Integer, nil)


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

Returns:

  • (String)


165
166
167
# File 'lib/torque/postgresql/attributes/enum.rb', line 165

def to_s
  nil? ? '' : super
end