Sha256: a7ff77227694c24043143a0a148510928d4deb4649ac9b8afd253d4b87ae8216

Contents?: true

Size: 2 KB

Versions: 20

Compression:

Stored size: 2 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2020 Eric Crane.  All rights reserved.
#
# Move an object to a new parent.
#

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

      KEYWORD = 'move'.freeze
      KEYWORD_SHORT = 'mv'.freeze
      TO = 'to'.freeze
      MISSING_SRC_ERR = 'Object to move was not specified!'.freeze
      MISSING_SRC_OBJ_ERR = 'Could not find object to move: '.freeze
      MISSING_DST_ERR = "Move' must include 'to' parent object!".freeze
      MISSING_DST_OBJ_ERR = 'Could not resolve target: '.freeze

      #
      # Run the verb.
      #
      def run
        dst = lookup_dst
        return if dst.nil?

        o = lookup_obj
        return unless o

        o.parent.remove_child o
        dst.add_child o
      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

      #
      # Lookup the object that we're moving.
      #
      def lookup_obj
        arr = @tokens.before_token( TO )
        if arr.count == 1
          @engine.err MISSING_SRC_ERR
          return
        end

        name = arr[ 1 ]
        pn = Gloo::Core::Pn.new( @engine, name )
        o = pn.resolve

        @engine.err( "#{MISSING_SRC_OBJ_ERR} #{name}" ) unless o
        return o
      end

      #
      # Lookup destination, the new parent object.
      #
      def lookup_dst
        dst = @tokens.after_token( TO )
        unless dst
          @engine.err MISSING_DST_ERR
          return nil
        end

        pn = Gloo::Core::Pn.new( @engine, dst )
        o = pn.resolve
        @engine.err( "#{MISSING_DST_OBJ_ERR} '#{dst}'" ) unless o
        return o
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

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