class Torque::PostgreSQL::Attributes::Enum

Constants

LAZY_VALUE

Public Class Methods

[](value, *)
Alias for: fetch
fetch(value, *) click to toggle source

Fetch a value from the list see github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/fixtures.rb#L656 see github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/validations/uniqueness.rb#L101

# File lib/torque/postgresql/attributes/enum.rb, line 72
def fetch(value, *)
  new(value.to_s) if values.include?(value)
end
Also aliased as: []
include_on(klass, method_name = nil) click to toggle source

Provide a method on the given class to setup which enums will be manually initialized

# File lib/torque/postgresql/attributes/enum.rb, line 29
def include_on(klass, method_name = nil)
  method_name ||= Torque::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
keys() click to toggle source

List of valus as symbols

# File lib/torque/postgresql/attributes/enum.rb, line 50
def keys
  values.map(&:to_sym)
end
lookup(name) click to toggle source

Find or create the class that will handle the value

# File lib/torque/postgresql/attributes/enum.rb, line 19
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
members() click to toggle source

Different from values, it returns the list of items already casted

# File lib/torque/postgresql/attributes/enum.rb, line 55
def members
  values.map(&method(:new))
end
new(value) click to toggle source

Overpass new so blank values return only nil

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 37
def new(value)
  return Lazy.new(self, LAZY_VALUE) if value.blank?
  super
end
new(value) click to toggle source

Override string initializer to check for a valid value

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 115
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
scope(attribute, value) click to toggle source

Build an active record scope for a given atribute agains a value

# File lib/torque/postgresql/attributes/enum.rb, line 90
def scope(attribute, value)
  attribute.eq(value)
end
texts() click to toggle source

Get the list of the values translated by I18n

# File lib/torque/postgresql/attributes/enum.rb, line 60
def texts
  members.map(&:text)
end
to_options() click to toggle source

Get a list of values translated and ready for select

# File lib/torque/postgresql/attributes/enum.rb, line 65
def to_options
  texts.zip(values)
end
type_name() click to toggle source

Get the type name from its class name

# File lib/torque/postgresql/attributes/enum.rb, line 78
def type_name
  @type_name ||= self.name.demodulize.underscore
end
valid?(value) click to toggle source

Check if the value is valid

# File lib/torque/postgresql/attributes/enum.rb, line 83
def valid?(value)
  return false if self == Enum
  return true if value.equal?(LAZY_VALUE)
  self.values.include?(value.to_s)
end
values() click to toggle source

Load the list of values in a lazy way

# File lib/torque/postgresql/attributes/enum.rb, line 43
def values
  @values ||= self == Enum ? nil : begin
    connection.enum_values(type_name).freeze
  end
end

Private Class Methods

connection() click to toggle source

Get a connection based on its name

# File lib/torque/postgresql/attributes/enum.rb, line 108
def connection
  ::ActiveRecord::Base.connection
end
method_missing(method_name, *arguments) click to toggle source

Allow fast creation of values

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 102
def method_missing(method_name, *arguments)
  return super if self == Enum
  valid?(method_name) ? new(method_name.to_s) : super
end
respond_to_missing?(method_name, include_private = false) click to toggle source

Allows checking value existance

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 97
def respond_to_missing?(method_name, include_private = false)
  valid?(method_name) || super
end

Public Instance Methods

<=>(other) click to toggle source

Allow comparison between values of the same enum

# File lib/torque/postgresql/attributes/enum.rb, line 122
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) click to toggle source

Only allow value comparison with values of the same class

# File lib/torque/postgresql/attributes/enum.rb, line 133
def ==(other)
  (self <=> other) == 0
rescue EnumError
  false
end
Also aliased as: eql?
empty?()
Alias for: nil?
eql?(other)
Alias for: ==
inspect() click to toggle source

Change the inspection to show the enum name

# File lib/torque/postgresql/attributes/enum.rb, line 169
def inspect
  nil? ? 'nil' : ":#{to_s}"
end
nil?() click to toggle source

Since it can have a lazy value, nil can be true here

# File lib/torque/postgresql/attributes/enum.rb, line 141
def nil?
  self == LAZY_VALUE
end
Also aliased as: empty?
replace(value) click to toggle source

It only accepts if the other value is valid

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 147
def replace(value)
  raise_invalid(value) unless self.class.valid?(value)
  super
end
text(attr = nil, model = nil) click to toggle source

Get a translated version of the value

# File lib/torque/postgresql/attributes/enum.rb, line 153
def text(attr = nil, model = nil)
  keys = i18n_keys(attr, model) << self.underscore.humanize
  ::I18n.translate(keys.shift, default: keys)
end
to_i() click to toggle source

Get the index of the value

# File lib/torque/postgresql/attributes/enum.rb, line 164
def to_i
  self.class.values.index(self)
end
to_s() click to toggle source

Change the string result for lazy value

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 159
def to_s
  nil? ? '' : super
end

Private Instance Methods

i18n_keys(attr = nil, model = nil) click to toggle source

Get the i18n keys to check

# File lib/torque/postgresql/attributes/enum.rb, line 176
def i18n_keys(attr = nil, model = nil)
  values = { type: self.class.type_name, value: to_s }
  list_from = :i18n_type_scopes

  if attr && model
    values[:attr] = attr
    values[:model] = model.model_name.i18n_key
    list_from = :i18n_scopes
  end

  Torque::PostgreSQL.config.enum.send(list_from).map do |key|
    (key % values).to_sym
  end
end
method_missing(method_name, *arguments) click to toggle source

Allow '_' to be associated to '-'

Calls superclass method
# File lib/torque/postgresql/attributes/enum.rb, line 200
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
raise_comparison(other) click to toggle source

Throw an exception for comparasion between different enums

# File lib/torque/postgresql/attributes/enum.rb, line 222
def raise_comparison(other)
  raise EnumError, "Comparison of #{self.class.name} with #{self.inspect} failed"
end
raise_invalid(value) click to toggle source

Throw an exception for invalid valus

# File lib/torque/postgresql/attributes/enum.rb, line 213
def raise_invalid(value)
  if value.is_a?(Numeric)
    raise EnumError, "#{value.inspect} is out of bounds of #{self.class.name}"
  else
    raise EnumError, "#{value.inspect} is not valid for #{self.class.name}"
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source

Check for valid '?' and '!' methods

# File lib/torque/postgresql/attributes/enum.rb, line 192
def respond_to_missing?(method_name, include_private = false)
  name = method_name.to_s

  return true if name.chomp!('?')
  name.chomp!('!') && self.class.valid?(name)
end