Module: AWS::Core::OptionGrammar::ModuleMethods

Includes:
Inflection
Included in:
AWS::Core::OptionGrammar
Defined in:
lib/aws/core/option_grammar.rb

Instance Method Summary collapse

Methods included from Inflection

class_name, ruby_name

Instance Method Details

#customize(config = []) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/aws/core/option_grammar.rb', line 506

def customize(config = [])
  m = Class.new(self)
  supported_options = m.supported_options.inject({}) do |memo, opt|
    memo[opt.name] = opt
    memo
  end
  config.each do |option_config|
    if config.kind_of?(Hash)
      (name, value_desc) = option_config
    else
      (name, value_desc) = parse_option(option_config)
    end
    option = supported_options[name] || DefaultOption.new(name)
    option = option.extend_with_config(*value_desc)
    supported_options[option.name] = option
  end
  
  supported_ary = supported_options.values
  MetaUtils.extend_method(m, :supported_options) { supported_ary }
  supported_ruby_names = supported_ary.inject({}) do |memo, opt|
    memo[opt.ruby_name] = opt
    memo
  end
  MetaUtils.extend_method(m, :option) { |n| supported_ruby_names[n] }
  supported_ary.each do |opt|
    MetaUtils.extend_method(m, "validate_#{opt.ruby_name}") do |value|
      opt.validate(value)
    end
  end
  
  m
end

#included(m) ⇒ Object



586
587
588
# File 'lib/aws/core/option_grammar.rb', line 586

def included(m)
  m.extend(self::ModuleMethods)
end

#option(name) ⇒ Object



539
540
541
# File 'lib/aws/core/option_grammar.rb', line 539

def option(name)
  nil
end

#request_params(options) ⇒ Object

Returns the options in AWS/Query format



562
563
564
565
566
567
568
# File 'lib/aws/core/option_grammar.rb', line 562

def request_params(options)
  validate(options)
  options.map do |(name, value)|
    name = name.to_s
    option(name).request_params(value)
  end.flatten
end

#supported_optionsObject



543
544
545
# File 'lib/aws/core/option_grammar.rb', line 543

def supported_options
  []
end

#to_h(options) ⇒ Object

Returns the options as a hash (which is used to generate JSON in to_json).



572
573
574
575
576
577
578
579
# File 'lib/aws/core/option_grammar.rb', line 572

def to_h(options)
  validate(options)
  options.inject({}) do |hash, (name, value)|
    option = self.option(name.to_s)
    hash[option.name] = option.hash_format(value)
    hash
  end
end

#to_json(options) ⇒ Object

Returns the options in JSON format



582
583
584
# File 'lib/aws/core/option_grammar.rb', line 582

def to_json(options)
  to_h(options).to_json
end

#validate(options) ⇒ Object



547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/aws/core/option_grammar.rb', line 547

def validate(options)
  options.each do |name, value|
    name = name.to_s
    raise ArgumentError.new("unexpected option #{name}") unless
      option(name)
    option(name).validate(value)
  end
  supported_options.each do |option|
    raise ArgumentError.new("missing required option #{option.ruby_name}") unless
      !option.required? ||
      options.has_key?(option.ruby_name) || options.has_key?(option.ruby_name.to_sym)
  end
end