Class: AWS::Core::Data
- Inherits:
-
Object
- Object
- AWS::Core::Data
- Includes:
- MethodMissingProxy
- Defined in:
- lib/aws/core/data.rb
Overview
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 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']
Defined Under Namespace
Modules: MethodMissingProxy Classes: List
Class Method Summary collapse
-
.cast(value) ⇒ Data, ...
Given a hash, this method returns a Data object.
Instance Method Summary collapse
-
#initialize(data) ⇒ Data
constructor
A new instance of Data.
-
#inspect ⇒ String
Returns an inspection string from the wrapped data.
- #kind_of?(klass) ⇒ Boolean (also: #is_a?)
- #method_missing(method_name, *args, &block) ⇒ Object
-
#respond_to?(method_name) ⇒ Boolean
Returns true if this data object will respond to the given method name.
- #to_a ⇒ Array (also: #to_ary)
-
#to_hash ⇒ Hash
(also: #to_h)
Returns contents of this Data object as a raw hash.
Methods included from MethodMissingProxy
Constructor Details
#initialize(data) ⇒ Data
Returns a new instance of Data.
121 122 123 |
# File 'lib/aws/core/data.rb', line 121 def initialize data @data = data end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/aws/core/data.rb', line 108 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 |
Class Method Details
.cast(value) ⇒ Data, ...
Given a hash, this method returns a AWS::Core::Data object. Given an Array, this method returns a List object. Everything else is returned as is.
188 189 190 191 192 193 194 |
# File 'lib/aws/core/data.rb', line 188 def cast value case value when Hash then Data.new(value) when Array then Data::List.new(value) else value end end |
Instance Method Details
#inspect ⇒ String
152 153 154 |
# File 'lib/aws/core/data.rb', line 152 def inspect @data.inspect end |
#kind_of?(klass) ⇒ Boolean Also known as: is_a?
157 158 159 160 161 162 163 |
# File 'lib/aws/core/data.rb', line 157 def kind_of? klass if klass == Hash true else super end end |
#respond_to?(method_name) ⇒ Boolean
Returns true if this data object will respond to the given method name.
140 141 142 143 |
# File 'lib/aws/core/data.rb', line 140 def respond_to? method_name @data.key?(_remove_question_mark(method_name)) or @data.respond_to?(method_name) end |
#to_a ⇒ Array Also known as: to_ary
132 133 134 |
# File 'lib/aws/core/data.rb', line 132 def to_a @data.to_a end |
#to_hash ⇒ Hash Also known as: to_h
Returns contents of this Data object as a raw hash.
126 127 128 |
# File 'lib/aws/core/data.rb', line 126 def to_hash @data end |