Sha256: af622a63c53b172cddadeb7625000568b18b932d416dbdb4f0247d003d09e0dd

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Enumerable
  Groupdate::PERIODS.each do |period|
    define_method :"group_by_#{period}" do |*args, **options, &block|
      if block
        # TODO throw error in Groupdate 5
        warn "[groupdate] positional arguments are deprecated" if args.any?
        Groupdate::Magic::Enumerable.group_by(self, period, (args[0] || {}).merge(options), &block)
      elsif respond_to?(:scoping)
        scoping { @klass.send(:"group_by_#{period}", *args, **options, &block) }
      else
        raise ArgumentError, "no block given"
      end
    end
  end

  def group_by_period(period, *args, **options, &block)
    if block || !respond_to?(:scoping)
      # TODO throw error in Groupdate 5
      warn "[groupdate] positional arguments are deprecated" if args.any?
      options = (args[0] || {}).merge(options)
      # to_sym is unsafe on user input, so convert to strings
      permitted_periods = ((options.delete(:permit) || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s)
      if permitted_periods.include?(period.to_s)
        send("group_by_#{period}", **options, &block)
      else
        raise ArgumentError, "Unpermitted period"
      end
    else
      scoping { @klass.send(:group_by_period, period, *args, **options, &block) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
groupdate-4.3.0 lib/groupdate/enumerable.rb