Sha256: 676553f1d217c3ff59faf0e6d99b6cb773dcc1a56955e9cd8bb9000b16febec7

Contents?: true

Size: 1.68 KB

Versions: 20

Compression:

Stored size: 1.68 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2020 Eric Crane.  All rights reserved.
#
# An object with a decimal value.
#

module Gloo
  module Objs
    class Decimal < Gloo::Core::Obj

      KEYWORD = 'decimal'.freeze
      KEYWORD_SHORT = 'num'.freeze

      #
      # The name of the object type.
      #
      def self.typename
        return KEYWORD
      end

      #
      # The short name of the object type.
      #
      def self.short_typename
        return KEYWORD_SHORT
      end

      #
      # Set the value with any necessary type conversions.
      #
      def set_value( new_value )
        if new_value.nil?
          self.value = 0.0
          return
        end

        unless new_value.is_a? Numeric
          self.value = @engine.converter.convert( new_value, 'Decimal', 0.0 )
          return
        end

        self.value = new_value.to_f
      end

      # ---------------------------------------------------------------------
      #    Messages
      # ---------------------------------------------------------------------

      #
      # Get a list of message names that this object receives.
      #
      def self.messages
        return super + %w[round]
      end

      #
      # Round the value to a whole value.
      # If a parameter is included in the message,
      # round to the precision given.
      #
      def msg_round
        data = 0
        if @params&.token_count&.positive?
          expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
          data = expr.evaluate.to_i
        end

        i = self.value.round( data )
        set_value i
        @engine.heap.it.set_to i
        return i
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
gloo-3.5.0 lib/gloo/objs/basic/decimal.rb
gloo-3.4.1 lib/gloo/objs/basic/decimal.rb
gloo-3.4.0 lib/gloo/objs/basic/decimal.rb
gloo-3.3.0 lib/gloo/objs/basic/decimal.rb
gloo-3.2.0 lib/gloo/objs/basic/decimal.rb
gloo-3.1.1 lib/gloo/objs/basic/decimal.rb
gloo-3.1.0 lib/gloo/objs/basic/decimal.rb
gloo-3.0.1 lib/gloo/objs/basic/decimal.rb
gloo-3.0.0 lib/gloo/objs/basic/decimal.rb
gloo-2.5.0 lib/gloo/objs/basic/decimal.rb
gloo-2.4.3 lib/gloo/objs/basic/decimal.rb
gloo-2.4.2 lib/gloo/objs/basic/decimal.rb
gloo-2.4.1 lib/gloo/objs/basic/decimal.rb
gloo-2.4.0 lib/gloo/objs/basic/decimal.rb
gloo-2.3.1 lib/gloo/objs/basic/decimal.rb
gloo-2.2.0 lib/gloo/objs/basic/decimal.rb
gloo-2.1.0 lib/gloo/objs/basic/decimal.rb
gloo-2.0.2 lib/gloo/objs/basic/decimal.rb
gloo-2.0.1 lib/gloo/objs/basic/decimal.rb
gloo-2.0.0 lib/gloo/objs/basic/decimal.rb