gdelugre / origami

Origami is a pure Ruby library to parse, modify and generate PDF documents.
GNU Lesser General Public License v3.0
326 stars 110 forks source link

Ruby 3.0 & origami (2.1.0): Origami::Date.now throws ArgumentError #80

Open UniqueTokens opened 3 years ago

UniqueTokens commented 3 years ago
>> require 'origami'
=> true
>> Origami::Date.now
Traceback (most recent call last):
        4: from (irb):2:in `<main>'
        3: from /home/user/.rvm/gems/ruby-3.0.0/gems/origami-2.1.0/lib/origami/string.rb:436:in `now'
        2: from /home/user/.rvm/gems/ruby-3.0.0/gems/origami-2.1.0/lib/origami/string.rb:436:in `new'
        1: from /home/user/.rvm/gems/ruby-3.0.0/gems/origami-2.1.0/lib/origami/string.rb:373:in `initialize'
ArgumentError (wrong number of arguments (given 1, expected 0; required keyword: year))
UniqueTokens commented 3 years ago

See https://rubyreferences.github.io/rubychanges/3.0.html#keyword-arguments-are-now-fully-separated-from-positional-arguments

**date monkey patch:

module Origami
  class Date < LiteralString
    def self.parse(str)
      #:nodoc:
      raise InvalidDateError, "Not a valid Date string" unless str =~ REGEXP_TOKEN

      date =
        {
          year: $~['year'].to_i
        }

      date[:month] = $~['month'].to_i if $~['month']
      date[:day] = $~['day'].to_i if $~['day']
      date[:hour] = $~['hour'].to_i if $~['hour']
      date[:min] = $~['min'].to_i if $~['min']
      date[:sec] = $~['sec'].to_i if $~['sec']

      if %w[+ -].include?($~['ut'])
        utc_offset = $~['ut_hour_off'].to_i * 3600 + $~['ut_min_off'].to_i * 60
        utc_offset = -utc_offset if $~['ut'] == '-'

        date[:utc_offset] = utc_offset
      end

      Origami::Date.new(**date)
    end

    def self.now
      now = Time.now.utc

      date =
        {
          year: now.strftime("%Y").to_i,
          month: now.strftime("%m").to_i,
          day: now.strftime("%d").to_i,
          hour: now.strftime("%H").to_i,
          min: now.strftime("%M").to_i,
          sec: now.strftime("%S").to_i,
          utc_offset: now.utc_offset
        }

      Origami::Date.new(**date)
    end
  end
end