class XMigra::DeclarativeSupport::StructureReader

Constants

EXTENSION_PREFIX

Attributes

children[R]
key_finder_proc[RW]
parent[R]

Public Class Methods

new(data, keypath=[]) click to toggle source
# File lib/xmigra/declarative_support.rb, line 8
def initialize(data, keypath=[])
  @keypath = keypath
  @data = data
  @children = []
  @used_keys = Set.new
end

Public Instance Methods

[](key) click to toggle source
# File lib/xmigra/declarative_support.rb, line 15
def [](key)
  result, result_key = item_and_keypath(key)
  
  case result
  when Hash
    result = StructureReader.new(result, result_key).tap do |r|
      r.parent = self
    end
  when Array
    raise "Invalid to fetch an array via [] -- use array_fetch"
  end
  
  @used_keys << key if @data.kind_of? Hash
  return result
end
array_fetch(key, key_finder_proc) click to toggle source
# File lib/xmigra/declarative_support.rb, line 45
def array_fetch(key, key_finder_proc)
  result, result_key = item_and_keypath(key)
  unless result.kind_of? Array
    raise ::TypeError, "Expected key for array"
  end
  
  @used_keys << key if @data.kind_of? Hash
  return StructureReader.new(result, result_key).tap do |r|
    r.parent = self
    r.key_finder_proc = key_finder_proc
  end
end
each() { |k, self| ... } click to toggle source
# File lib/xmigra/declarative_support.rb, line 63
def each
  to_enum(:each) unless block_given?
  
  if @data.kind_of? Hash
    @data.each_key {|k| yield k, self[k]}
  else
    (0...@data.length).each {|i| yield self[i]}
  end
end
each_extension(&blk) click to toggle source
# File lib/xmigra/declarative_support.rb, line 106
def each_extension(&blk)
  return to_enum(:each_extension) if blk.nil?
  
  if @data.kind_of? Hash
    @data.each_pair do |k, val|
      next unless k.kind_of?(String) and k.start_with?(EXTENSION_PREFIX)
      blk.call((@keypath + [k]).join('.'), val)
    end
  end
  
  children.each do |child|
    child.each_extension(&blk)
  end
end
each_unused_standard_key(&blk) click to toggle source
# File lib/xmigra/declarative_support.rb, line 121
def each_unused_standard_key(&blk)
  return to_enum(:each_unused_standard_key) if blk.nil?
  
  if @data.kind_of? Hash
    @data.each_key do |k|
      next if @used_keys.include?(k)
      next if k.kind_of?(String) && k.start_with?(EXTENSION_PREFIX)
      blk.call(@keypath + [k]).join('.')
    end
  end
  children.each {|child| child.each_unused_standard_key(&blk)}
end
eql?(other) click to toggle source
# File lib/xmigra/declarative_support.rb, line 82
def eql?(other)
  @data.eql?(other)
end
fetch(key, *args) click to toggle source
# File lib/xmigra/declarative_support.rb, line 31
def fetch(key, *args)
  if args.length > 1
    raise ArgumentError, "fetch takes 1 or 2 arguments"
  end
  
  if @data.has_key?(key)
    return self[key]
  elsif args.length == 1
    return args[0]
  else
    raise KeyError, "#{key.inspect} not present"
  end
end
hash() click to toggle source
# File lib/xmigra/declarative_support.rb, line 78
def hash
  @data.hash
end
join(sep=$,) click to toggle source
# File lib/xmigra/declarative_support.rb, line 102
def join(sep=$,)
  @data.join(sep)
end
keys() click to toggle source
# File lib/xmigra/declarative_support.rb, line 86
def keys
  @data.keys
end
kind_of?(klass) click to toggle source
Calls superclass method
# File lib/xmigra/declarative_support.rb, line 74
def kind_of?(klass)
  return super(klass) || @data.kind_of?(klass)
end
length() click to toggle source
# File lib/xmigra/declarative_support.rb, line 98
def length
  @data.length
end
raw_item(key) click to toggle source
# File lib/xmigra/declarative_support.rb, line 58
def raw_item(key)
  @used_keys << key if @data.kind_of? Hash
  return @data[key]
end
uniq() click to toggle source
# File lib/xmigra/declarative_support.rb, line 94
def uniq
  collect {|o| o}.uniq
end
values() click to toggle source
# File lib/xmigra/declarative_support.rb, line 90
def values
  @data.values
end

Protected Instance Methods

item_and_keypath(key) click to toggle source
# File lib/xmigra/declarative_support.rb, line 145
def item_and_keypath(key)
  item = @data[key]
  subkey = begin
    if @key_finder_proc
      @keypath + [@key_finder_proc.call(item)]
    else
      @keypath + [key]
    end
  end
  return item, subkey
end
parent=(new_val) click to toggle source
# File lib/xmigra/declarative_support.rb, line 138
def parent=(new_val)
  @parent.children.delete(self) if @parent
  @parent = new_val
  @parent.children << self if @parent
  new_val
end