Sha256: 4a522fbe38c202f4a711ed747659973e08f39601cb8f247e28b365f49e3269ca

Contents?: true

Size: 1.97 KB

Versions: 20

Compression:

Stored size: 1.97 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2019 Eric Crane.  All rights reserved.
#
# If something is true, do something.
#

module Gloo
  module Verbs
    class If < Gloo::Core::Verb

      KEYWORD = 'if'.freeze
      KEYWORD_SHORT = 'if'.freeze
      THEN = 'then'.freeze
      MISSING_EXPR_ERR = 'Missing Expression!'.freeze

      #
      # Run the verb.
      #
      def run
        value = value_tokens
        return if value.nil?

        return unless evals_true( value )

        run_then
      end

      #
      # Get the Verb's keyword.
      #
      def self.keyword
        return KEYWORD
      end

      #
      # Get the Verb's keyword shortcut.
      #
      def self.keyword_shortcut
        return KEYWORD_SHORT
      end

      # ---------------------------------------------------------------------
      #    Private functions
      # ---------------------------------------------------------------------

      private

      #
      # Get the list of tokens that represent the parameters
      # of the if command.
      #
      def value_tokens
        value = @tokens.before_token( THEN )
        if value && value.count > 1
          # The first token is the verb, so we drop it.
          value = value[ 1..-1 ]
        else
          @engine.err MISSING_EXPR_ERR
        end

        return value
      end

      #
      # Does the given value evalute to true?
      #
      def evals_true( value )
        eval_result = false
        if value.count.positive?
          expr = Gloo::Expr::Expression.new( @engine, value )
          result = expr.evaluate
          eval_result = true if result == true
          eval_result = true if result.is_a?( Numeric ) && result != 0
        end

        return eval_result
      end

      #
      # Run the 'then' command.
      #
      def run_then
        cmd = @tokens.expr_after( THEN )
        i = @engine.parser.parse_immediate cmd
        return unless i

        i.run
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

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