Class: Hash

Inherits:
Object show all
Defined in:
lib/rdoba/a.rb,
lib/rdoba/dup.rb,
lib/rdoba/common.rb,
lib/rdoba/deploy.rb,
lib/rdoba/hashorder.rb

Defined Under Namespace

Classes: Each

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#orderObject

Returns the value of attribute order.



4
5
6
# File 'lib/rdoba/hashorder.rb', line 4

def order
  @order
end

Instance Method Details

#__dup__Object



35
# File 'lib/rdoba/dup.rb', line 35

alias :__dup__ :dup

#__each__Object



44
# File 'lib/rdoba/hashorder.rb', line 44

alias :__each__ :each

#__each_key__Object



46
# File 'lib/rdoba/hashorder.rb', line 46

alias :__each_key__ :each_key

#__each_pair__Object



45
# File 'lib/rdoba/hashorder.rb', line 45

alias :__each_pair__ :each_pair

#__each_value__Object



47
# File 'lib/rdoba/hashorder.rb', line 47

alias :__each_value__ :each_value

#deploy(vars = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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 = {}) ⇒ Object



8
9
10
11
# File 'lib/rdoba/deploy.rb', line 8

def deploy!(vars = {})
  self.replace deploy(vars)
  # TODO add variable copy
end

#disorderObject



40
41
42
# File 'lib/rdoba/hashorder.rb', line 40

def disorder
  @order = nil; self
end

#dup(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#each(&block) ⇒ Object



49
50
51
# File 'lib/rdoba/hashorder.rb', line 49

def each(&block)
  @order ? each_special(Hash::Each::General, &block) : __each__(&block)
end

#each_key(&block) ⇒ Object



57
58
59
# File 'lib/rdoba/hashorder.rb', line 57

def each_key(&block)
  @order ? each_special(Hash::Each::Key, &block) : __each_key__(&block)
end

#each_pair(&block) ⇒ Object



53
54
55
# File 'lib/rdoba/hashorder.rb', line 53

def each_pair(&block)
  @order ? each_special(Hash::Each::Pair, &block) : __each_pair__(&block)
end

#each_value(&block) ⇒ Object



61
62
63
# File 'lib/rdoba/hashorder.rb', line 61

def each_value(&block)
  @order ? each_special(Hash::Each::Value, &block) : __each_value__(&block)
end

#geta(index, options = {}) ⇒ Object

TODO => [] + class Index



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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[:

#reverseObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# 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!Object



154
155
156
# File 'lib/rdoba/common.rb', line 154

def reverse!
  replace(reverse)
end

#seta(index, value, options = {}) ⇒ Object

TODO => [] + class Index



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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[:

#|(inval) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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