class AWS::Core::Data

Data is a light wrapper around a Ruby hash that provides method missing access to the hash contents.

## Method Missing Access

You can access hash content with methods if their keys are symbols.

data = AWS::Core::Data.new({ :a => 1, :b => 2, :c => true })
data.a #=> 1
data.b #=> 2
data.c #=> true
data.d #=> raises NoMethodError

## Boolean Methods

Given the structure above you can also use question-mark methods.

data.c? #=> true
data.d? #=> raises NoMethodError

## Nested Hashes

If the data contains nested hashes you can chain methods into the structure.

data = AWS::Core::Data.new(:a => { :b => { :c => 'abc' }})
data.a.b.c #=> 'abc'

## Nested Arrays

Arrays are wrapped in {Data::List} objects. They ensure any data returned is correctly wrapped so you can continue using method-missing access.

data = AWS::Core::Data.new(
 :people => [
   {:name => 'john'},
   {:name => 'jane'},
]})

data.people[0].name #=> 'john'
data.people[1].name #=> 'jane'

data.people.map(&:name) #=> ['john','jane']

Public Class Methods

new(data) click to toggle source

@param [Hash] data The ruby hash of data you need wrapped.

# File lib/aws/core/data.rb, line 126
def initialize data
  @data = data
end

Protected Class Methods

cast(value) click to toggle source

Given a hash, this method returns a {Data} object. Given an Array, this method returns a {Data::List} object. Everything else is returned as is.

@param [Object] value The value to conditionally wrap.

@return [Data,Data::List,Object] Wraps hashes and lists with

Data and List objects, all other objects are returned as
is.
# File lib/aws/core/data.rb, line 193
def cast value
  case value
  when Hash then Data.new(value)
  when Array then Data::List.new(value)
  else value
  end
end

Public Instance Methods

inspect() click to toggle source

Returns an inspection string from the wrapped data.

data = AWS::Core::Data.new({ :a => 1, :b => 2, :c => true })
data.inspect #=> '{:a=>1, :b=>2, :c=>true}'

@return [String]

# File lib/aws/core/data.rb, line 157
def inspect
  @data.inspect
end
is_a?(klass)
Alias for: kind_of?
kind_of?(klass) click to toggle source

@api private

Calls superclass method
# File lib/aws/core/data.rb, line 162
def kind_of? klass
  if klass == Hash
    true
  else
    super
  end
end
Also aliased as: is_a?
method_missing(method_name, *args, &block) click to toggle source
# File lib/aws/core/data.rb, line 113
def method_missing method_name, *args, &block
  if
    args.empty? and !block_given? and
    key = _remove_question_mark(method_name) and
    @data.has_key?(key)
  then
    Data.cast(@data[key])
  else
    super
  end
end
respond_to?(method_name) click to toggle source

@param [String,Symbol] method_name @return [Boolean] Returns true if this data object will

respond to the given method name.
# File lib/aws/core/data.rb, line 145
def respond_to? method_name
  @data.key?(_remove_question_mark(method_name)) or
    @data.respond_to?(method_name)
end
to_a() click to toggle source

@return [Array]

# File lib/aws/core/data.rb, line 137
def to_a
  @data.to_a
end
Also aliased as: to_ary
to_ary()
Alias for: to_a
to_h()
Alias for: to_hash
to_hash() click to toggle source

@return [Hash] Returns contents of this Data object as a raw hash.

# File lib/aws/core/data.rb, line 131
def to_hash
  @data
end
Also aliased as: to_h

Protected Instance Methods

_remove_question_mark(method_name) click to toggle source
# File lib/aws/core/data.rb, line 173
def _remove_question_mark method_name
  case method_name
  when Symbol then method_name.to_s.sub(/\?$/, '').to_sym
  when String then method_name.sub(/\?$/, '')
  else method_name
  end
end