Sha256: 5a9cb0c1fa02d11d40ce7be832ec3bfc85b0cede842fceaf8cba9d2eb8fdb81c

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module Braintree
  class AdvancedSearch
    class SearchNode
      def self.operators(*operator_names)
        operator_names.each do |operator|
          define_method(operator) do |value|
            @parent.add_criteria(@node_name, operator => value.to_s)
          end
        end
      end

      def initialize(name, parent)
        @node_name, @parent = name, parent
      end
    end

    class TextNode < SearchNode
      operators :is, :is_not, :ends_with, :starts_with, :contains
    end

    class MultipleValueNode < SearchNode
      def in(*values)
        values.flatten!

        unless allowed_values.nil?
          bad_values = values - allowed_values
          raise ArgumentError.new("Invalid argument(s) for #{@node_name}: #{bad_values.join(", ")}") if bad_values.any?
        end

        @parent.add_criteria(@node_name, values)
      end

      def initialize(name, parent, options)
        super(name, parent)
        @options = options
      end

      def allowed_values
        @options[:allows]
      end
    end

    def self.search_fields(*fields)
      fields.each do |field|
        define_method(field) do
          TextNode.new(field, self)
        end
      end
    end

    def self.multiple_value_field(field, options={})
      define_method(field) do
        MultipleValueNode.new(field, self, options)
      end
    end

    def initialize
      @criteria = {}
    end

    def add_criteria(key, value)
      @criteria[key] = value
    end

    def to_hash
      @criteria
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
braintree-2.0.0 lib/braintree/advanced_search.rb
braintree-1.2.1 lib/braintree/advanced_search.rb
braintree-1.2.0 lib/braintree/advanced_search.rb