Class: Enum

Inherits:
SimpleDelegator show all
Defined in:
lib/trax/core/primitives/enum.rb

Overview

Accepts either an integer or the name when setting a value ProductCategory.new(1) => #:clothing, :value => 1

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Enum

Returns a new instance of Enum.



161
162
163
# File 'lib/trax/core/primitives/enum.rb', line 161

def initialize(val)
  self.choice = val unless val.nil? && self.class.allow_nil
end

Class Attribute Details

._names_as_stringsObject

Returns the value of attribute _names_as_strings.



147
148
149
# File 'lib/trax/core/primitives/enum.rb', line 147

def _names_as_strings
  @_names_as_strings
end

._names_hashObject

Returns the value of attribute _names_hash.



146
147
148
# File 'lib/trax/core/primitives/enum.rb', line 146

def _names_hash
  @_names_hash
end

._values_hashObject

Returns the value of attribute _values_hash.



145
146
147
# File 'lib/trax/core/primitives/enum.rb', line 145

def _values_hash
  @_values_hash
end

Instance Attribute Details

#choiceObject

Instance Methods ###



159
160
161
# File 'lib/trax/core/primitives/enum.rb', line 159

def choice
  @choice
end

Class Method Details

.===(val) ⇒ Object



124
125
126
# File 'lib/trax/core/primitives/enum.rb', line 124

def self.===(val)
  _names_hash.values.any?{|v| v === val }
end

.[](val) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/trax/core/primitives/enum.rb', line 41

def self.[](val)
  if ::Is.numeric?(val)
    self._values_hash[val]
  elsif ::Is.symbolic?(val)
    val = val.to_sym if val.is_a?(String)
    self._names_hash[val]
  elsif val.superclass.name == "EnumValue"
    val = val.to_sym
    self._names_hash[val]
  end
end

.as_json(options = {}) ⇒ Object



53
54
55
# File 'lib/trax/core/primitives/enum.rb', line 53

def self.as_json(options={})
  choice.to_s
end

.choicesObject



57
58
59
# File 'lib/trax/core/primitives/enum.rb', line 57

def self.choices
  @choices ||= self._values_hash.values
end

.define_enum_value(const_name, val = nil) ⇒ Object Also known as: enum_value, define

Class Methods ###



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trax/core/primitives/enum.rb', line 24

def self.define_enum_value(const_name, val=nil)
  name = "#{const_name}".underscore.to_sym
  const_name = name.to_s.camelize
  val = (self._values_hash.length + 1) if val.nil?

  raise ::Trax::Core::Errors::DuplicateEnumValue.new(:klass => self.class.name, :value => const_name) if self === name
  raise ::Trax::Core::Errors::DuplicateEnumValue.new(:klass => self.class.name, :value => val) if self === val

  value_klass = self.const_set(const_name, Class.new(::EnumValue){
    self.tag = name
    self.value = val
  })

  self._values_hash[val] = value_klass
  self._names_hash[name] = value_klass
end

.define_values(*args) ⇒ Object

define multiple values if its iterable



77
78
79
80
81
# File 'lib/trax/core/primitives/enum.rb', line 77

def self.define_values(*args)
  args.each_with_index do |arg, i|
    define_enum_value(arg, (i + 1))
  end
end

.each(&block) ⇒ Object



83
84
85
# File 'lib/trax/core/primitives/enum.rb', line 83

def self.each(&block)
  keys.each(&block)
end

.each_pair(&block) ⇒ Object



87
88
89
# File 'lib/trax/core/primitives/enum.rb', line 87

def self.each_pair(&block)
  self._names_hash.each_pair(&block)
end

.formatted_choicesObject



61
62
63
64
65
# File 'lib/trax/core/primitives/enum.rb', line 61

def self.formatted_choices
  @formatted_choices ||= choices.each_with_object({}) do |choice, hash|
    hash[choice.to_i] = choice.to_s
  end
end

.inherited(subklass) ⇒ Object

Hooks ###



151
152
153
154
155
156
# File 'lib/trax/core/primitives/enum.rb', line 151

def self.inherited(subklass)
  subklass.instance_variable_set(:@_values_hash, ::Hash.new)
  subklass.instance_variable_set(:@_names_hash, ::Hash.new)
  subklass.allow_nil = false
  subklass.raise_on_invalid = false
end

.key?(name) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/trax/core/primitives/enum.rb', line 95

def self.key?(name)
  _names_hash.key?(name)
end

.keysObject



91
92
93
# File 'lib/trax/core/primitives/enum.rb', line 91

def self.keys
  _names_hash.keys
end

.namesObject



99
100
101
# File 'lib/trax/core/primitives/enum.rb', line 99

def self.names
  _names_hash.values
end

.no_raise_mode?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/trax/core/primitives/enum.rb', line 103

def self.no_raise_mode?
  !raise_on_invalid
end

.select_values(*args) ⇒ Object



67
68
69
70
# File 'lib/trax/core/primitives/enum.rb', line 67

def self.select_values(*args)
  args.flat_compact_uniq!
  args.map{|arg| self[arg].to_i }
end

.to_schemaObject



132
133
134
135
136
137
138
139
140
# File 'lib/trax/core/primitives/enum.rb', line 132

def self.to_schema
  ::Trax::Core::Definition.new(
    :name => self.name.demodulize.underscore,
    :source => self.name,
    :type => :enum,
    :choices => choices.map(&:to_schema),
    :values => keys
  )
end

.typeObject



128
129
130
# File 'lib/trax/core/primitives/enum.rb', line 128

def self.type
  :enum
end

.valid_name?(val) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/trax/core/primitives/enum.rb', line 107

def self.valid_name?(val)
  _names_as_strings.include?(val)
end

.valid_value?(val) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/trax/core/primitives/enum.rb', line 111

def self.valid_value?(val)
  values.include?(val)
end

.value?(val) ⇒ Boolean

because calling valid_value? in the define_enum_value method is unclear

Returns:

  • (Boolean)


116
117
118
# File 'lib/trax/core/primitives/enum.rb', line 116

def self.value?(val)
  valid_value?(val)
end

.valuesObject



120
121
122
# File 'lib/trax/core/primitives/enum.rb', line 120

def self.values
  _names_hash.values.map(&:to_i)
end

Instance Method Details

#__getobj__Object



180
181
182
# File 'lib/trax/core/primitives/enum.rb', line 180

def __getobj__
  @choice || nil
end

#current_indexObject



165
166
167
# File 'lib/trax/core/primitives/enum.rb', line 165

def current_index
  self.class.names.index(choice)
end

#next_valueObject



184
185
186
187
# File 'lib/trax/core/primitives/enum.rb', line 184

def next_value
  return choice if self.class.names.length == current_index
  self.class.names[(current_index + 1)]
end

#next_value?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/trax/core/primitives/enum.rb', line 189

def next_value?
  !(current_index == (self.class.names.length - 1))
end

#previous_valueObject



204
205
206
# File 'lib/trax/core/primitives/enum.rb', line 204

def previous_value
  self.class.names[(current_index - 1)]
end

#previous_value?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/trax/core/primitives/enum.rb', line 208

def previous_value?
  !!current_index
end

#select_next_valueObject

set choice if next value exists, return selected choi



194
195
196
197
# File 'lib/trax/core/primitives/enum.rb', line 194

def select_next_value
  self.choice = next_value.to_sym if next_value?
  self
end

#select_previous_valueObject



199
200
201
202
# File 'lib/trax/core/primitives/enum.rb', line 199

def select_previous_value
  self.choice = previous_value.to_sym if previous_value?
  self
end

#to_sObject



212
213
214
# File 'lib/trax/core/primitives/enum.rb', line 212

def to_s
  choice.to_s
end

#valid_choice?(val) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/trax/core/primitives/enum.rb', line 216

def valid_choice?(val)
  self.class === val
end