Sha256: 82dcd2f6e3e5ee2c74e8fdd8abb11513aa09565067d6bc04721f2929e4ece9b5

Contents?: true

Size: 1.72 KB

Versions: 29

Compression:

Stored size: 1.72 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Transforms
    module Transforms

      class CasingTransform < TransformRule
        VALID_FORMS = %w(lower upper title)

        class << self
          def accepts?(forward_form, backward_form)
            valid_form?(forward_form) && valid_form?(backward_form)
          end

          private

          def valid_form?(form)
            !form || form.null? || form.blank? || (
              VALID_FORMS.include?(form.transform.downcase)
            )
          end
        end

        attr_reader :forward_transform, :backward_transform

        def apply_to(cursor)
          if forward_transform
            case forward_transform
              when 'lower'
                apply_lower(cursor)
              when 'upper'
                apply_upper(cursor)
              when 'title'
                apply_title(cursor)
            end

            cursor.reset_position
          end
        end

        private

        def after_initialize
          @forward_transform = normalize_transform(forward_form)
          @backward_transform = normalize_transform(backward_form)
        end

        def normalize_transform(form)
          if form && form.has_transform?
            form.transform.downcase
          end
        end

        def apply_lower(cursor)
          cursor.set_text(caser.downcase(cursor.text))
        end

        def apply_upper(cursor)
          cursor.set_text(caser.upcase(cursor.text))
        end

        def apply_title(cursor)
          cursor.set_text(caser.titlecase(cursor.text))
        end

        def caser
          TwitterCldr::Shared::Caser
        end
      end

    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
twitter_cldr-6.13.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.12.1 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.12.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.11.5 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.11.4 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.11.3 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.11.2 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.11.1 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.11.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.10.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.9.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.8.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.7.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.6.2 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.6.1 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.6.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.5.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.4.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.3.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb
twitter_cldr-6.2.0 lib/twitter_cldr/transforms/transforms/casing_transform.rb