Class: Aqua::Translator::Rat

Inherits:
Object
  • Object
show all
Defined in:
lib/aqua/object/translator.rb

Overview

Rat class is used by the translators packing side and aggregates methods for merging packed representation, externals and attachments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pack = Mash.new, externals = Mash.new, attachments = []) ⇒ Rat

Returns a new instance of Rat.



170
171
172
173
174
# File 'lib/aqua/object/translator.rb', line 170

def initialize( pack=Mash.new, externals=Mash.new, attachments=[] )
  self.pack = pack
  self.externals = externals
  self.attachments = attachments
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



169
170
171
# File 'lib/aqua/object/translator.rb', line 169

def attachments
  @attachments
end

#externalsObject

Returns the value of attribute externals.



169
170
171
# File 'lib/aqua/object/translator.rb', line 169

def externals
  @externals
end

#packObject

Returns the value of attribute pack.



169
170
171
# File 'lib/aqua/object/translator.rb', line 169

def pack
  @pack
end

Instance Method Details

#==(other_rat) ⇒ Object



219
220
221
# File 'lib/aqua/object/translator.rb', line 219

def ==( other_rat ) 
  self.pack == other_rat.pack && self.externals == other_rat.externals && self.attachments == other_rat.attachments
end

#barf(accessor) ⇒ Object

outputs and resets the accessor



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/aqua/object/translator.rb', line 189

def barf( accessor )
  case accessor
  when :pack, 'pack'
    meal = self.pack
    self.pack = {}
  when :externals, 'externals'  
    meal = self.externals
    self.externals = {}  
  else
    meal = self.attachments
    self.attachments = []
  end    
  meal
end

#eat(other_rat) ⇒ Object

merges the two rats



177
178
179
180
181
182
183
184
185
186
# File 'lib/aqua/object/translator.rb', line 177

def eat( other_rat )
  if self.pack.respond_to?(:keys) 
    self.pack.merge!( other_rat.pack )
  else
    self.pack << other_rat.pack  # this is a special case for array init rats
  end    
  self.externals.merge!( other_rat.externals )
  self.attachments += other_rat.attachments 
  self
end

#hord(other_rat, index) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/aqua/object/translator.rb', line 204

def hord( other_rat, index)
  if [String, Symbol].include?( index.class ) 
    self.pack[index] = other_rat.barf(:pack) 
  else # for nested hording
    eval_string = index.inject("self.pack") do |result, element|
      element = "'#{element}'" if element.class == String
      result += "[#{element}]" 
    end 
    value = other_rat.barf(:pack)
    instance_eval "#{eval_string} = #{value.inspect}"
  end      
  self.eat( other_rat ) 
  self
end