Class: EnumeratedAttribute::Attribute::AttributeDescriptor

Inherits:
Array
  • Object
show all
Defined in:
lib/enumerated_attribute/attribute/attribute_descriptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, enums = [], opts = {}) ⇒ AttributeDescriptor

Returns a new instance of AttributeDescriptor.



8
9
10
11
12
13
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 8

def initialize(name, enums=[], opts={})
	super enums
	@name = name
	@options = opts
	@labels_hash = Hash[*self.collect{|e| [e, e.to_s.gsub(/_/, ' ').squeeze(' ').capitalize]}.flatten]
end

Instance Attribute Details

#init_valueObject

Returns the value of attribute init_value.



6
7
8
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 6

def init_value
  @init_value
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 5

def name
  @name
end

Instance Method Details

#allows_nil?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 15

def allows_nil?
	@options.key?(:nil) ? @options[:nil] : true
end

#allows_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 18

def allows_value?(value)
	self.include?(value.to_sym)
end

#enumsObject



22
23
24
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 22

def enums
	self
end

#hashObject



31
32
33
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 31

def hash
	@labels_hash
end

#label(value) ⇒ Object



25
26
27
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 25

def label(value)
	@labels_hash[value]
end

#labelsObject



28
29
30
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 28

def labels
	@labels_array ||= self.map{|e| @labels_hash[e]}
end

#select_optionsObject



34
35
36
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 34

def select_options
	@select_options ||= self.map{|e| [@labels_hash[e], e.to_s]}
end

#set_label(enum_value, label_string) ⇒ Object



38
39
40
41
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 38

def set_label(enum_value, label_string)
	reset_labels
	@labels_hash[enum_value.to_sym] = label_string
end