Sha256: 9a8189ac109824f3609f2d0b8ea89ace0b6812226cb4d998c4bee8c21db8a050

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 KB

Contents

module Hookit
  module Resource
    class HookFile < Base
      
      field :path
      field :source
      field :mode
      field :owner
      field :group

      actions :create, :create_if_missing, :delete, :touch
      default_action :create

      def initialize(name)
        path name unless path
        source ::File.basename(name)
        super
      end

      def run(action)
        case action
        when :create
          create!
          chown!
          chmod!            
        when :create_if_missing
          create_if_missing!
          chown!
          chmod!
        when :delete
          delete!
        when :touch
          touch!
        end
      end

      protected

      def create!
        ::File.write path, render
      end

      def create_if_missing!
        if not ::File.exists? path
          create!
        end
      end

      def delete!
        ::File.delete path
      end

      def chown!
        return unless owner or group
        `chown #{(group.nil?) ? owner : "#{owner}:#{group}"} #{path}`
      end

      def chmod!
        ::File.chmod(mode, path) if mode
      end

      def touch!
        `touch -c #{path}`
      end

      def render
        ::File.read("#{file_dir}/#{source}")
      end

      def file_dir
        "#{module_root}/files"
      end

      def module_root
        dict[:module_root]
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hookit-1.0.0 lib/hookit/resource/hook_file.rb
hookit-0.12.3 lib/hookit/resource/hook_file.rb
hookit-0.12.2 lib/hookit/resource/hook_file.rb
hookit-0.12.1 lib/hookit/resource/hook_file.rb
hookit-0.12.0 lib/hookit/resource/hook_file.rb
hookit-0.11.2 lib/hookit/resource/hook_file.rb
hookit-0.11.1 lib/hookit/resource/hook_file.rb
hookit-0.11.0 lib/hookit/resource/hook_file.rb
hookit-0.10.0 lib/hookit/resource/hook_file.rb
hookit-0.9.2 lib/hookit/resource/hook_file.rb
hookit-0.9.0 lib/hookit/resource/hook_file.rb
hookit-0.8.0 lib/hookit/resource/hook_file.rb
hookit-0.7.11 lib/hookit/resource/hook_file.rb