Parent

Namespace

Methods

Map

Constants

Dup
Load
Version

Public Class Methods

_explode(key, value, accum = {:branches => [], :leaves => []}) click to toggle source
# File lib/map.rb, line 890
def Map._explode(key, value, accum = {:branches => [], :leaves => []})
  key = Array(key).flatten

  case value
    when Array
      accum[:branches].push([key, Array])

      value.each_with_index do |v, k|
        Map._explode(key + [k], v, accum)
      end

    when Hash
      accum[:branches].push([key, Map])

      value.each do |k, v|
        Map._explode(key + [k], v, accum)
      end

    else
      accum[:leaves].push([key, value])
  end

  accum
end
add(*args) click to toggle source
# File lib/map.rb, line 915
def Map.add(*args)
  args.flatten!
  args.compact!

  Map.for(args.shift).tap do |map|
    args.each{|arg| map.add(arg)}
  end
end
add_conversion_method!(method) click to toggle source
# File lib/map.rb, line 94
def add_conversion_method!(method)
  if define_conversion_method!(method)
    method = method.to_s.strip
    raise ArguementError if method.empty?
    module_eval(          unless conversion_methods.include?(#{ method.inspect })            conversion_methods.unshift(#{ method.inspect })          end, __FILE__, __LINE__)
    true
  else
    false
  end
end
allocate() click to toggle source
# File lib/map.rb, line 29
def allocate
  super.instance_eval do
    @keys = []
    self
  end
end
alphanumeric_key_for(key) click to toggle source
# File lib/map.rb, line 1020
def Map.alphanumeric_key_for(key)
  return key if key.is_a?(Numeric)

  digity, stringy, digits = /^(~)?(\d+)$/omx.match(key).to_a

  digity ? stringy ? String(digits) : Integer(digits) : key
end
args_for_arity(args, arity) click to toggle source
# File lib/map.rb, line 178
def args_for_arity(args, arity)
  arity = Integer(arity.respond_to?(:arity) ? arity.arity : arity)
  arity < 0 ? args.dup : args.slice(0, arity)
end
bcall(*args, &block) click to toggle source
# File lib/map.rb, line 188
def bcall(*args, &block)
  args = Map.args_for_arity(args, block.arity)
  block.call(*args)
end
blank?(value) click to toggle source
# File lib/map.rb, line 727
def Map.blank?(value)
  return value.blank? if value.respond_to?(:blank?)

  case value
    when String
      value.strip.empty?
    when Numeric
      value == 0
    when false
      true
    else
      value.respond_to?(:empty?) ? value.empty? : !value
  end
end
breadth_first_each(enumerable, accum = [], &block) click to toggle source
# File lib/map.rb, line 1093
def Map.breadth_first_each(enumerable, accum = [], &block)
  levels = []

  keys = Map.depth_first_keys(enumerable)

  keys.each do |key|
    key.size.times do |i|
      k = key.slice(0, i + 1)
      level = k.size - 1
      levels[level] ||= Array.new
      last = levels[level].last
      levels[level].push(k) unless last == k
    end
  end

  levels.each do |level|
    level.each do |key|
      val = enumerable.get(key)
      block ? block.call(key, val) : accum.push([key, val])
    end
  end

  block ? enumerable : accum
end
call(object, method, *args, &block) click to toggle source
# File lib/map.rb, line 183
def call(object, method, *args, &block)
  args = Map.args_for_arity(args, object.method(method).arity)
  object.send(method, *args, &block)
end
coerce(other) click to toggle source
# File lib/map.rb, line 50
def coerce(other)
  case other
    when Map
      other
    else
      allocate.update(other.to_hash)
  end
end
collection_has?(collection, key, &block) click to toggle source
# File lib/map.rb, line 769
def Map.collection_has?(collection, key, &block)
  has_key =
    case collection
      when Array
        key = (Integer(key) rescue -1)
        (0...collection.size).include?(key)

      when Hash
        collection.has_key?(key)

      else
        raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]")
    end

  block.call(key) if(has_key and block)

  has_key
end
collection_key(collection, key, &block) click to toggle source
# File lib/map.rb, line 747
def Map.collection_key(collection, key, &block)
  case collection
    when Array
      begin
        key = Integer(key)
      rescue
        raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]")
      end
      collection[key]

    when Hash
      collection[key]

    else
      raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]")
  end
end
collection_set(collection, key, value, &block) click to toggle source
# File lib/map.rb, line 792
def Map.collection_set(collection, key, value, &block)
  set_key = false

  case collection
    when Array
      begin
        key = Integer(key)
      rescue
        raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]=#{ value.inspect }")
      end
      set_key = true
      collection[key] = value

    when Hash
      set_key = true
      collection[key] = value

    else
      raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]=#{ value.inspect }")
  end

  block.call(key) if(set_key and block)

  [key, value]
end
combine(*args) click to toggle source
# File lib/map.rb, line 924
def Map.combine(*args)
  Map.add(*args)
end
conversion_methods() click to toggle source
# File lib/map.rb, line 65
def conversion_methods
  @conversion_methods ||= (
    map_like = ancestors.select{|ancestor| ancestor <= Map}
    type_names = map_like.map do |ancestor|
      name = ancestor.name.to_s.strip
      next if name.empty?
      name.downcase.gsub(/::/, '_')
    end.compact
    list = type_names.map{|type_name| "to_#{ type_name }"}
    list.each{|method| define_conversion_method!(method)}
    list
  )
end
convert_key(key) click to toggle source
# File lib/map.rb, line 253
def Map.convert_key(key)
  key.kind_of?(Symbol) ? key.to_s : key
end
convert_value(value) click to toggle source
# File lib/map.rb, line 265
def self.convert_value(value)
  conversion_methods.each do |method|
    #return convert_value(value.send(method)) if value.respond_to?(method)
    hashlike = value.is_a?(Hash)
    if hashlike and value.respond_to?(method)
      value = value.send(method)
      break
    end
  end

  case value
    when Hash
      coerce(value)
    when Array
      value.map!{|v| convert_value(v)}
    else
      value
  end
end
define_conversion_method!(method) click to toggle source
# File lib/map.rb, line 79
def define_conversion_method!(method)
  method = method.to_s.strip
  raise ArguementError if method.empty?
  module_eval(        unless public_method_defined?(#{ method.inspect })          def #{ method }            self          end          true        else          false        end, __FILE__, __LINE__)
end
demongoize(object) click to toggle source
# File lib/map.rb, line 1162
def Map.demongoize(object)
  Map.for(object)
end
depth_first_each(enumerable, path = [], accum = [], &block) click to toggle source

TODO - technically this returns only leaves so the name isn't quite right. re-factor for 3.0

# File lib/map.rb, line 1042
def Map.depth_first_each(enumerable, path = [], accum = [], &block)
  Map.pairs_for(enumerable) do |key, val|
    path.push(key)
    if((val.is_a?(Hash) or val.is_a?(Array)) and not val.empty?)
      Map.depth_first_each(val, path, accum)
    else
      accum << [path.dup, val]
    end
    path.pop()
  end
  if block
    accum.each{|keys, val| block.call(keys, val)}
  else
    accum
  end
end
depth_first_keys(enumerable, path = [], accum = [], &block) click to toggle source
# File lib/map.rb, line 1059
def Map.depth_first_keys(enumerable, path = [], accum = [], &block)
  accum = Map.depth_first_each(enumerable, path = [], accum = [], &block)
  accum.map!{|kv| kv.first}
  accum
end
depth_first_values(enumerable, path = [], accum = [], &block) click to toggle source
# File lib/map.rb, line 1065
def Map.depth_first_values(enumerable, path = [], accum = [], &block)
  accum = Map.depth_first_each(enumerable, path = [], accum = [], &block)
  accum.map!{|kv| kv.last}
  accum
end
each_pair(*args, &block) click to toggle source

iterate over arguments in pairs smartly.

# File lib/map.rb, line 111
def each_pair(*args, &block)
  size = args.size
  first = args.first

  if block.nil?
    result = []
    block = lambda{|*kv| result.push(kv)}
  else
    result = args
  end

  return args if size == 0

  if size == 1
    conversion_methods.each do |method|
      if first.respond_to?(method)
        first = first.send(method)
        break
      end
    end

    if first.respond_to?(:each_pair)
      first.each_pair do |key, val|
        block.call(key, val)
      end
      return args
    end

    if first.respond_to?(:each_slice)
      first.each_slice(2) do |key, val|
        block.call(key, val)
      end
      return args
    end

    raise(ArgumentError, 'odd number of arguments for Map')
  end

  array_of_pairs = args.all?{|a| a.is_a?(Array) and a.size == 2}

  if array_of_pairs
    args.each do |pair|
      k, v = pair[0..1]
      block.call(k, v)
    end
  else
    0.step(args.size - 1, 2) do |a|
      key = args[a]
      val = args[a + 1]
      block.call(key, val)
    end
  end

  args
end
evolve(object) click to toggle source
# File lib/map.rb, line 1166
def Map.evolve(object)
  Map.for(object)
end
explode(hash) click to toggle source
# File lib/map.rb, line 872
def Map.explode(hash)
  accum = {:branches => [], :leaves => []}

  hash.each do |key, value|
    Map._explode(key, value, accum)
  end

  branches = accum[:branches]
  leaves = accum[:leaves]

  sort_by_key_size = proc{|a,b| a.first.size <=> b.first.size}

  branches.sort!(&sort_by_key_size)
  leaves.sort!(&sort_by_key_size)

  accum
end
for(*args, &block) click to toggle source
# File lib/map.rb, line 43
def for(*args, &block)
  if(args.size == 1 and block.nil?)
    return args.first if args.first.class == self
  end
  new(*args, &block)
end
from_hash(hash, order = nil) click to toggle source

support for building ordered hasshes from a map's own image

# File lib/map.rb, line 537
def Map.from_hash(hash, order = nil)
  map = Map.for(hash)
  map.reorder!(order) if order
  map
end
intersection(a, b) click to toggle source
# File lib/map.rb, line 168
def intersection(a, b)
  a, b, i = Map.for(a), Map.for(b), Map.new
  a.depth_first_each{|key, val| i.set(key, val) if b.has?(key)}
  i
end
key_for(*keys) click to toggle source
# File lib/map.rb, line 1032
def self.key_for(*keys)
  return keys.flatten
end
keys_for(enumerable) click to toggle source
# File lib/map.rb, line 1118
def Map.keys_for(enumerable)
  keys = enumerable.respond_to?(:keys) ? enumerable.keys : Array.new(enumerable.size){|i| i}
  keys
end
libdir(*args, &block) click to toggle source
# File lib/map.rb, line 11
def libdir(*args, &block)
  @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
  libdir = args.empty? ? @libdir : File.join(@libdir, *args.map{|arg| arg.to_s})
ensure
  if block
    begin
      $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.first==libdir
      module_eval(&block)
    ensure
      $LOAD_PATH.shift() if $LOAD_PATH.first==libdir
    end
  end
end
load(*args, &block) click to toggle source
# File lib/map.rb, line 25
def load(*args, &block)
  libdir{ Load.call(*args, &block) }
end
map_for(hash) click to toggle source
# File lib/map.rb, line 244
def Map.map_for(hash)
  map = klass.coerce(hash)
  map.default = hash.default
  map
end
match(haystack, needle) click to toggle source
# File lib/map.rb, line 174
def match(haystack, needle)
  intersection(haystack, needle) == needle
end
new(*args, &block) click to toggle source
# File lib/map.rb, line 200
def initialize(*args, &block)
  case args.size
    when 0
      super(&block)

    when 1
      first = args.first
      case first
        when nil, false
          nil
        when Hash
          initialize_from_hash(first)
        when Array
          initialize_from_array(first)
        else
          if first.respond_to?(:to_hash)
            initialize_from_hash(first.to_hash)
          else
            initialize_from_hash(first)
          end
      end

    else
      initialize_from_array(args)
  end
end
new(*args, &block) click to toggle source
# File lib/map.rb, line 36
def new(*args, &block)
  allocate.instance_eval do
    initialize(*args, &block)
    self
  end
end
options_for(*args, &block) click to toggle source
# File lib/map/options.rb, line 160
def Map.options_for(*args, &block)
  Map::Options.for(*args, &block)
end
options_for!(*args, &block) click to toggle source
# File lib/map/options.rb, line 164
def Map.options_for!(*args, &block)
  Map::Options.for(*args, &block).pop
end
pairs_for(enumerable, *args, &block) click to toggle source
# File lib/map.rb, line 1071
def Map.pairs_for(enumerable, *args, &block)
  if block.nil?
    pairs, block = [], lambda{|*pair| pairs.push(pair)}
  else
    pairs = false
  end

  result =
    case enumerable
      when Hash
        enumerable.each_pair(*args, &block)
      when Array
        enumerable.each_with_index(*args) do |val, key|
          block.call(key, val)
        end
      else
        enumerable.each_pair(*args, &block)
    end

  pairs ? pairs : result
end
struct(*args, &block) click to toggle source
# File lib/map/struct.rb, line 49
def Map.struct(*args, &block)
  new(*args, &block).struct
end
tap(*args, &block) click to toggle source
# File lib/map.rb, line 59
def tap(*args, &block)
  new(*args).tap do |map|
    map.tap(&block) if block
  end
end
update_options_for!(args, &block) click to toggle source
# File lib/map/options.rb, line 168
def Map.update_options_for!(args, &block)
  options = Map.options_for(args)
  block.call(options)
end
version() click to toggle source
# File lib/map.rb, line 7
def version
  Map::Version
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/map.rb, line 511
def <=>(other)
  cmp = keys <=> klass.coerce(other).keys
  return cmp unless cmp.zero?
  values <=> klass.coerce(other).values
end
==(other) click to toggle source

equality / sorting / matching support

# File lib/map.rb, line 497
def ==(other)
  case other
    when Map
      return false if keys != other.keys
      super(other)

    when Hash
      self == Map.from_hash(other, self)

    else
      false
  end
end
=~(hash) click to toggle source
# File lib/map.rb, line 517
def =~(hash)
  to_hash == klass.coerce(hash).to_hash
end
[](key) click to toggle source
# File lib/map.rb, line 338
def [](key)
  key = convert_key(key)
  __get__(key)
end
[]=(key, val) click to toggle source
# File lib/map.rb, line 331
def []=(key, val)
  key, val = convert(key, val)
  keys.push(key) unless has_key?(key)
  __set__(key, val)
end
add(*args) click to toggle source
# File lib/map.rb, line 846
def add(*args)
  case
    when args.empty?
      return []
    when args.size == 1 && args.first.is_a?(Hash)
      hash = args.shift
    else
      hash = {}
      value = args.pop
      key = Array(args).flatten
      hash[key] = value
  end

  exploded = Map.explode(hash)

  exploded[:branches].each do |bkey, btype|
    set(bkey, btype.new) unless get(bkey).is_a?(btype)
  end

  exploded[:leaves].each do |lkey, lvalue|
    set(lkey, lvalue)
  end

  self
end
alphanumeric_key_for(key) click to toggle source
# File lib/map.rb, line 1028
def alphanumeric_key_for(key)
  Map.alphanumeric_key_for(key)
end
apply(other) click to toggle source
# File lib/map.rb, line 1013
def apply(other)
  Map.for(other).depth_first_each do |keys, value|
    set(keys => value) unless !get(keys).nil?
  end
  self
end
blank?(*keys) click to toggle source
# File lib/map.rb, line 742
def blank?(*keys)
  return empty? if keys.empty?
  !has?(*keys) or Map.blank?(get(*keys))
end
breadth_first_each(*args, &block) click to toggle source
# File lib/map.rb, line 1135
def breadth_first_each(*args, &block)
  Map.breadth_first_each(self, *args, &block)
end
clear() click to toggle source
# File lib/map.rb, line 426
def clear
  keys.clear
  super
end
clone() click to toggle source
# File lib/map.rb, line 313
def clone
  copy
end
collection_has?(*args, &block) click to toggle source
# File lib/map.rb, line 788
def collection_has?(*args, &block)
  Map.collection_has?(*args, &block)
end
collection_key(*args, &block) click to toggle source
# File lib/map.rb, line 765
def collection_key(*args, &block)
  Map.collection_key(*args, &block)
end
collection_set(*args, &block) click to toggle source
# File lib/map.rb, line 818
def collection_set(*args, &block)
  Map.collection_set(*args, &block)
end
combine(*args, &block) click to toggle source
# File lib/map.rb, line 932
def combine(*args, &block)
  dup.tap do |map|
    map.combine!(*args, &block)
  end
end
combine!(*args, &block) click to toggle source
# File lib/map.rb, line 928
def combine!(*args, &block)
  add(*args, &block)
end
contains(other) click to toggle source
# File lib/map.rb, line 1139
def contains(other)
  other = other.is_a?(Hash) ? Map.coerce(other) : other
  breadth_first_each{|key, value| return true if value == other}
  return false
end
conversion_methods() click to toggle source

conversions

# File lib/map.rb, line 572
def conversion_methods
  self.class.conversion_methods
end
convert(key, val) click to toggle source
# File lib/map.rb, line 293
def convert(key, val)
  [convert_key(key), convert_value(val)]
end
convert_key(key) click to toggle source
# File lib/map.rb, line 257
def convert_key(key)
  if klass.respond_to?(:convert_key)
    klass.convert_key(key)
  else
    Map.convert_key(key)
  end
end
convert_value(value) click to toggle source
# File lib/map.rb, line 284
def convert_value(value)
  if klass.respond_to?(:convert_value)
    klass.convert_value(value)
  else
    Map.convert_value(value)
  end
end
copy() click to toggle source
# File lib/map.rb, line 297
def copy
  default = self.default
  self.default = nil
  copy = Marshal.load(Marshal.dump(self)) rescue Dup.bind(self).call()
  copy.default = default
  copy
ensure
  self.default = default
end
default(key = nil) click to toggle source
# File lib/map.rb, line 317
def default(key = nil)
  key.is_a?(Symbol) && include?(key = key.to_s) ? self[key] : super
end
default=(value) click to toggle source
# File lib/map.rb, line 321
def default=(value)
  raise ArgumentError.new("Map doesn't work so well with a non-nil default value!") unless value.nil?
end
delete(key) click to toggle source

mutators

# File lib/map.rb, line 420
def delete(key)
  key = convert_key(key)
  keys.delete(key)
  super(key)
end
delete_if(&block) click to toggle source
# File lib/map.rb, line 431
def delete_if(&block)
  to_delete = []

  each do |key, val|
    args = [key, val]
    to_delete.push(key) if !!Map.bcall(*args, &block)
  end

  to_delete.each{|key| delete(key)}

  self
end
depth_first_each(*args, &block) click to toggle source
# File lib/map.rb, line 1123
def depth_first_each(*args, &block)
  Map.depth_first_each(self, *args, &block)
end
depth_first_keys(*args, &block) click to toggle source
# File lib/map.rb, line 1127
def depth_first_keys(*args, &block)
  Map.depth_first_keys(self, *args, &block)
end
depth_first_values(*args, &block) click to toggle source
# File lib/map.rb, line 1131
def depth_first_values(*args, &block)
  Map.depth_first_values(self, *args, &block)
end
deserialize(object) click to toggle source
# File lib/map.rb, line 1158
def deserialize(object)
  ::Map.for(object)
end
dup() click to toggle source
# File lib/map.rb, line 309
def dup
  copy
end
each() click to toggle source
# File lib/map.rb, line 412
def each
  keys.each{|key| yield(key, self[key])}
  self
end
each_key() click to toggle source
# File lib/map.rb, line 402
def each_key
  keys.each{|key| yield(key)}
  self
end
each_value() click to toggle source
# File lib/map.rb, line 407
def each_value
  keys.each{|key| yield self[key]}
  self
end
each_with_index() click to toggle source

iterator methods

# File lib/map.rb, line 397
def each_with_index
  keys.each_with_index{|key, index| yield([key, self[key]], index)}
  self
end
extractable_options?() click to toggle source

for rails' extract_options! compat

# File lib/map.rb, line 1148
def extractable_options?
  true
end
fetch(key, *args, &block) click to toggle source
# File lib/map.rb, line 343
def fetch(key, *args, &block)
  key = convert_key(key)
  super(key, *args, &block)
end
first() click to toggle source
# File lib/map.rb, line 387
def first
  [keys.first, self[keys.first]]
end
forcing(forcing=nil, &block) click to toggle source
# File lib/map.rb, line 992
def forcing(forcing=nil, &block)
  @forcing ||= nil

  if block
    begin
      previous = @forcing
      @forcing = forcing
      block.call()
    ensure
      @forcing = previous
    end
  else
    @forcing
  end
end
forcing?(forcing=nil) click to toggle source
# File lib/map.rb, line 1008
def forcing?(forcing=nil)
  @forcing ||= nil
  @forcing == forcing
end
get(*keys) click to toggle source

support for compound key indexing and depth first iteration

# File lib/map.rb, line 670
def get(*keys)
  keys = key_for(keys)

  if keys.size <= 1
    if !self.has_key?(keys.first) && block_given?
      return yield
    else
      return self[keys.first]
    end
  end

  keys, key = keys[0..-2], keys[-1]
  collection = self

  keys.each do |k|
    if Map.collection_has?(collection, k)
      collection = Map.collection_key(collection, k)
    else
      collection = nil
    end

    unless collection.respond_to?('[]')
      leaf = collection
      return leaf
    end
  end

  if !Map.collection_has?(collection, key) && block_given?
    yield #default_value
  else
    Map.collection_key(collection, key)
  end
end
has?(*keys) click to toggle source
# File lib/map.rb, line 704
def has?(*keys)
  keys = key_for(keys)
  collection = self

  return Map.collection_has?(collection, keys.first) if keys.size <= 1

  keys, key = keys[0..-2], keys[-1]

  keys.each do |k|
    if Map.collection_has?(collection, k)
      collection = Map.collection_key(collection, k)
    else
      collection = nil
    end

    return collection unless collection.respond_to?('[]')
  end

  return false unless(collection.is_a?(Hash) or collection.is_a?(Array))

  Map.collection_has?(collection, key)
end
id() click to toggle source
# File lib/map.rb, line 662
def id
  return self[:id] if has_key?(:id)
  return self[:_id] if has_key?(:_id)
  raise NoMethodError
end
initialize_from_array(array) click to toggle source
# File lib/map.rb, line 233
def initialize_from_array(array)
  map = self
  Map.each_pair(array){|key, val| map[key] = val}
end
initialize_from_hash(hash) click to toggle source
# File lib/map.rb, line 227
def initialize_from_hash(hash)
  map = self
  map.update(hash)
  map.default = hash.default
end
inspect(*args, &block) click to toggle source
# File lib/map.rb, line 565
def inspect(*args, &block)
  require 'pp' unless defined?(PP)
  PP.pp(self, '')
end
invert() click to toggle source
# File lib/map.rb, line 543
def invert
  inverted = klass.allocate
  inverted.default = self.default
  keys.each{|key| inverted[self[key]] = key }
  inverted
end
keep_if( &block ) click to toggle source

See: github.com/rubinius/rubinius/blob/98c516820d9f78bd63f29dab7d5ec9bc8692064d/kernel/common/hash19.rb#L476-L484

# File lib/map.rb, line 445
def keep_if( &block )
  raise RuntimeError.new( "can't modify frozen #{ self.class.name }" ) if frozen?
  return to_enum( :keep_if ) unless block_given?
  each { | key , val | delete key unless yield( key , val ) }
  self
end
key?(key) click to toggle source
# File lib/map.rb, line 348
def key?(key)
  super(convert_key(key))
end
key_for(*keys) click to toggle source
# File lib/map.rb, line 1036
def key_for(*keys)
  self.class.key_for(*keys)
end
keys() click to toggle source

instance constructor

# File lib/map.rb, line 196
def keys
  @keys ||= []
end
klass() click to toggle source

support methods

# File lib/map.rb, line 240
def klass
  self.class
end
last() click to toggle source
# File lib/map.rb, line 391
def last
  [keys.last, self[keys.last]]
end
leaf_for(key, options = {}, &block) click to toggle source
# File lib/map.rb, line 938
def leaf_for(key, options = {}, &block)
  leaf = self
  key = Array(key).flatten
  k = key.first

  key.each_cons(2) do |a, b|
    exists = Map.collection_has?(leaf, a)

    case b
      when Numeric
        if options[:autovivify]
          Map.collection_set(leaf, a, Array.new) unless exists
        end

      when String, Symbol
        if options[:autovivify]
          Map.collection_set(leaf, a, Map.new) unless exists
        end
    end

    leaf = Map.collection_key(leaf, a)
    k = b
  end

  block ? block.call(leaf, k) : [leaf, k]
end
map_for(hash) click to toggle source
# File lib/map.rb, line 249
def map_for(hash)
  klass.map_for(hash)
end
merge(*args) click to toggle source
# File lib/map.rb, line 361
def merge(*args)
  copy.update(*args)
end
method_missing(*args, &block) click to toggle source

a sane method missing that only supports writing values or reading *previously set* values

# File lib/map.rb, line 637
def method_missing(*args, &block)
  method = args.first.to_s
  case method
    when /=$/
      key = args.shift.to_s.chomp('=')
      value = args.shift
      self[key] = value
    when /\?$/
      key = args.shift.to_s.chomp('?')
      self.has?( key )
    else
      key = method
      unless has_key?(key)
        return(block ? fetch(key, &block) : super(*args))
      end
      self[key]
  end
end
mongoize() click to toggle source
# File lib/map.rb, line 1170
def mongoize
  self
end
name_for(*args, &block) click to toggle source
# File lib/map/params.rb, line 26
def name_for(*args, &block)
  options = Map.options_for!(args)
  options[:value] = nil
  args.push(options)
  param_for(*args, &block)
end
param_for(*args, &block) click to toggle source
# File lib/map/params.rb, line 2
def param_for(*args, &block)
  options = Map.options_for!(args)

  prefix = options[:prefix] || 'map'

  src_key = args.flatten.map{|arg| Map.alphanumeric_key_for(arg)}

  dst_key = src_key.map{|k| k.is_a?(Numeric) ? 0 : k}

  src = self
  dst = Map.new

  value =
    if options.has_key?(:value)
      options[:value]
    else
      src.get(src_key).to_s
    end

  dst.set(dst_key, value)

  Param.param_for(dst, prefix)
end
pop() click to toggle source
# File lib/map.rb, line 487
def pop
  unless empty?
    key = keys.last
    val = delete(key)
    [key, val]
  end
end
push(*args) click to toggle source
# File lib/map.rb, line 477
def push(*args)
  Map.each_pair(*args) do |key, val|
    key = convert_key(key)
    delete(key)
    keys.push(key)
    __set__(key, val)
  end
  self
end
reject(&block) click to toggle source
# File lib/map.rb, line 550
def reject(&block)
  dup.delete_if(&block)
end
reject!(&block) click to toggle source
# File lib/map.rb, line 554
def reject!(&block)
  hash = reject(&block)
  self == hash ? nil : hash
end
reorder(order = {}) click to toggle source

reordering support

# File lib/map.rb, line 523
def reorder(order = {})
  order = Map.for(order)
  map = Map.new
  keys = order.depth_first_keys | depth_first_keys
  keys.each{|key| map.set(key, get(key))}
  map
end
reorder!(order = {}) click to toggle source
# File lib/map.rb, line 531
def reorder!(order = {})
  replace(reorder(order))
end
replace(*args) click to toggle source
# File lib/map.rb, line 452
def replace(*args)
  clear
  update(*args)
end
respond_to?(method, *args, &block) click to toggle source
# File lib/map.rb, line 656
def respond_to?(method, *args, &block)
  has_key = has_key?(method)
  setter = method.to_s =~ /=\Z/
  !!((!has_key and setter) or has_key or super)
end
reverse_merge(hash) click to toggle source
# File lib/map.rb, line 366
def reverse_merge(hash)
  map = copy
  hash.each{|key, val| map[key] = val unless map.key?(key)}
  map
end
reverse_merge!(hash) click to toggle source
# File lib/map.rb, line 372
def reverse_merge!(hash)
  replace(reverse_merge(hash))
end
rm(*args) click to toggle source
# File lib/map.rb, line 965
def rm(*args)
  paths, path = args.partition{|arg| arg.is_a?(Array)}
  paths.push(path)

  paths.each do |p|
    if p.size == 1
      delete(*p)
      next
    end

    branch, leaf = p[0..-2], p[-1]
    collection = get(branch)

    case collection
      when Hash
        key = leaf
        collection.delete(key)
      when Array
        index = leaf
        collection.delete_at(index)
      else
        raise(IndexError, "(#{ collection.inspect }).rm(#{ p.inspect })")
    end
  end
  paths
end
select() click to toggle source
# File lib/map.rb, line 559
def select
  array = []
  each{|key, val| array << [key,val] if yield(key, val)}
  array
end
serialize(object) click to toggle source

for mongoid type system support

# File lib/map.rb, line 1154
def serialize(object)
  ::Map.for(object)
end
set(*args) click to toggle source
# File lib/map.rb, line 822
def set(*args)
  case
    when args.empty?
      return []
    when args.size == 1 && args.first.is_a?(Hash)
      hash = args.shift
    else
      hash = {}
      value = args.pop
      key = Array(args).flatten
      hash[key] = value
  end

  strategy = hash.map{|skey, svalue| [Array(skey), svalue]}

  strategy.each do |skey, svalue|
    leaf_for(skey, :autovivify => true) do |leaf, k|
      Map.collection_set(leaf, k, svalue)
    end
  end

  self
end
shift() click to toggle source

ordered container specific methods

# File lib/map.rb, line 459
def shift
  unless empty?
    key = keys.first
    val = delete(key)
    [key, val]
  end
end
stringify_keys() click to toggle source
# File lib/map.rb, line 626
def stringify_keys; dup end
stringify_keys!() click to toggle source

oh rails - would that map.rb existed before all this non-sense...

# File lib/map.rb, line 625
def stringify_keys!; self end
struct() click to toggle source
# File lib/map/struct.rb, line 45
def struct
  @struct ||= Struct.new(self)
end
symbolize_keys() click to toggle source
# File lib/map.rb, line 628
def symbolize_keys; dup end
symbolize_keys!() click to toggle source
# File lib/map.rb, line 627
def symbolize_keys!; self end
to_array() click to toggle source
# File lib/map.rb, line 604
def to_array
  array = []
  each{|*pair| array.push(pair)}
  array
end
to_hash() click to toggle source
# File lib/map.rb, line 588
def to_hash
  hash = Hash.new(default)
  each do |key, val|
    val = val.to_hash if val.respond_to?(:to_hash)
    hash[key] = val
  end
  hash
end
to_list() click to toggle source
# File lib/map.rb, line 611
def to_list
  list = []
  each_pair do |key, val|
    list[key.to_i] = val if(key.is_a?(Numeric) or key.to_s =~ /^\d+$/)
  end
  list
end
to_options() click to toggle source
# File lib/map.rb, line 630
def to_options; dup end
to_options!() click to toggle source
# File lib/map.rb, line 629
def to_options!; self end
to_params() click to toggle source
# File lib/map/params.rb, line 33
def to_params
  to_params = Array.new

  depth_first_each do |key, val|
    to_params.push(param_for(key))
  end

  to_params.join('&')
end
to_s() click to toggle source
# File lib/map.rb, line 619
def to_s
  to_array.to_s
end
to_yaml( opts = {} ) click to toggle source
# File lib/map.rb, line 597
def to_yaml( opts = {} )
  map = self
  YAML.quick_emit(self.object_id, opts){|out|
    out.map('!omap'){|m| map.each{|k,v| m.add(k, v)}}
  }
end
unshift(*args) click to toggle source
# File lib/map.rb, line 467
def unshift(*args)
  Map.each_pair(*args) do |key, val|
    key = convert_key(key)
    delete(key)
    keys.unshift(key)
    __set__(key, val)
  end
  self
end
update(*args) click to toggle source
# File lib/map.rb, line 355
def update(*args)
  Map.each_pair(*args){|key, val| store(key, val)}
  self
end
values() click to toggle source
# File lib/map.rb, line 376
def values
  array = []
  keys.each{|key| array.push(self[key])}
  array
end
values_at(*keys) click to toggle source
# File lib/map.rb, line 383
def values_at(*keys)
  keys.map{|key| self[key]}
end
with_indifferent_access() click to toggle source
# File lib/map.rb, line 632
def with_indifferent_access; dup end
with_indifferent_access!() click to toggle source
# File lib/map.rb, line 631
def with_indifferent_access!; self end

[Validate]

Generated with the Darkfish Rdoc Generator 2.