Module: Rake::Opt::KeywordArgs::InstanceMethods

Defined in:
lib/rake/opt/keyword_args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arg_optionsObject

Returns the value of attribute arg_options.



90
91
92
# File 'lib/rake/opt/keyword_args.rb', line 90

def arg_options
  @arg_options
end

Instance Method Details

#arg_hash_to_str(hash) ⇒ Object



151
152
153
154
155
# File 'lib/rake/opt/keyword_args.rb', line 151

def arg_hash_to_str(hash)
  args = ''
  hash.each{ |k, v| args << "#{k}=#{v} " }
  return args.strip
end

#parse_args(args) ⇒ Object

Raises:



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rake/opt/keyword_args.rb', line 92

def parse_args(args)
  raise Error, 'Expected only one argument'  if args.count > 1
  args = (args.first && args.first.last) ? args.first.last : ''
  parsed_args = Hashie::Mash.new
  args.split(PARAM_SEPARATOR).each do |pair|
    arg, value = pair.split('=', 2)
    parsed_args[arg] = value.blank? ? nil : value
  end

  return parsed_args
end

#verify_and_merge_args(args) ⇒ Object



146
147
148
149
# File 'lib/rake/opt/keyword_args.rb', line 146

def verify_and_merge_args(args)
  verified = verify_args(args)
  return self.parse_args(args).merge(verified)
end

#verify_args(args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rake/opt/keyword_args.rb', line 104

def verify_args(args)
  return args unless self.arg_options

  parsed_args = self.parse_args(args)
  output = Hashie::Mash.new
  self.arg_options.each do |arg_name, options|
    # Extract Value if it's set, otherwise, use the default
    if parsed_args[arg_name]
      value = parsed_args[arg_name]
    else
      value = options[:default]
    end

    # Cast the argument to the proper type
    if value.nil?
    # Keep it as nil
    elsif [Fixnum, Integer].include? options[:class]
      value = value.to_i
    elsif Float == options[:class]
      value = value.to_f
    elsif Symbol == options[:class]
      value = value.to_sym
    elsif Rational == options[:class]
      value = value.to_r
    elsif [String, nil].include? options[:class]
      value = value.to_s
    elsif Boolean == options[:class]
      value = %w{ 1 true yes t }.include? value.to_s.downcase
    else
      raise UnsupportedTypeError, "Arguments of type #{options[:class]} are not supported"
    end

    # Set the output value
    output[arg_name] = value

    if options[:required] && output[arg_name].nil? && !Rake.application.options.dryrun
      raise ValidationError, "Argument :#{arg_name} is required for Task #{self.name}"
    end
  end
  return output
end