Sha256: afcb28dcae68790f7255103158288510016a794a13c4b6e40f2af91bd3886cf8

Contents?: true

Size: 988 Bytes

Versions: 2

Compression:

Stored size: 988 Bytes

Contents

module Decanter
  module Parser
    module Core

      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods

        # Check if allowed, parse if not
        def parse(name, values, options={})
          case
          when allowed?(values)
            { name => values }
          else
            _parse(name, values, options)
          end
        end

        # Define parser
        def parser(&block)
          @parser = block
        end

        # Set allowed classes
        def allow(*args)
          @allowed = args
        end

        # Set preparsers
        def pre(*parsers)
          @pre = parsers
        end

        # Get prepareer
        def preparsers
          @pre || []
        end

        # Check for allowed classes
        def allowed?(values)
          @allowed && Array.wrap(values).all? do |value|
            @allowed.any? { |allowed| value.is_a? allowed }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decanter-0.8.1 lib/decanter/parser/core.rb
decanter-0.8.0 lib/decanter/parser/core.rb