Sha256: 222abc2479983dadbaf60d95cf33862f9c14d9167ba97f2fec809dbc91416d05

Contents?: true

Size: 809 Bytes

Versions: 8

Compression:

Stored size: 809 Bytes

Contents

module Groupdate
  module QueryMethods
    Groupdate::PERIODS.each do |period|
      define_method :"group_by_#{period}" do |field, time_zone = nil, range = nil, **options|
        Groupdate::Magic::Relation.generate_relation(self,
          period: period,
          field: field,
          time_zone: time_zone,
          range: range,
          **options
        )
      end
    end

    def group_by_period(period, field, permit: nil, **options)
      # to_sym is unsafe on user input, so convert to strings
      permitted_periods = ((permit || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s)
      if permitted_periods.include?(period.to_s)
        send("group_by_#{period}", field, **options)
      else
        raise ArgumentError, "Unpermitted period"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
groupdate2-4.1.5 lib/groupdate/query_methods.rb
groupdate-4.2.0 lib/groupdate/query_methods.rb
groupdate-4.1.2 lib/groupdate/query_methods.rb
groupdate-4.1.1 lib/groupdate/query_methods.rb
groupdate-4.1.0 lib/groupdate/query_methods.rb
groupdate-4.0.2 lib/groupdate/query_methods.rb
groupdate-4.0.1 lib/groupdate/query_methods.rb
groupdate-4.0.0 lib/groupdate/query_methods.rb