Sha256: 2a9af04eaa008fd37adc8e109892be1113622fd70d825a1d510e1ee26e2dd7ce

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Bidu
  module Mercy
    class Report
      class Range < Report::ActiveRecord
        ALLOWED_PARAMETERS=[:period, :maximum, :minimum]
        DEFAULT_OPTION = {
          period: 1.day,
          scope: :all,
          minimum: nil,
          maximum: nil
        }

        json_parse :scope
        json_parse :minimum, :maximum, type: :integer

        def initialize(options)
          super(DEFAULT_OPTION.merge(options))
        end

        def scoped
          @scoped ||= fetch_scoped(last_entries, scope)
        end

        def error?
          @error ||= !count_in_range?
        end

        def as_json
          {
            status: status,
            count: count
          }
        end

        def count
          scoped.count
        end

        private

        def range
          (minimum..maximum)
        end

        def count_in_range?
          return range.include?(count) unless (maximum.nil? || minimum.nil?)
          return count >= minimum unless minimum.nil?
          return count <= maximum unless maximum.nil?
          true
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mercy-1.3.0 lib/bidu/mercy/report/range.rb
bidu-mercy-1.3.0 lib/bidu/mercy/report/range.rb