Class: MinceMigrator::Migrations::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/mince_migrator/migrations/name.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Name



6
7
8
9
# File 'lib/mince_migrator/migrations/name.rb', line 6

def initialize(value)
  self.value = value if value
  @errors = []
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/mince_migrator/migrations/name.rb', line 4

def value
  @value
end

Instance Method Details

#capitalized_phrase(val) ⇒ Object



20
21
22
23
24
# File 'lib/mince_migrator/migrations/name.rb', line 20

def capitalized_phrase(val)
  val.split(' ').each_with_index.map do |word, i|
    i == 0 ? word.capitalize : word.downcase
  end.join(" ")
end

#filenameObject



11
12
13
# File 'lib/mince_migrator/migrations/name.rb', line 11

def filename
  @filename ||= "#{value.downcase.gsub(" ", "_")}.rb"
end

#normalized_string(val) ⇒ Object



26
27
28
29
30
# File 'lib/mince_migrator/migrations/name.rb', line 26

def normalized_string(val)
  val = val.gsub(/[-_]/, ' ') # convert dashes and underscores to spaces
  pattern = /(^[0-9]|[^a-zA-Z0-9\s])/ # only allow letters and numbers, but do not allow numbers at the beginning
  val.gsub(pattern, '')
end

#reasons_for_failureObject



38
39
40
# File 'lib/mince_migrator/migrations/name.rb', line 38

def reasons_for_failure
  @errors.join(" ")
end

#valid?Boolean



32
33
34
35
36
# File 'lib/mince_migrator/migrations/name.rb', line 32

def valid?
  @errors = []
  validate_value
  @errors.empty?
end

#validate_valueObject



42
43
44
# File 'lib/mince_migrator/migrations/name.rb', line 42

def validate_value
  @errors << "Name is invalid, it must start with a character from A-Z or a-z" if value.nil? || value == ''
end