Sha256: 442b04c3bc71acaa7b64d56bf769e65129bd1c85cd3515aa683e2a689753815b

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 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 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 dst
        o = pn.resolve
        $engine.err( "#{MISSING_DST_OBJ_ERR} '#{dst}'" ) unless o
        return o
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gloo-0.8.0 lib/gloo/verbs/move.rb
gloo-0.7.7 lib/gloo/verbs/move.rb
gloo-0.7.6 lib/gloo/verbs/move.rb
gloo-0.7.5 lib/gloo/verbs/move.rb
gloo-0.7.4 lib/gloo/verbs/move.rb
gloo-0.7.3 lib/gloo/verbs/move.rb
gloo-0.7.2 lib/gloo/verbs/move.rb
gloo-0.7.1 lib/gloo/verbs/move.rb
gloo-0.7.0 lib/gloo/verbs/move.rb