class Hash

Attributes

order[R]

Public Instance Methods

__dup__(opts = {})
Alias for: dup
__each__(&block)
Alias for: each
__each_key__(&block)
Alias for: each_key
__each_pair__(&block)
Alias for: each_pair
__each_value__(&block)
Alias for: each_value
deploy(vars = {}) click to toggle source
# File lib/rdoba/deploy.rb, line 13
def deploy(vars = {})
  res = {}

  self.keys.sort do |x,y|
    if x =~ /=$/
      y =~ /=$/ ? x <=> y : -1
    else
      y !~ /=$/ ? x <=> y : 1
    end
  end.each do |key|

    if key =~ /(.*)=$/
      vars[$1] = self[key]
      next

    elsif key =~ /(.*)@$/
      sym = $1
      eval "res.class.co( :attr_accessor, :#{sym})"
      eval "res.#{sym} = self[key]"
      next

    elsif key =~ /^%([^%].*)/
      next $stderr.puts "Warning: undefined variable " +
          "#{$1.inspect} found. Ignoring..." unless vars.key?($1)
      var = vars[$1].dup
      if var.class == Hash
        res |= var.deploy(vars)
      elsif var.class == String
        res[var] = nil
      else
        raise "Undeployable hash #{$1} value class #{var.class}"
      end
      next

    elsif key =~ /^%%(.*)/
      key.replace $1.to_s
    end

    def deploy_value(value, vars)
      case value.class.to_sym
      when :String
        if value =~ /^%([^%].*)/
          begin; vars[$1].deploy(vars); rescue; nil end
        elsif value =~ /(.*)%([A-Za-z0-9_А-я]+)(.*)/
          a = [ $1.to_s, $2.to_s, $3.to_s ]
          a[1] = begin; vars[a[1]].deploy(vars).to_s; rescue; vars[a[1]] end
          a.join
        else
          value
        end
      when :Hash
        value.deploy(vars)
      when :Array
        value.map do |sub|
          deploy_value(sub, vars)
        end
      else
        value
      end
    end

    value = self[key]
    res[key] = deploy_value(value, vars)
  end
  res
end
deploy!(vars = {}) click to toggle source
# File lib/rdoba/deploy.rb, line 8
def deploy!(vars = {})
  self.replace deploy(vars)
  # TODO add variable copy
end
deploy_value(value, vars) click to toggle source
# File lib/rdoba/deploy.rb, line 51
def deploy_value(value, vars)
  case value.class.to_sym
  when :String
    if value =~ /^%([^%].*)/
      begin; vars[$1].deploy(vars); rescue; nil end
    elsif value =~ /(.*)%([A-Za-z0-9_А-я]+)(.*)/
      a = [ $1.to_s, $2.to_s, $3.to_s ]
      a[1] = begin; vars[a[1]].deploy(vars).to_s; rescue; vars[a[1]] end
      a.join
    else
      value
    end
  when :Hash
    value.deploy(vars)
  when :Array
    value.map do |sub|
      deploy_value(sub, vars)
    end
  else
    value
  end
end
disorder() click to toggle source
# File lib/rdoba/hashorder.rb, line 40
def disorder
  @order = nil; self
end
dup(opts = {}) click to toggle source
# File lib/rdoba/dup.rb, line 36
def dup(opts = {})
  if (opts.class == Hash ? opts.key?(:recursive) : opts.to_s.to_sym == :recursive)
    res = {}

    def sub_dup(value)
      if value.class.to_s =~ /(Hash|Array)/
        value.dup(:recursive)
      else
        begin
          value.dup
        rescue
          new = value
        end
      end
    end

    self.each do |key, value| res[ sub_dup(key) ] = sub_dup(value) end

    res
  elsif opts.empty?
    __dup__
  else
    raise "Unsupported option(s): #{opts.class == Hash ? opts.keys.join(', ') : opts}"
  end
end
Also aliased as: __dup__
each(&block) click to toggle source
# File lib/rdoba/hashorder.rb, line 49
def each(&block)
  @order ? each_special(Hash::Each::General, &block) : __each__(&block)
end
Also aliased as: __each__
each_key(&block) click to toggle source
# File lib/rdoba/hashorder.rb, line 57
def each_key(&block)
  @order ? each_special(Hash::Each::Key, &block) : __each_key__(&block)
end
Also aliased as: __each_key__
each_pair(&block) click to toggle source
# File lib/rdoba/hashorder.rb, line 53
def each_pair(&block)
  @order ? each_special(Hash::Each::Pair, &block) : __each_pair__(&block)
end
Also aliased as: __each_pair__
each_value(&block) click to toggle source
# File lib/rdoba/hashorder.rb, line 61
def each_value(&block)
  @order ? each_special(Hash::Each::Value, &block) : __each_value__(&block)
end
Also aliased as: __each_value__
geta(index, options = {}) click to toggle source
# File lib/rdoba/a.rb, line 49
def geta(index, options = {}) #TODO => [] + class Index
  dbp11 "[geta] <<< hash = #{self.inspect}, index = #{index.inspect}, options = #{options.inspect}"
  options[:сокр] ||= @сокр

  if index.class == Array
    return self if index == [] or index == ['']
    index = index.clone
    value = geta_value(index.shift, options)
    (value.class == Hash or value.class == Array) ? value.geta(index, options) : value
  else
    geta_value(index, options)
  end
end
order=(order) click to toggle source
# File lib/rdoba/hashorder.rb, line 34
def order=(order)
  return nil if order.class != Array

  @order = order
end
reverse() click to toggle source
# File lib/rdoba/common.rb, line 158
def reverse
  h = {}
  self.each_pair do |key, value|
    if h.key? value
      if h[value].class == Array
        h[value] << key
      else
        h[value] = [ h[value], key ]
      end
    else
      h[value] = key
    end
  end
  h
end
reverse!() click to toggle source
# File lib/rdoba/common.rb, line 154
def reverse!
  replace(reverse)
end
seta(index, value, options = {}) click to toggle source
# File lib/rdoba/a.rb, line 63
def seta(index, value, options = {}) #TODO => [] + class Index
  dbp11 "[seta] <<< index: #{index.inspect}, value: #{value.inspect}, options: #{options.inspect}"
  options[:сокр] ||= @сокр

  return self[index] = value if index.class != Array # TODO spec index

  back = 0
  index = index.reverse.map do |x|
    if x.empty?
      back += 1
      nil
    elsif back > 0
      back -= 1
      nil
    else
      x
    end
  end.compact.reverse
  dbp12 "[seta]> result index: #{index.inspect}"

  obj = nil
  o = self
  dbp14 "[seta]>> self: #{o.inspect}"
  set_idx = index.pop
  par_class = set_idx =~ /^\d+$/ ? Array : Hash
  par_idx = nil
  index.each do |idx|
    unless o
      dbp14 "[seta]>> parent idx: #{par_idx.inspect}, idx: #{idx.inspect}, parent obj: #{o.inspect}"
      o = idx =~ /^\d+$/ && [] || {}
      obj[par_idx] = o
    end
    obj = o
    o = (obj.class == Hash) ? obj.geta_value(idx, options) : obj[idx]
    dbp14 "[seta]>> cur idx: #{idx.inspect}, parent obj: #{obj.inspect}, obj: #{o.inspect}"
    unless o
      if idx == index.last
        o = par_class.new
        obj[idx] = o
      else
        par_idx = idx =~ /^\d+$/ && idx.to_i || idx
      end
    end
  end

  raise "Invalid path" unless o # TODO special exception

  o[set_idx] = value
end
sub_dup(value) click to toggle source
# File lib/rdoba/dup.rb, line 40
def sub_dup(value)
  if value.class.to_s =~ /(Hash|Array)/
    value.dup(:recursive)
  else
    begin
      value.dup
    rescue
      new = value
    end
  end
end
|(inval) click to toggle source
# File lib/rdoba/common.rb, line 136
def |(inval)
  res = self.dup
  inval.each_pair do |key, val|
    if val.class == res[key].class
      if val.class == Hash
        res[key] |= inval[key]
      elsif val.class == Array
        res[key].concat val
      else
        res[key] = val
      end
    else
      res[key] = val
    end
  end
  res
end

Protected Instance Methods

geta_value(cid, options = {}) click to toggle source
# File lib/rdoba/a.rb, line 28
def geta_value(cid, options = {})
  res = ((not cid) || cid.empty?) && self || self[cid] ||
      (options[:сокр] && (self[options[:сокр][cid]] || self[options[:сокр].reverse[cid]]))

  if not res and options[:try_regexp]
    self.keys.each do |key|
      break res = self[key] if key.rmatch(cid)
      if options[:сокр]
        options[:сокр].each_pair do |val1, val2|
          break res = self[key] if key.rmatch(cid.gsub(/#{val1}/, val2)) or
              key.rmatch(cid.gsub(/#{val2}/, val1))
        end
      end
    end
  end

  res
end

Private Instance Methods

each_special(spec) { |key, self| ... } click to toggle source
# File lib/rdoba/hashorder.rb, line 15
def each_special(spec)
  (@order | self.keys).each do |key|
    if self.has_key? key
      case spec
      when Hash::Each::General
        yield key, self[key]
      when Hash::Each::Pair
        yield key, self[key]
      when Hash::Each::Key
        yield key
      when Hash::Each::Value
        yield self[key]
      end
    end
  end
end