Class: Ramaze::Dictionary

Inherits:
Object show all
Defined in:
lib/ramaze/snippets/ramaze/dictionary.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Dictionary) initialize(*args, &blk)

A new instance of Dictionary



130
131
132
133
134
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 130

def initialize( *args, &blk )
  @order = []
  @order_by = nil
  @hash = Hash.new( *args, &blk )
end

Class Method Details

+ (Object) [](*args)

-- TODO is this needed? Doesn't the super class do this? ++



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 82

def []( *args )
  hsh = new
  if Hash === args[0]
    hsh.replace(args[0])
  elsif (args.size % 2) != 0
    raise ArgumentError, "odd number of elements for Hash"
  else
    while !args.empty?
      hsh[args.shift] = args.shift
    end
  end
  hsh
end

+ (Object) alpha(*args, &block)

Alternate to #new which creates a dictionary sorted by key.

d = Dictionary.alpha
d["z"] = 1
d["y"] = 2
d["x"] = 3
d  #=> {"x"=>3,"y"=>2,"z"=>2}

This is equivalent to:

Dictionary.new.order_by { |key,value| key }


114
115
116
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 114

def alpha( *args, &block )
  new( *args, &block ).order_by_key
end

+ (Object) auto(*args)

Alternate to #new which auto-creates sub-dictionaries as needed.

d = Dictionary.auto
d["a"]["b"]["c"] = "abc"  #=> { "a"=>{"b"=>{"c"=>"abc"}}}


123
124
125
126
127
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 123

def auto( *args )
  #AutoDictionary.new(*args)
  leet = lambda { |hsh, key| hsh[key] = new( &leet ) }
  new(*args, &leet)
end

+ (Object) new_by(*args, &blk)

Like #new but the block sets the order.



98
99
100
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 98

def new_by( *args, &blk )
  new(*args).order_by(&blk)
end

Instance Method Details

- (Object) <<(kv)



321
322
323
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 321

def <<(kv)
  push(*kv)
end

- (Object) ==(hsh2)

def ==( hsh2 )

return false if @order != hsh2.order
super hsh2

end



202
203
204
205
206
207
208
209
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 202

def ==( hsh2 )
  if hsh2.is_a?( Dictionary )
    @order == hsh2.order &&
      @hash  == hsh2.instance_variable_get("@hash")
  else
    false
  end
end

- (Object) [](k)



211
212
213
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 211

def [] k
  @hash[ k ]
end

- (Object) []=(k, i = nil, v = nil)

Store operator.

h[key] = value

Or with additional index.

h[key,index] = value


227
228
229
230
231
232
233
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 227

def []=(k, i=nil, v=nil)
  if v
    insert(i,k,v)
  else
    store(k,i)
  end
end

- (Object) clear



245
246
247
248
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 245

def clear
  @order = []
  @hash.clear
end

- (Object) delete(key)



250
251
252
253
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 250

def delete( key )
  @order.delete( key )
  @hash.delete( key )
end

- (Object) delete_if



271
272
273
274
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 271

def delete_if
  order.clone.each { |k| delete k if yield }
  self
end

- (Object) dup



356
357
358
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 356

def dup
  self.class[*to_a.flatten]
end

- (Object) each Also known as: each_pair



265
266
267
268
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 265

def each
  order.each { |k| yield( k,@hash[k] ) }
  self
end

- (Object) each_key



255
256
257
258
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 255

def each_key
  order.each { |k| yield( k ) }
  self
end

- (Object) each_value



260
261
262
263
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 260

def each_value
  order.each { |k| yield( @hash[k] ) }
  self
end

- (Boolean) empty?

Returns:

  • (Boolean)


395
396
397
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 395

def empty?
  @hash.empty?
end

- (Object) fetch(k)



215
216
217
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 215

def fetch( k )
  @hash.fetch( k )
end

- (Object) find



377
378
379
380
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 377

def find
  each{|k,v| return k, v if yield(k,v) }
  return nil
end

- (Object) first



382
383
384
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 382

def first
  @hash[order.first]
end

- (Object) insert(i, k, v)



235
236
237
238
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 235

def insert( i,k,v )
  @order.insert( i,k )
  @hash.store( k,v )
end

- (Object) inspect



350
351
352
353
354
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 350

def inspect
  ary = []
  each {|k,v| ary << k.inspect + "=>" + v.inspect}
  '{' + ary.join(", ") + '}'
end

- (Object) invert



286
287
288
289
290
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 286

def invert
  hsh2 = self.class.new
  order.each { |k| hsh2[@hash[k]] = k }
  hsh2
end

- (Object) keys



282
283
284
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 282

def keys
  order
end

- (Object) last



386
387
388
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 386

def last
  @hash[order.last]
end

- (Object) length Also known as: size



390
391
392
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 390

def length
  @order.length
end

- (Object) merge(hsh2)



367
368
369
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 367

def merge( hsh2 )
  self.dup.update(hsh2)
end

- (Object) order



136
137
138
139
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 136

def order
  reorder if @order_by
  @order
end

- (Object) order_by(&block)

Keep dictionary sorted by a specific sort order.



143
144
145
146
147
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 143

def order_by( &block )
  @order_by = block
  order
  self
end

- (Object) order_by_key

Keep dictionary sorted by key.

d = Dictionary.new.order_by_key
d["z"] = 1
d["y"] = 2
d["x"] = 3
d  #=> {"x"=>3,"y"=>2,"z"=>2}

This is equivalent to:

Dictionary.new.order_by { |key,value| key }

The initializer Dictionary#alpha also provides this.



163
164
165
166
167
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 163

def order_by_key
  @order_by = lambda { |k,v| k }
  order
  self
end

- (Object) order_by_value

Keep dictionary sorted by value.

d = Dictionary.new.order_by_value
d["z"] = 1
d["y"] = 2
d["x"] = 3
d  #=> {"x"=>3,"y"=>2,"z"=>2}

This is equivalent to:

Dictionary.new.order_by { |key,value| value }


181
182
183
184
185
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 181

def order_by_value
  @order_by = lambda { |k,v| v }
  order
  self
end

- (Object) pop



335
336
337
338
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 335

def pop
  key = order.last
  key ? [key,delete(key)] : nil
end

- (Object) push(k, v)



325
326
327
328
329
330
331
332
333
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 325

def push( k,v )
  unless @hash.include?( k )
    @order.push( k )
    @hash.store( k,v )
    true
  else
    false
  end
end

- (Object) reject(&block)



292
293
294
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 292

def reject( &block )
  self.dup.delete_if(&block)
end

- (Object) reject!(&block)



296
297
298
299
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 296

def reject!( &block )
  hsh2 = reject(&block)
  self == hsh2 ? nil : hsh2
end

- (Object) reorder



189
190
191
192
193
194
195
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 189

def reorder
  if @order_by
    assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by( &@order_by )
    @order = assoc.collect{ |k,v| k }
  end
  @order
end

- (Object) replace(hsh2)



301
302
303
304
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 301

def replace( hsh2 )
  @order = hsh2.order
  @hash = hsh2.hash
end

- (Object) select



371
372
373
374
375
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 371

def select
  ary = []
  each { |k,v| ary << [k,v] if yield k,v }
  ary
end

- (Object) shift



306
307
308
309
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 306

def shift
  key = order.first
  key ? [key,delete(key)] : super
end

- (Object) store(a, b)



240
241
242
243
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 240

def store( a,b )
  @order.push( a ) unless @hash.has_key?( a )
  @hash.store( a,b )
end

- (Object) to_a



340
341
342
343
344
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 340

def to_a
  ary = []
  each { |k,v| ary << [k,v] }
  ary
end

- (Object) to_s



346
347
348
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 346

def to_s
  self.to_a.to_s
end

- (Object) unshift(k, v)



311
312
313
314
315
316
317
318
319
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 311

def unshift( k,v )
  unless @hash.include?( k )
    @order.unshift( k )
    @hash.store( k,v )
    true
  else
    false
  end
end

- (Object) update(hsh2) Also known as: merge!



360
361
362
363
364
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 360

def update( hsh2 )
  hsh2.each { |k,v| self[k] = v }
  reorder
  self
end

- (Object) values



276
277
278
279
280
# File 'lib/ramaze/snippets/ramaze/dictionary.rb', line 276

def values
  ary = []
  order.each { |k| ary.push @hash[k] }
  ary
end