Class: EnumeratedAttribute::Attribute::AttributeDescriptor
- Inherits:
-
Array
- Object
- Array
- EnumeratedAttribute::Attribute::AttributeDescriptor
- Defined in:
- lib/enumerated_attribute/attribute/attribute_descriptor.rb
Instance Attribute Summary collapse
-
#init_value ⇒ Object
Returns the value of attribute init_value.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #allows_nil? ⇒ Boolean
- #allows_value?(value) ⇒ Boolean
- #enums ⇒ Object
- #hash ⇒ Object
-
#initialize(name, enums = [], opts = {}) ⇒ AttributeDescriptor
constructor
A new instance of AttributeDescriptor.
- #label(value) ⇒ Object
- #labels ⇒ Object
- #select_options ⇒ Object
- #set_label(enum_value, label_string) ⇒ Object
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_value ⇒ Object
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 |
#name ⇒ Object (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
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
18 19 20 |
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 18 def allows_value?(value) self.include?(value.to_sym) end |
#enums ⇒ Object
22 23 24 |
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 22 def enums self end |
#hash ⇒ Object
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 |
#labels ⇒ Object
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_options ⇒ Object
34 35 36 |
# File 'lib/enumerated_attribute/attribute/attribute_descriptor.rb', line 34 def @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 |