Sha256: 48693c7a8327425b34919e66ee250602aeb2e325920adb952b09f6730140d0f8

Contents?: true

Size: 887 Bytes

Versions: 26

Compression:

Stored size: 887 Bytes

Contents

module JSONAPIonify::Structure
  module Helpers
    module MemberNames
      extend self

      def valid?(value)
        return false if value.nil?
        value = value.to_s if value.is_a? Symbol
        [
          value.present?,
          valid_ends?(value),
          contains_valid_characters?(value),
          !contains_invalid_characters?(value)
        ].reduce(:&)
      end

      private

      def contains_valid_characters?(value)
        value =~ /\A[a-zA-Z0-9\u0080-\uFFFF_\s-]+\Z/
      end

      def valid_ends?(value)
        ['-', '_', ' '].map do |char|
          !value.start_with?(char) & !value.end_with?(char)
        end.reduce(:&)
      end

      def contains_invalid_characters?(value)
        %w{+ , . [ ] ! " # $ % & ' ( ) * / : ; < = > ? @ \\ ^ ` { } | ~}.map do |char|
          value.include? char
        end.reduce(:|)
      end

    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.9 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.8 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.7 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.5 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.4 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.3 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.2 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.1 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.12.0 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.11 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.10 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.9 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.8 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.7 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.6 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.5 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.11.0 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.10.2 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.10.1 lib/jsonapionify/structure/helpers/member_names.rb