class Seahorse::Client::ParamConverter

Public Class Methods

new(shape) click to toggle source

@param [Model::Shapes::Shape] shape

# File lib/seahorse/client/param_converter.rb, line 15
def initialize(shape)
  @shape = shape
end

Private Class Methods

add(shape_class, value_class, converter = nil, &block) click to toggle source

Registers a new value converter. Converters run in the context of a shape and value class.

# add a converter that stringifies integers
shape_class = Seahorse::Model::Shapes::StringShape
ParamConverter.add(shape_class, Integer) { |i| i.to_s }

@param [Class<Model::Shapes::Shape>] shape_class @param [Class] value_class @param [#call] converter (nil) An object that responds to `#call`

accepting a single argument.  This function should perform
the value conversion if possible, returning the result.
If the conversion is not possible, the original value should
be returned.

@return [void]

# File lib/seahorse/client/param_converter.rb, line 98
def add(shape_class, value_class, converter = nil, &block)
  @converters[shape_class][value_class] = converter || block
end
c(shape, value) click to toggle source

@api private

# File lib/seahorse/client/param_converter.rb, line 103
def c(shape, value)
  if converter = converter_for(shape, value)
    converter.call(value)
  else
    value
  end
end
convert(shape, params) click to toggle source

@param [Model::Shapes::InputShape] shape @param [Hash] params @return [Hash]

# File lib/seahorse/client/param_converter.rb, line 79
def convert(shape, params)
  new(shape).convert(params)
end
converter_for(shape_class, value) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 113
def converter_for(shape_class, value)
  unless @converters[shape_class].key?(value.class)
    @mutex.synchronize {
      unless @converters[shape_class].key?(value.class)
        @converters[shape_class][value.class] = find(shape_class, value)
      end
    }
  end
  @converters[shape_class][value.class]
end
each_base_class(shape_class) { |ancestor| ... } click to toggle source
# File lib/seahorse/client/param_converter.rb, line 138
def each_base_class(shape_class, &block)
  shape_class.ancestors.each do |ancestor|
    yield(ancestor) if @converters.key?(ancestor)
  end
end
find(shape_class, value) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 124
def find(shape_class, value)
  converter = nil
  each_base_class(shape_class) do |klass|
    @converters[klass].each do |value_class, block|
      if value_class === value
        converter = block
        break
      end
    end
    break if converter
  end
  converter
end

Public Instance Methods

convert(params) click to toggle source

@param [Hash] params @return [Hash]

# File lib/seahorse/client/param_converter.rb, line 21
def convert(params)
  structure(@shape, params)
end

Private Instance Methods

c(shape, value) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 70
def c(shape, value)
  self.class.c(shape.class, value)
end
list(list, values) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 41
def list(list, values)
  values = c(list, values)
  if values.is_a?(Array)
    values.map { |v| member(list.member, v) }
  else
    values
  end
end
map(map, values) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 50
def map(map, values)
  values = c(map, values)
  if values.is_a?(Hash)
    values.each.with_object({}) do |(key, value), hash|
      hash[member(map.key, key)] = member(map.value, value)
    end
  else
    values
  end
end
member(shape, value) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 61
def member(shape, value)
  case shape
  when Model::Shapes::Structure then structure(shape, value)
  when Model::Shapes::List then list(shape, value)
  when Model::Shapes::Map then map(shape, value)
  else c(shape, value)
  end
end
structure(structure, values) click to toggle source
# File lib/seahorse/client/param_converter.rb, line 27
def structure(structure, values)
  values = c(structure, values)
  if values.is_a?(Hash)
    values.each do |k, v|
      unless v.nil?
        if structure.member?(k)
          values[k] = member(structure.member(k), v)
        end
      end
    end
  end
  values
end