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.10.0 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.9.3 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.9.2 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.9.1 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.9.0 lib/jsonapionify/structure/helpers/member_names.rb
jsonapionify-0.0.1.pre lib/jsonapionify/structure/helpers/member_names.rb