Sha256: 2ae01cdc299d296aa9bc5c13e172e2f2832d9dab67675a5ce958652efa205214

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Bidu
  module House
    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 & 1 rubygems

Version Path
bidu-house-1.3.0 lib/bidu/house/report/range.rb
bidu-house-1.2.0 lib/bidu/house/report/range.rb