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

Inherits:
String
  • Object
show all
Extended by:
Enumerable
Includes:
Comparable
Defined in:
lib/torque/postgresql/attributes/enum.rb

Defined Under Namespace

Classes: EnumError

Constant Summary collapse

LAZY_VALUE =
0.chr

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Enum

Override string initializer to check for a valid value



115
116
117
118
119
# 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

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 ‘-’



200
201
202
203
204
205
206
207
208
209
210
# 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

Class Method Details

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



72
73
74
# File 'lib/torque/postgresql/attributes/enum.rb', line 72

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

.keysObject

List of valus as symbols



50
51
52
# File 'lib/torque/postgresql/attributes/enum.rb', line 50

def keys
  values.map(&:to_sym)
end

.lookup(name) ⇒ Object

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 = Torque::PostgreSQL.config.enum.namespace

  return namespace.const_get(const) if namespace.const_defined?(const)
  namespace.const_set(const, Class.new(Enum))
end

.membersObject

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



55
56
57
# File 'lib/torque/postgresql/attributes/enum.rb', line 55

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

.scope(attribute, value) ⇒ Object

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



90
91
92
# File 'lib/torque/postgresql/attributes/enum.rb', line 90

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

.textsObject

Get the list of the values translated by I18n



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

def texts
  members.map(&:text)
end

.to_optionsObject

Get a list of values translated and ready for select



65
66
67
# File 'lib/torque/postgresql/attributes/enum.rb', line 65

def to_options
  texts.zip(values)
end

.type_nameObject

Get the type name from its class name



78
79
80
# File 'lib/torque/postgresql/attributes/enum.rb', line 78

def type_name
  @type_name ||= self.name.demodulize.underscore
end

.valid?(value) ⇒ Boolean

Check if the value is valid

Returns:

  • (Boolean)


83
84
85
86
87
# 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

.valuesObject

Load the list of values in a lazy way



43
44
45
46
47
# File 'lib/torque/postgresql/attributes/enum.rb', line 43

def values
  @values ||= self == Enum ? nil : begin
    connection.enum_values(type_name).freeze
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Allow comparison between values of the same enum



122
123
124
125
126
127
128
129
130
# 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) ⇒ Object Also known as: eql?

Only allow value comparison with values of the same class



133
134
135
136
137
# File 'lib/torque/postgresql/attributes/enum.rb', line 133

def ==(other)
  (self <=> other) == 0
rescue EnumError
  false
end

#inspectObject

Change the inspection to show the enum name



169
170
171
# File 'lib/torque/postgresql/attributes/enum.rb', line 169

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)


141
142
143
# File 'lib/torque/postgresql/attributes/enum.rb', line 141

def nil?
  self == LAZY_VALUE
end

#replace(value) ⇒ Object

It only accepts if the other value is valid



147
148
149
150
# 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) ⇒ Object

Get a translated version of the value



153
154
155
156
# 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_iObject

Get the index of the value



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

def to_i
  self.class.values.index(self)
end

#to_sObject

Change the string result for lazy value



159
160
161
# File 'lib/torque/postgresql/attributes/enum.rb', line 159

def to_s
  nil? ? '' : super
end