Sha256: 5f2bc361257e90c7d065fc7c5441240bc969b79469c8b4e94a83c8732e76d5e7

Contents?: true

Size: 1.29 KB

Versions: 31

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Phlexi
  module Form
    module Options
      module Length
        def minlength(minlength = nil)
          if minlength.nil?
            options.fetch(:minlength) { options[:minlength] = calculate_minlength }
          else
            options[:minlength] = minlength
            self
          end
        end

        def maxlength(maxlength = nil)
          if maxlength.nil?
            options.fetch(:maxlength) { options[:maxlength] = calculate_maxlength }
          else
            options[:maxlength] = maxlength
            self
          end
        end

        private

        def calculate_minlength
          minimum_length_value_from(find_length_validator)
        end

        def minimum_length_value_from(length_validator)
          if length_validator
            length_validator.options[:is] || length_validator.options[:minimum]
          end
        end

        def calculate_maxlength
          maximum_length_value_from(find_length_validator)
        end

        def maximum_length_value_from(length_validator)
          if length_validator
            length_validator.options[:is] || length_validator.options[:maximum]
          end
        end

        def find_length_validator
          find_validator(:length)
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
phlexi-form-0.8.3 lib/phlexi/form/options/length.rb
phlexi-form-0.8.2 lib/phlexi/form/options/length.rb
phlexi-form-0.8.1 lib/phlexi/form/options/length.rb
phlexi-form-0.8.0 lib/phlexi/form/options/length.rb
phlexi-form-0.7.2 lib/phlexi/form/options/length.rb
phlexi-form-0.7.1 lib/phlexi/form/options/length.rb
phlexi-form-0.7.0 lib/phlexi/form/options/length.rb
phlexi-form-0.6.2 lib/phlexi/form/options/length.rb
phlexi-form-0.6.1 lib/phlexi/form/options/length.rb
phlexi-form-0.6.0 lib/phlexi/form/options/length.rb
phlexi-form-0.5.11 lib/phlexi/form/options/length.rb
phlexi-form-0.5.10 lib/phlexi/form/options/length.rb
phlexi-form-0.5.9 lib/phlexi/form/options/length.rb
phlexi-form-0.5.8 lib/phlexi/form/options/length.rb
phlexi-form-0.5.7 lib/phlexi/form/options/length.rb
phlexi-form-0.5.6 lib/phlexi/form/options/length.rb
phlexi-form-0.5.5 lib/phlexi/form/options/length.rb
phlexi-form-0.5.4 lib/phlexi/form/options/length.rb
phlexi-form-0.5.3 lib/phlexi/form/options/length.rb
phlexi-form-0.5.2 lib/phlexi/form/options/length.rb