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



120
121
122
123
124
# File 'lib/torque/postgresql/attributes/enum.rb', line 120

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



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/torque/postgresql/attributes/enum.rb', line 205

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

.connection_specification_nameObject

You can specify the connection name for each enum



35
36
37
# File 'lib/torque/postgresql/attributes/enum.rb', line 35

def connection_specification_name
  return self == Enum ? 'primary' : superclass.connection_specification_name
end

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



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

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



27
28
29
30
31
32
# File 'lib/torque/postgresql/attributes/enum.rb', line 27

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_sym] = builder.subtype
  end
end

.keysObject

List of valus as symbols



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

def keys
  values.map(&:to_sym)
end

.lookup(name) ⇒ Object

Find or create the class that will handle the value



17
18
19
20
21
22
23
# File 'lib/torque/postgresql/attributes/enum.rb', line 17

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



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

def members
  values.map(&method(:new))
end

.new(value) ⇒ Object

Overpass new so blank values return only nil



40
41
42
43
# File 'lib/torque/postgresql/attributes/enum.rb', line 40

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



95
96
97
# File 'lib/torque/postgresql/attributes/enum.rb', line 95

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

.textsObject

Get the list of the values translated by I18n



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

def texts
  members.map(&:text)
end

.to_optionsObject

Get a list of values translated and ready for select



70
71
72
# File 'lib/torque/postgresql/attributes/enum.rb', line 70

def to_options
  texts.zip(values)
end

.type_nameObject

Get the type name from its class name



83
84
85
# File 'lib/torque/postgresql/attributes/enum.rb', line 83

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

.valid?(value) ⇒ Boolean

Check if the value is valid

Returns:

  • (Boolean)


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

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



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

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

Instance Method Details

#<=>(other) ⇒ Object

Allow comparison between values of the same enum



127
128
129
130
131
132
133
134
135
# File 'lib/torque/postgresql/attributes/enum.rb', line 127

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



138
139
140
141
142
# File 'lib/torque/postgresql/attributes/enum.rb', line 138

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

#inspectObject

Change the inspection to show the enum name



174
175
176
# File 'lib/torque/postgresql/attributes/enum.rb', line 174

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)


146
147
148
# File 'lib/torque/postgresql/attributes/enum.rb', line 146

def nil?
  self == LAZY_VALUE
end

#replace(value) ⇒ Object

It only accepts if the other value is valid



152
153
154
155
# File 'lib/torque/postgresql/attributes/enum.rb', line 152

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



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

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



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

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

#to_sObject

Change the string result for lazy value



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

def to_s
  nil? ? '' : super
end