Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/aqua/support/mash.rb,
lib/aqua/object/initializers.rb

Direct Known Subclasses

HashWithIndifferentAccess

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.aqua_init(init, opts = Aqua::Translator::Opts.new) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/aqua/object/initializers.rb', line 220

def self.aqua_init( init, opts=Aqua::Translator::Opts.new )
  unpacked = {}
  init.each do |key, value|
    unless key == aqua_key_register
      if key.match(/^#{aqua_key_register_prefix}(\d*)$/)
        index = $1.to_i
        key = Aqua::Translator.unpack_object( init[aqua_key_register][index], opts )
      end 
      opts.path += "[#{key}]" 
      value = Aqua::Translator.unpack_object( value, opts )
      unpacked[ key ] = value 
    end  
  end
  self == Hash ? unpacked : new( unpacked )  
end

.aqua_key_registerObject



203
204
205
# File 'lib/aqua/object/initializers.rb', line 203

def self.aqua_key_register
  "#{aqua_key_register_prefix}KEYS".freeze
end

.aqua_key_register_prefixObject



207
208
209
# File 'lib/aqua/object/initializers.rb', line 207

def self.aqua_key_register_prefix
  "/_OBJECT_"
end

.aqua_object_key_index(index) ⇒ Object



211
212
213
# File 'lib/aqua/object/initializers.rb', line 211

def self.aqua_object_key_index( index ) 
  "#{aqua_key_register_prefix}#{index}"
end

Instance Method Details

#aqua_next_object_index(hash) ⇒ Object



215
216
217
218
# File 'lib/aqua/object/initializers.rb', line 215

def aqua_next_object_index( hash )
  hash[self.class.aqua_key_register] ||= []
  hash[self.class.aqua_key_register].size
end

#to_aqua_init(path = '') ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/aqua/object/initializers.rb', line 185

def to_aqua_init( path='')
  rat = Aqua::Translator::Rat.new
  self.each do |raw_key, value|
    key_class = raw_key.class
    if key_class == String
      key = raw_key
    else # key is an object 
      index = aqua_next_object_index( rat.pack )  
      key = self.class.aqua_object_key_index( index )
      key_rat = Aqua::Translator.pack_object( raw_key, path+"['#{self.class.aqua_key_register}'][#{index}]")
      rat.hord( key_rat, [self.class.aqua_key_register, index] )
    end
    obj_rat = Aqua::Translator.pack_object( value, path+"['#{key}']" )
    rat.hord( obj_rat, key )
  end
  rat 
end

#with_indifferent_accessObject



8
9
10
11
12
# File 'lib/aqua/support/mash.rb', line 8

def with_indifferent_access
  hash = HashWithIndifferentAccess.new(self)
  hash.default = self.default
  hash
end