Sha256: 8bf9afb2aecfdb45d22c8da7e65b7d086aa6bc603f54de63f75f3c3357cddeee

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

require_relative '../function'

module Dentaku
  module AST
    class Duration < Function
      def self.min_param_count
        1
      end

      def self.max_param_count
        1
      end

      class Value
        attr_reader :value, :unit

        def initialize(value, unit)
          @value = value
          @unit = validate_unit(unit)
        end

        def validate_unit(unit)
          case unit.downcase
          when /years?/ then :year
          when /months?/ then :month
          when /days?/ then :day
          else
            raise Dentaku::ArgumentError.for(:incompatible_type, value: unit, for: Duration),
              "'#{unit || unit.class}' is not a valid duration unit"
          end
        end
      end

      def type
        :duration
      end

      def value(context = {})
        value_node, unit_node = *@args
        Value.new(value_node.value(context), unit_node.identifier)
      end

      def dependencies(context = {})
        value_node = @args.first
        value_node.dependencies(context)
      end
    end
  end
end

Dentaku::AST::Function.register_class(:duration, Dentaku::AST::Duration)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dentaku-3.4.2 lib/dentaku/ast/functions/duration.rb
dentaku-3.4.1 lib/dentaku/ast/functions/duration.rb
dentaku-3.4.0 lib/dentaku/ast/functions/duration.rb
dentaku-3.3.4 lib/dentaku/ast/functions/duration.rb
dentaku-3.3.3 lib/dentaku/ast/functions/duration.rb