Module: EnumeratedAttribute::Attribute::Arguments

Defined in:
lib/enumerated_attribute/attribute/arguments.rb

Class Method Summary collapse

Class Method Details

.init_incrementor_decrementor_method_names(config) ⇒ Object



7
8
9
10
# File 'lib/enumerated_attribute/attribute/arguments.rb', line 7

def self.init_incrementor_decrementor_method_names(config)
				config.incrementor = config.opts[:incrementor] || config.opts[:inc] || "#{config.attr_name}_next"
				config.decrementor = config.opts[:decrementor] || config.opts[:dec] || "#{config.attr_name}_previous"
end

.init_plural_name(config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/enumerated_attribute/attribute/arguments.rb', line 11

def self.init_plural_name(config)
				config.plural_name = config.opts[:plural] || config.opts[:enums_accessor] || config.opts[:enums] || begin
    case
					when config.attr_name =~ /[aeiou]y$/
"#{config.attr_name}s"
					when config.attr_name =~ /y$/
config.attr_name.sub(/y$/, 'ies')
					when config.attr_name =~ /(sh|ch|x|s)$/
"#{config.attr_name}es"
					else
"#{config.attr_name}s"
					end
				end
end

.parse_enum_attr_arguments(args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/enumerated_attribute/attribute/arguments.rb', line 30

def self.parse_enum_attr_arguments(args)
  config = OpenStruct.new
  config.attr_name = args[0].to_s
  config.attr_symbol = config.attr_name.to_sym
  config.enums = (args[1] && args[1].is_a?(Array) ? args[1] : [])
  index = config.enums.empty? ? 1 : 2
  config.opts = (args[index] && args[index].is_a?(Hash) ? args[index] : {})

  validate_enum_attr_arguments(config)
  init_plural_name(config)
  init_incrementor_decrementor_method_names(config)
  process_enums_for_initial_value(config)

  config
end

.process_enums_for_initial_value(config) ⇒ Object



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

def self.process_enums_for_initial_value(config)
  config.initial_value = nil
				config.enums = config.enums.map{|v| (v =~ /^\^/ ? (config.initial_value ||= v[1, v.length-1].to_sym) : v.to_sym )}
end

.validate_enum_attr_arguments(config) ⇒ Object

Raises:



4
5
6
# File 'lib/enumerated_attribute/attribute/arguments.rb', line 4

def self.validate_enum_attr_arguments(config)
				raise(InvalidDefinition, 'second argument of enumerated_attribute/enum_attr is not an array of symbols or strings representing the enum values', caller) if config.enums.empty?
end