Sha256: a096c754c60c16614148e17116fa33e1592089e673b42fcbdeedcf8b6720eb62

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module MinceMigrator
  module Migrations
    class Name
      attr_reader :value

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

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

      def value=(val)
        normalized_string = normalized_string(val)
        @value = capitalized_phrase(normalized_string)
      end

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

      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

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

      def reasons_for_failure
        @errors.join(" ")
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mince_migrator-1.0.2 lib/mince_migrator/migrations/name.rb
mince_migrator-1.0.1 lib/mince_migrator/migrations/name.rb
mince_migrator-1.0.0 lib/mince_migrator/migrations/name.rb