Sha256: ba0b60afa0048ac185e6fa3a643c2d521b0a42afb190f29bf7712d4adeb9d419

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

module ReportsKit
  class Order
    attr_accessor :relation, :field, :direction

    VALID_RELATIONS = %w(count dimension1 dimension2)
    VALID_FIELDS = [nil, 'label']
    VALID_DIRECTIONS = %w(asc desc)

    def initialize(relation, field, direction)
      self.relation = relation
      self.field = field
      self.direction = direction
    end

    def self.parse(string)
      string ||= ''
      field_expression, direction = string.to_s.split(/\s+/)
      relation, field = (field_expression || '').split('.')

      relation = relation.presence
      field = field.presence
      direction = direction.presence || 'asc'

      relation = relation.to_i if relation =~ /^\d+$/

      raise ArgumentError.new("Invalid relation: #{relation}") unless VALID_RELATIONS.include?(relation) || relation.is_a?(Fixnum)
      raise ArgumentError.new("Invalid field: #{field}") unless VALID_FIELDS.include?(field)
      raise ArgumentError.new("Invalid direction: #{direction}") unless VALID_DIRECTIONS.include?(direction)

      new(relation, field, direction)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
reports_kit-0.7.1 lib/reports_kit/order.rb
reports_kit-0.7.0 lib/reports_kit/order.rb
reports_kit-0.5.0 lib/reports_kit/order.rb
reports_kit-0.4.1 lib/reports_kit/order.rb
reports_kit-0.4.0 lib/reports_kit/order.rb
reports_kit-0.3.3 lib/reports_kit/order.rb
reports_kit-0.3.2 lib/reports_kit/order.rb
reports_kit-0.3.1 lib/reports_kit/order.rb
reports_kit-0.3.0 lib/reports_kit/order.rb
reports_kit-0.2.0 lib/reports_kit/order.rb