Sha256: f0c0e5ba3d61e78cc9fefe39cff9fe300bfd86b035bed96f17dbd0736b910236

Contents?: true

Size: 1.63 KB

Versions: 7

Compression:

Stored size: 1.63 KB

Contents

class Lxc
  module Helpers

    module Options
      class << self
        def included(klass)
          klass.class_eval do
            class << self
              attr_reader :options
              
              def option(name, short, type, args={})
                @options ||= {}
                @options[name] = args.merge(:short => short, :type => type)
                instance_eval do
                  attr_accessor name.to_sym
                end
              end
            end
          end
        end
      end

      private

      def configure!(args)
        self.class.options.each do |name, opts|
          argv = args.detect{|k,v| (Array(opts[:aliases]) + Array(opts[:short]) + [name]).include?(k.to_sym)}
          argv = argv.last if argv
          argv ||= opts[:default]
          if(argv)
            check_type!(name, argv, opts[:type])
            self.send("#{name}=", argv)
          else
            if(opts[:required])
              raise ArgumentError.new "Missing required argument: #{name}"
            end
          end
        end
        if(ipaddress && gateway.nil?)
          self.gateway = ipaddress.sub(%r{\d+$}, '1')
        end
      end

      def check_type!(arg_name, val, type)
        valid = false
        case type
        when :string
          valid = val.is_a?(String)
        when :boolean
          valid = val.is_a?(TrueClass) || val.is_a?(FalseClass)
        when :integer
          valid = val.is_a?(Numeric)
        end
        raise ArgumentError.new "Invalid type provided for #{arg_name}. Expecting value type of: #{type.inspect} Got: #{val.class} -  #{val}" unless valid
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
elecksee-1.0.22 lib/elecksee/helpers/options.rb
elecksee-1.0.20 lib/elecksee/helpers/options.rb
elecksee-1.0.18 lib/elecksee/helpers/options.rb
elecksee-1.0.16 lib/elecksee/helpers/options.rb
elecksee-1.0.14 lib/elecksee/helpers/options.rb
elecksee-1.0.12 lib/elecksee/helpers/options.rb
elecksee-1.0.10 lib/elecksee/helpers/options.rb