Sha256: 84a7cd039ce316a4ecc4e891079414a11cd62d1fea1999618cd1cf176c06a235

Contents?: true

Size: 1011 Bytes

Versions: 9

Compression:

Stored size: 1011 Bytes

Contents

module EnumeratedAttribute
	module Attribute
		class AttributeDescriptor < Array		
			attr_reader :name
			attr_accessor :init_value
			
			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
			
			def allows_nil?
				@options.key?(:nil) ? @options[:nil] : true
			end
			def allows_value?(value)
				self.include?(value.to_sym)
			end
				
			def enums
				self
			end
			def label(value)
				@labels_hash[value]
			end
			def labels
				@labels_array ||= self.map{|e| @labels_hash[e]}
			end
			def hash
				@labels_hash
			end
			def select_options
				@select_options ||= self.map{|e| [@labels_hash[e], e.to_s]}
			end
			
			def set_label(enum_value, label_string)
				reset_labels
				@labels_hash[enum_value.to_sym] = label_string
			end
			
			protected
			def reset_labels
				@labels_array = nil
				@select_options = nil
			end
			
		end
	end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
jeffp-enumerated_attribute-0.2.3 lib/enumerated_attribute/attribute/attribute_descriptor.rb
edave-enumerated_attribute-0.2.18 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.16 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.13 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.12 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.11 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.10 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.8 lib/enumerated_attribute/attribute/attribute_descriptor.rb
enumerated_attribute-0.2.7 lib/enumerated_attribute/attribute/attribute_descriptor.rb