Class: MinceMigrator::Migrations::Name
- Inherits:
-
Object
- Object
- MinceMigrator::Migrations::Name
- Defined in:
- lib/mince_migrator/migrations/name.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #capitalized_phrase(val) ⇒ Object
- #filename ⇒ Object
-
#initialize(value) ⇒ Name
constructor
A new instance of Name.
- #normalized_string(val) ⇒ Object
- #reasons_for_failure ⇒ Object
- #valid? ⇒ Boolean
- #validate_value ⇒ Object
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
#value ⇒ Object
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 |
#filename ⇒ Object
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_failure ⇒ Object
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_value ⇒ Object
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 |