Sha256: 9b20d95883ec39f071c9b7199c331f92a413bdcabd35812c350f532765e9106e

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

module Admino
  module Query
    class Configuration
      class Field
        attr_reader :name
        attr_reader :coerce_to

        def initialize(name, options = {})
          options.symbolize_keys!
          options.assert_valid_keys(:coerce)

          @name = name.to_sym

          if coerce_to = options[:coerce]
            @coerce_to = coerce_to.to_sym
          end
        end
      end

      class Group
        attr_reader :name
        attr_reader :scopes

        def initialize(name, scopes)
          @name = name.to_sym
          @scopes = scopes.map(&:to_sym)
        end
      end

      class Sorting
        attr_reader :scopes
        attr_reader :default_scope
        attr_reader :default_direction

        def initialize(scopes, options = {})
          options.symbolize_keys!
          options.assert_valid_keys(:default_scope, :default_direction)

          @scopes = scopes.map(&:to_sym)
          @default_scope = if options[:default_scope]
                             options[:default_scope].to_sym
                           end

          @default_direction = if options[:default_direction]
                                 options[:default_direction].to_sym
                               end
        end
      end

      attr_reader :fields
      attr_reader :groups
      attr_reader :sorting
      attr_accessor :starting_scope_callable
      attr_accessor :ending_scope_callable

      def initialize
        @fields = []
        @groups = []
      end

      def add_field(name, options = {})
        Field.new(name, options).tap do |field|
          self.fields << field
        end
      end

      def add_group(name, scopes)
        Group.new(name, scopes).tap do |group|
          self.groups << group
        end
      end

      def add_sorting_scopes(scopes, options = {})
        @sorting = Sorting.new(scopes, options)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
admino-0.0.3 lib/admino/query/configuration.rb
admino-0.0.2 lib/admino/query/configuration.rb