Class: Map
- Inherits:
-
Hash
show all
- Defined in:
- lib/map.rb,
lib/map/params.rb,
lib/map/struct.rb,
lib/map/options.rb,
lib/map/integrations/active_record.rb
Defined Under Namespace
Modules: Arguments, Integrations, Options, Param
Classes: Struct
Constant Summary
collapse
- Version =
'6.4.2'
- Load =
Kernel.method(:load)
- Dup =
instance_method(:dup)
Class Method Summary
collapse
-
._explode(key, value, accum = {:branches => [], :leaves => []}) ⇒ Object
-
.add(*args) ⇒ Object
-
.add_conversion_method!(method) ⇒ Object
-
.allocate ⇒ Object
-
.alphanumeric_key_for(key) ⇒ Object
-
.args_for_arity(args, arity) ⇒ Object
-
.bcall(*args, &block) ⇒ Object
-
.blank?(value) ⇒ Boolean
-
.breadth_first_each(enumerable, accum = [], &block) ⇒ Object
-
.call(object, method, *args, &block) ⇒ Object
-
.coerce(other) ⇒ Object
-
.collection_has?(collection, key, &block) ⇒ Boolean
-
.collection_key(collection, key, &block) ⇒ Object
-
.collection_set(collection, key, value, &block) ⇒ Object
-
.combine(*args) ⇒ Object
-
.conversion_methods ⇒ Object
-
.convert_key(key) ⇒ Object
def self.convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end.
-
.convert_value(value) ⇒ Object
-
.define_conversion_method!(method) ⇒ Object
-
.demongoize(object) ⇒ Object
-
.depth_first_each(enumerable, path = [], accum = [], &block) ⇒ Object
TODO - technically this returns only leaves so the name isn’t quite right.
-
.depth_first_keys(enumerable, path = [], accum = [], &block) ⇒ Object
-
.depth_first_values(enumerable, path = [], accum = [], &block) ⇒ Object
-
.each_pair(*args, &block) ⇒ Object
iterate over arguments in pairs smartly.
-
.evolve(object) ⇒ Object
-
.explode(hash) ⇒ Object
-
.for(*args, &block) ⇒ Object
-
.from_hash(hash, order = nil) ⇒ Object
support for building ordered hasshes from a map’s own image.
-
.intersection(a, b) ⇒ Object
-
.key_for(*keys) ⇒ Object
-
.keys_for(enumerable) ⇒ Object
-
.libdir(*args, &block) ⇒ Object
-
.load(*args, &block) ⇒ Object
-
.map_for(hash) ⇒ Object
-
.match(haystack, needle) ⇒ Object
-
.new(*args, &block) ⇒ Object
(also: [])
-
.options_for(*args, &block) ⇒ Object
-
.options_for!(*args, &block) ⇒ Object
-
.pairs_for(enumerable, *args, &block) ⇒ Object
-
.struct(*args, &block) ⇒ Object
-
.update_options_for!(args, &block) ⇒ Object
-
.version ⇒ Object
Instance Method Summary
collapse
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
equality / sorting / matching support.
-
#=~(hash) ⇒ Object
-
#[](key) ⇒ Object
-
#[]=(key, val) ⇒ Object
(also: #store)
-
#__get__ ⇒ Object
-
#__set__ ⇒ Object
-
#__update__ ⇒ Object
-
#add(*args) ⇒ Object
-
#alphanumeric_key_for(key) ⇒ Object
-
#apply(other) ⇒ Object
-
#blank?(*keys) ⇒ Boolean
-
#breadth_first_each(*args, &block) ⇒ Object
-
#clear ⇒ Object
-
#clone ⇒ Object
-
#collection_has?(*args, &block) ⇒ Boolean
-
#collection_key(*args, &block) ⇒ Object
-
#collection_set(*args, &block) ⇒ Object
-
#combine(*args, &block) ⇒ Object
-
#combine!(*args, &block) ⇒ Object
-
#contains(other) ⇒ Object
(also: #contains?)
-
#conversion_methods ⇒ Object
-
#convert(key, val) ⇒ Object
-
#convert_key(key) ⇒ Object
-
#convert_value(value) ⇒ Object
(also: #convert_val)
-
#copy ⇒ Object
-
#default(key = nil) ⇒ Object
-
#default=(value) ⇒ Object
-
#delete(key) ⇒ Object
-
#delete_if(&block) ⇒ Object
-
#depth_first_each(*args, &block) ⇒ Object
-
#depth_first_keys(*args, &block) ⇒ Object
-
#depth_first_values(*args, &block) ⇒ Object
-
#deserialize(object) ⇒ Object
-
#dup ⇒ Object
-
#each ⇒ Object
(also: #each_pair)
-
#each_key ⇒ Object
-
#each_value ⇒ Object
-
#each_with_index ⇒ Object
-
#extractable_options? ⇒ Boolean
for rails’ extract_options! compat.
-
#fetch(key, *args, &block) ⇒ Object
-
#first ⇒ Object
-
#forcing(forcing = nil, &block) ⇒ Object
-
#forcing?(forcing = nil) ⇒ Boolean
-
#get(*keys) ⇒ Object
support for compound key indexing and depth first iteration.
-
#has?(*keys) ⇒ Boolean
-
#id ⇒ Object
-
#initialize(*args, &block) ⇒ Map
constructor
-
#initialize_from_array(array) ⇒ Object
-
#initialize_from_hash(hash) ⇒ Object
-
#inspect(*args, &block) ⇒ Object
-
#invert ⇒ Object
-
#keep_if(&block) ⇒ Object
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
-
#key_for(*keys) ⇒ Object
-
#keys ⇒ Object
-
#klass ⇒ Object
-
#last ⇒ Object
-
#leaf_for(key, options = {}, &block) ⇒ Object
-
#map_for(hash) ⇒ Object
-
#merge(*args) ⇒ Object
(also: #+)
-
#method_missing(*args, &block) ⇒ Object
a sane method missing that only supports writing values or reading *previously set* values.
-
#mongoize ⇒ Object
-
#name_for(*args, &block) ⇒ Object
-
#param_for(*args, &block) ⇒ Object
-
#pop ⇒ Object
-
#push(*args) ⇒ Object
-
#reject(&block) ⇒ Object
-
#reject!(&block) ⇒ Object
-
#reorder(order = {}) ⇒ Object
-
#reorder!(order = {}) ⇒ Object
-
#replace(*args) ⇒ Object
-
#respond_to?(method, *args, &block) ⇒ Boolean
-
#reverse_merge(hash) ⇒ Object
-
#reverse_merge!(hash) ⇒ Object
-
#rm(*args) ⇒ Object
-
#select ⇒ Object
-
#serialize(object) ⇒ Object
for mongoid type system support.
-
#set(*args) ⇒ Object
-
#shift ⇒ Object
ordered container specific methods.
-
#stringify_keys ⇒ Object
-
#stringify_keys! ⇒ Object
oh rails - would that map.rb existed before all this non-sense…
-
#struct ⇒ Object
-
#symbolize_keys ⇒ Object
-
#symbolize_keys! ⇒ Object
-
#to_array ⇒ Object
(also: #to_a)
-
#to_hash ⇒ Object
-
#to_list ⇒ Object
-
#to_options ⇒ Object
-
#to_options! ⇒ Object
-
#to_params ⇒ Object
-
#to_s ⇒ Object
-
#to_yaml(opts = {}) ⇒ Object
-
#unshift(*args) ⇒ Object
-
#update(*args) ⇒ Object
(also: #merge!)
-
#values ⇒ Object
(also: #vals)
-
#values_at(*keys) ⇒ Object
-
#with_indifferent_access ⇒ Object
-
#with_indifferent_access! ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ Map
Returns a new instance of Map.
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/map.rb', line 195
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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
a sane method missing that only supports writing values or reading *previously set* values
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
# File 'lib/map.rb', line 638
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
|
Class Method Details
._explode(key, value, accum = {:branches => [], :leaves => []}) ⇒ Object
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
|
# File 'lib/map.rb', line 891
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) ⇒ Object
916
917
918
919
920
921
922
923
|
# File 'lib/map.rb', line 916
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) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/map.rb', line 88
def add_conversion_method!(method)
if define_conversion_method!(method)
method = method.to_s.strip
raise ArguementError if method.empty?
module_eval(<<-__, __FILE__, __LINE__)
unless conversion_methods.include?(#{ method.inspect })
conversion_methods.unshift(#{ method.inspect })
end
__
true
else
false
end
end
|
.allocate ⇒ Object
29
30
31
32
33
34
|
# File 'lib/map.rb', line 29
def allocate
super.instance_eval do
@keys = []
self
end
end
|
.alphanumeric_key_for(key) ⇒ Object
1021
1022
1023
1024
1025
1026
1027
|
# File 'lib/map.rb', line 1021
def Map.alphanumeric_key_for(key)
return key if key.is_a?(Numeric)
digity, stringy, digits = %r/^(~)?(\d+)$/iomx.match(key).to_a
digity ? stringy ? String(digits) : Integer(digits) : key
end
|
.args_for_arity(args, arity) ⇒ Object
173
174
175
176
|
# File 'lib/map.rb', line 173
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) ⇒ Object
183
184
185
186
|
# File 'lib/map.rb', line 183
def bcall(*args, &block)
args = Map.args_for_arity(args, block.arity)
block.call(*args)
end
|
.blank?(value) ⇒ Boolean
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
# File 'lib/map.rb', line 728
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) ⇒ Object
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
|
# File 'lib/map.rb', line 1094
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) ⇒ Object
178
179
180
181
|
# File 'lib/map.rb', line 178
def call(object, method, *args, &block)
args = Map.args_for_arity(args, object.method(method).arity)
object.send(method, *args, &block)
end
|
.coerce(other) ⇒ Object
50
51
52
53
54
55
56
57
|
# 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) ⇒ Boolean
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
# File 'lib/map.rb', line 770
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) ⇒ Object
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
# File 'lib/map.rb', line 748
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) ⇒ Object
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
# File 'lib/map.rb', line 793
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) ⇒ Object
925
926
927
|
# File 'lib/map.rb', line 925
def Map.combine(*args)
Map.add(*args)
end
|
.conversion_methods ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/map.rb', line 59
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) ⇒ Object
def self.convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end
254
255
256
|
# File 'lib/map.rb', line 254
def self.convert_key(key)
key = key.kind_of?(Symbol) ? key.to_s : key
end
|
.convert_value(value) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/map.rb', line 266
def self.convert_value(value)
conversion_methods.each do |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) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/map.rb', line 73
def define_conversion_method!(method)
method = method.to_s.strip
raise ArguementError if method.empty?
module_eval(<<-__, __FILE__, __LINE__)
unless public_method_defined?(#{ method.inspect })
def #{ method }
self
end
true
else
false
end
__
end
|
.demongoize(object) ⇒ Object
1162
1163
1164
|
# File 'lib/map.rb', line 1162
def Map.demongoize(object)
Map.for(object)
end
|
.depth_first_each(enumerable, path = [], accum = [], &block) ⇒ Object
TODO - technically this returns only leaves so the name isn’t quite right. re-factor for 3.0
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
|
# File 'lib/map.rb', line 1043
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) ⇒ Object
1060
1061
1062
1063
1064
|
# File 'lib/map.rb', line 1060
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) ⇒ Object
1066
1067
1068
1069
1070
|
# File 'lib/map.rb', line 1066
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) ⇒ Object
iterate over arguments in pairs smartly.
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/map.rb', line 105
def each_pair(*args, &block)
size = args.size
parity = size % 2 == 0 ? :even : :odd
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|
key, val, *ignored = pair
block.call(key, val)
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) ⇒ Object
1166
1167
1168
|
# File 'lib/map.rb', line 1166
def Map.evolve(object)
Map.for(object)
end
|
.explode(hash) ⇒ Object
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
|
# File 'lib/map.rb', line 873
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) ⇒ Object
43
44
45
46
47
48
|
# 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) ⇒ Object
support for building ordered hasshes from a map’s own image
542
543
544
545
546
|
# File 'lib/map.rb', line 542
def Map.from_hash(hash, order = nil)
map = Map.for(hash)
map.reorder!(order) if order
map
end
|
.intersection(a, b) ⇒ Object
163
164
165
166
167
|
# File 'lib/map.rb', line 163
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) ⇒ Object
1033
1034
1035
|
# File 'lib/map.rb', line 1033
def self.key_for(*keys)
return keys.flatten
end
|
.keys_for(enumerable) ⇒ Object
1119
1120
1121
|
# File 'lib/map.rb', line 1119
def Map.keys_for(enumerable)
keys = enumerable.respond_to?(:keys) ? enumerable.keys : Array.new(enumerable.size){|i| i}
end
|
.libdir(*args, &block) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# 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) ⇒ Object
25
26
27
|
# File 'lib/map.rb', line 25
def load(*args, &block)
libdir{ Load.call(*args, &block) }
end
|
.map_for(hash) ⇒ Object
239
240
241
242
243
|
# File 'lib/map.rb', line 239
def Map.map_for(hash)
map = klass.coerce(hash)
map.default = hash.default
map
end
|
.match(haystack, needle) ⇒ Object
169
170
171
|
# File 'lib/map.rb', line 169
def match(haystack, needle)
intersection(haystack, needle) == needle
end
|
.new(*args, &block) ⇒ Object
Also known as:
[]
36
37
38
39
40
41
|
# File 'lib/map.rb', line 36
def new(*args, &block)
allocate.instance_eval do
initialize(*args, &block)
self
end
end
|
.options_for(*args, &block) ⇒ Object
160
161
162
|
# File 'lib/map/options.rb', line 160
def Map.options_for(*args, &block)
Map::Options.for(*args, &block)
end
|
.options_for!(*args, &block) ⇒ Object
164
165
166
|
# 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) ⇒ Object
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
# File 'lib/map.rb', line 1072
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) ⇒ Object
49
50
51
|
# File 'lib/map/struct.rb', line 49
def Map.struct(*args, &block)
new(*args, &block).struct
end
|
.update_options_for!(args, &block) ⇒ Object
168
169
170
171
|
# File 'lib/map/options.rb', line 168
def Map.update_options_for!(args, &block)
options = Map.options_for(args)
block.call(options)
end
|
.version ⇒ Object
7
8
9
|
# File 'lib/map.rb', line 7
def version
Map::Version
end
|
Instance Method Details
#<=>(other) ⇒ Object
516
517
518
519
520
|
# File 'lib/map.rb', line 516
def <=>(other)
cmp = keys <=> klass.coerce(other).keys
return cmp unless cmp.zero?
values <=> klass.coerce(other).values
end
|
#==(other) ⇒ Object
equality / sorting / matching support
502
503
504
505
506
507
508
509
510
511
512
513
514
|
# File 'lib/map.rb', line 502
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) ⇒ Object
522
523
524
|
# File 'lib/map.rb', line 522
def =~(hash)
to_hash == klass.coerce(hash).to_hash
end
|
#[](key) ⇒ Object
339
340
341
342
|
# File 'lib/map.rb', line 339
def [](key)
key = convert_key(key)
__get__(key)
end
|
#[]=(key, val) ⇒ Object
Also known as:
store
332
333
334
335
336
|
# File 'lib/map.rb', line 332
def []=(key, val)
key, val = convert(key, val)
keys.push(key) unless has_key?(key)
__set__(key, val)
end
|
#__get__ ⇒ Object
329
|
# File 'lib/map.rb', line 329
alias_method '__get__', '[]'
|
#__set__ ⇒ Object
328
|
# File 'lib/map.rb', line 328
alias_method '__set__', '[]='
|
#__update__ ⇒ Object
330
|
# File 'lib/map.rb', line 330
alias_method '__update__', 'update'
|
#add(*args) ⇒ Object
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
|
# File 'lib/map.rb', line 847
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 |key, type|
set(key, type.new) unless get(key).is_a?(type)
end
exploded[:leaves].each do |key, value|
set(key, value)
end
self
end
|
#alphanumeric_key_for(key) ⇒ Object
#apply(other) ⇒ Object
1014
1015
1016
1017
1018
1019
|
# File 'lib/map.rb', line 1014
def apply(other)
Map.for(other).depth_first_each do |keys, value|
set(keys => value) unless !get(keys).nil?
end
self
end
|
#blank?(*keys) ⇒ Boolean
743
744
745
746
|
# File 'lib/map.rb', line 743
def blank?(*keys)
return empty? if keys.empty?
!has?(*keys) or Map.blank?(get(*keys))
end
|
#breadth_first_each(*args, &block) ⇒ Object
1135
1136
1137
|
# File 'lib/map.rb', line 1135
def breadth_first_each(*args, &block)
Map.breadth_first_each(enumerable=self, *args, &block)
end
|
#clear ⇒ Object
427
428
429
430
|
# File 'lib/map.rb', line 427
def clear
keys.clear
super
end
|
#clone ⇒ Object
314
315
316
|
# File 'lib/map.rb', line 314
def clone
copy
end
|
#collection_has?(*args, &block) ⇒ Boolean
789
790
791
|
# File 'lib/map.rb', line 789
def collection_has?(*args, &block)
Map.collection_has?(*args, &block)
end
|
#collection_key(*args, &block) ⇒ Object
766
767
768
|
# File 'lib/map.rb', line 766
def collection_key(*args, &block)
Map.collection_key(*args, &block)
end
|
#collection_set(*args, &block) ⇒ Object
819
820
821
|
# File 'lib/map.rb', line 819
def collection_set(*args, &block)
Map.collection_set(*args, &block)
end
|
#combine(*args, &block) ⇒ Object
933
934
935
936
937
|
# File 'lib/map.rb', line 933
def combine(*args, &block)
dup.tap do |map|
map.combine!(*args, &block)
end
end
|
#combine!(*args, &block) ⇒ Object
929
930
931
|
# File 'lib/map.rb', line 929
def combine!(*args, &block)
add(*args, &block)
end
|
#contains(other) ⇒ Object
Also known as:
contains?
1139
1140
1141
1142
1143
|
# 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 ⇒ Object
577
578
579
|
# File 'lib/map.rb', line 577
def conversion_methods
self.class.conversion_methods
end
|
#convert(key, val) ⇒ Object
294
295
296
|
# File 'lib/map.rb', line 294
def convert(key, val)
[convert_key(key), convert_value(val)]
end
|
#convert_key(key) ⇒ Object
258
259
260
261
262
263
264
|
# File 'lib/map.rb', line 258
def convert_key(key)
if klass.respond_to?(:convert_key)
klass.convert_key(key)
else
Map.convert_key(key)
end
end
|
#convert_value(value) ⇒ Object
Also known as:
convert_val
285
286
287
288
289
290
291
|
# File 'lib/map.rb', line 285
def convert_value(value)
if klass.respond_to?(:convert_value)
klass.convert_value(value)
else
Map.convert_value(value)
end
end
|
#copy ⇒ Object
298
299
300
301
302
303
304
305
306
|
# File 'lib/map.rb', line 298
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) ⇒ Object
318
319
320
|
# File 'lib/map.rb', line 318
def default(key = nil)
key.is_a?(Symbol) && include?(key = key.to_s) ? self[key] : super
end
|
#default=(value) ⇒ Object
322
323
324
|
# File 'lib/map.rb', line 322
def default=(value)
raise ArgumentError.new("Map doesn't work so well with a non-nil default value!") unless value.nil?
end
|
#delete(key) ⇒ Object
421
422
423
424
425
|
# File 'lib/map.rb', line 421
def delete(key)
key = convert_key(key)
keys.delete(key)
super(key)
end
|
#delete_if(&block) ⇒ Object
432
433
434
435
436
437
438
439
440
441
442
443
|
# File 'lib/map.rb', line 432
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) ⇒ Object
1123
1124
1125
|
# File 'lib/map.rb', line 1123
def depth_first_each(*args, &block)
Map.depth_first_each(enumerable=self, *args, &block)
end
|
#depth_first_keys(*args, &block) ⇒ Object
1127
1128
1129
|
# File 'lib/map.rb', line 1127
def depth_first_keys(*args, &block)
Map.depth_first_keys(enumerable=self, *args, &block)
end
|
#depth_first_values(*args, &block) ⇒ Object
1131
1132
1133
|
# File 'lib/map.rb', line 1131
def depth_first_values(*args, &block)
Map.depth_first_values(enumerable=self, *args, &block)
end
|
#deserialize(object) ⇒ Object
1158
1159
1160
|
# File 'lib/map.rb', line 1158
def deserialize(object)
::Map.for(object)
end
|
#dup ⇒ Object
310
311
312
|
# File 'lib/map.rb', line 310
def dup
copy
end
|
#each ⇒ Object
Also known as:
each_pair
413
414
415
416
|
# File 'lib/map.rb', line 413
def each
keys.each{|key| yield(key, self[key])}
self
end
|
#each_key ⇒ Object
403
404
405
406
|
# File 'lib/map.rb', line 403
def each_key
keys.each{|key| yield(key)}
self
end
|
#each_value ⇒ Object
408
409
410
411
|
# File 'lib/map.rb', line 408
def each_value
keys.each{|key| yield self[key]}
self
end
|
#each_with_index ⇒ Object
398
399
400
401
|
# File 'lib/map.rb', line 398
def each_with_index
keys.each_with_index{|key, index| yield([key, self[key]], index)}
self
end
|
for rails’ extract_options! compat
1148
1149
1150
|
# File 'lib/map.rb', line 1148
def
true
end
|
#fetch(key, *args, &block) ⇒ Object
344
345
346
347
|
# File 'lib/map.rb', line 344
def fetch(key, *args, &block)
key = convert_key(key)
super(key, *args, &block)
end
|
#first ⇒ Object
388
389
390
|
# File 'lib/map.rb', line 388
def first
[keys.first, self[keys.first]]
end
|
#forcing(forcing = nil, &block) ⇒ Object
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
|
# File 'lib/map.rb', line 993
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) ⇒ Boolean
1009
1010
1011
1012
|
# File 'lib/map.rb', line 1009
def forcing?(forcing=nil)
@forcing ||= nil
@forcing == forcing
end
|
#get(*keys) ⇒ Object
support for compound key indexing and depth first iteration
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
|
# File 'lib/map.rb', line 671
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?
default_value = yield
else
Map.collection_key(collection, key)
end
end
|
#has?(*keys) ⇒ Boolean
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
# File 'lib/map.rb', line 705
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 ⇒ Object
663
664
665
666
667
|
# File 'lib/map.rb', line 663
def id
return self[:id] if has_key?(:id)
return self[:_id] if has_key?(:_id)
raise NoMethodError
end
|
#initialize_from_array(array) ⇒ Object
228
229
230
231
|
# File 'lib/map.rb', line 228
def initialize_from_array(array)
map = self
Map.each_pair(array){|key, val| map[key] = val}
end
|
#initialize_from_hash(hash) ⇒ Object
222
223
224
225
226
|
# File 'lib/map.rb', line 222
def initialize_from_hash(hash)
map = self
map.update(hash)
map.default = hash.default
end
|
#inspect(*args, &block) ⇒ Object
570
571
572
573
|
# File 'lib/map.rb', line 570
def inspect(*args, &block)
require 'pp' unless defined?(PP)
PP.pp(self, '')
end
|
#invert ⇒ Object
548
549
550
551
552
553
|
# File 'lib/map.rb', line 548
def invert
inverted = klass.allocate
inverted.default = self.default
keys.each{|key| inverted[self[key]] = key }
inverted
end
|
#keep_if(&block) ⇒ Object
446
447
448
449
450
451
|
# File 'lib/map.rb', line 446
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) ⇒ Boolean
Also known as:
include?, has_key?, member?
349
350
351
|
# File 'lib/map.rb', line 349
def key?(key)
super(convert_key(key))
end
|
#key_for(*keys) ⇒ Object
1037
1038
1039
|
# File 'lib/map.rb', line 1037
def key_for(*keys)
self.class.key_for(*keys)
end
|
#keys ⇒ Object
191
192
193
|
# File 'lib/map.rb', line 191
def keys
@keys ||= []
end
|
#klass ⇒ Object
235
236
237
|
# File 'lib/map.rb', line 235
def klass
self.class
end
|
#last ⇒ Object
392
393
394
|
# File 'lib/map.rb', line 392
def last
[keys.last, self[keys.last]]
end
|
#leaf_for(key, options = {}, &block) ⇒ Object
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
|
# File 'lib/map.rb', line 939
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) ⇒ Object
244
245
246
|
# File 'lib/map.rb', line 244
def map_for(hash)
klass.map_for(hash)
end
|
#merge(*args) ⇒ Object
Also known as:
+
362
363
364
|
# File 'lib/map.rb', line 362
def merge(*args)
copy.update(*args)
end
|
#mongoize ⇒ Object
1170
1171
1172
|
# File 'lib/map.rb', line 1170
def mongoize
self
end
|
#name_for(*args, &block) ⇒ Object
26
27
28
29
30
31
|
# 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) ⇒ Object
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# 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 ⇒ Object
492
493
494
495
496
497
498
|
# File 'lib/map.rb', line 492
def pop
unless empty?
key = keys.last
val = delete(key)
[key, val]
end
end
|
#push(*args) ⇒ Object
480
481
482
483
484
485
486
487
488
489
490
|
# File 'lib/map.rb', line 480
def push(*args)
Map.each_pair(*args) do |key, val|
if key?(key)
delete(key)
else
keys.push(key)
end
__set__(key, val)
end
self
end
|
#reject(&block) ⇒ Object
555
556
557
|
# File 'lib/map.rb', line 555
def reject(&block)
dup.delete_if(&block)
end
|
#reject!(&block) ⇒ Object
559
560
561
562
|
# File 'lib/map.rb', line 559
def reject!(&block)
hash = reject(&block)
self == hash ? nil : hash
end
|
#reorder(order = {}) ⇒ Object
528
529
530
531
532
533
534
|
# File 'lib/map.rb', line 528
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 = {}) ⇒ Object
536
537
538
|
# File 'lib/map.rb', line 536
def reorder!(order = {})
replace(reorder(order))
end
|
#replace(*args) ⇒ Object
453
454
455
456
|
# File 'lib/map.rb', line 453
def replace(*args)
clear
update(*args)
end
|
#respond_to?(method, *args, &block) ⇒ Boolean
657
658
659
660
661
|
# File 'lib/map.rb', line 657
def respond_to?(method, *args, &block)
has_key = has_key?(method)
setter = method.to_s =~ /=\Z/o
!!((!has_key and setter) or has_key or super)
end
|
#reverse_merge(hash) ⇒ Object
367
368
369
370
371
|
# File 'lib/map.rb', line 367
def reverse_merge(hash)
map = copy
hash.each{|key, val| map[key] = val unless map.key?(key)}
map
end
|
#reverse_merge!(hash) ⇒ Object
373
374
375
|
# File 'lib/map.rb', line 373
def reverse_merge!(hash)
replace(reverse_merge(hash))
end
|
#rm(*args) ⇒ Object
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
|
# File 'lib/map.rb', line 966
def rm(*args)
paths, path = args.partition{|arg| arg.is_a?(Array)}
paths.push(path)
paths.each do |path|
if path.size == 1
delete(*path)
next
end
branch, leaf = path[0..-2], path[-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(#{ path.inspect })")
end
end
paths
end
|
#select ⇒ Object
564
565
566
567
568
|
# File 'lib/map.rb', line 564
def select
array = []
each{|key, val| array << [key,val] if yield(key, val)}
array
end
|
#serialize(object) ⇒ Object
for mongoid type system support
1154
1155
1156
|
# File 'lib/map.rb', line 1154
def serialize(object)
::Map.for(object)
end
|
#set(*args) ⇒ Object
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
|
# File 'lib/map.rb', line 823
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{|key, value| [Array(key), value]}
strategy.each do |key, value|
leaf_for(key, :autovivify => true) do |leaf, k|
Map.collection_set(leaf, k, value)
end
end
self
end
|
#shift ⇒ Object
ordered container specific methods
460
461
462
463
464
465
466
|
# File 'lib/map.rb', line 460
def shift
unless empty?
key = keys.first
val = delete(key)
[key, val]
end
end
|
#stringify_keys ⇒ Object
627
|
# File 'lib/map.rb', line 627
def stringify_keys; dup end
|
#stringify_keys! ⇒ Object
oh rails - would that map.rb existed before all this non-sense…
626
|
# File 'lib/map.rb', line 626
def stringify_keys!; self end
|
#struct ⇒ Object
45
46
47
|
# File 'lib/map/struct.rb', line 45
def struct
@struct ||= Struct.new(map=self)
end
|
#symbolize_keys ⇒ Object
629
|
# File 'lib/map.rb', line 629
def symbolize_keys; dup end
|
#symbolize_keys! ⇒ Object
628
|
# File 'lib/map.rb', line 628
def symbolize_keys!; self end
|
#to_array ⇒ Object
Also known as:
to_a
605
606
607
608
609
|
# File 'lib/map.rb', line 605
def to_array
array = []
each{|*pair| array.push(pair)}
array
end
|
#to_hash ⇒ Object
589
590
591
592
593
594
595
596
|
# File 'lib/map.rb', line 589
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 ⇒ Object
612
613
614
615
616
617
618
|
# File 'lib/map.rb', line 612
def to_list
list = []
each_pair do |key, val|
list[key.to_i] = val if(key.is_a?(Numeric) or key.to_s =~ %r/^\d+$/)
end
list
end
|
#to_options ⇒ Object
631
|
# File 'lib/map.rb', line 631
def to_options; dup end
|
#to_options! ⇒ Object
630
|
# File 'lib/map.rb', line 630
def to_options!; self end
|
#to_params ⇒ Object
33
34
35
36
37
38
39
40
41
|
# 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 ⇒ Object
620
621
622
|
# File 'lib/map.rb', line 620
def to_s
to_array.to_s
end
|
#to_yaml(opts = {}) ⇒ Object
598
599
600
601
602
603
|
# File 'lib/map.rb', line 598
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) ⇒ Object
468
469
470
471
472
473
474
475
476
477
478
|
# File 'lib/map.rb', line 468
def unshift(*args)
Map.each_pair(*args) do |key, val|
if key?(key)
delete(key)
else
keys.unshift(key)
end
__set__(key, val)
end
self
end
|
#update(*args) ⇒ Object
Also known as:
merge!
356
357
358
359
|
# File 'lib/map.rb', line 356
def update(*args)
Map.each_pair(*args){|key, val| store(key, val)}
self
end
|
#values ⇒ Object
Also known as:
vals
377
378
379
380
381
|
# File 'lib/map.rb', line 377
def values
array = []
keys.each{|key| array.push(self[key])}
array
end
|
#values_at(*keys) ⇒ Object
384
385
386
|
# File 'lib/map.rb', line 384
def values_at(*keys)
keys.map{|key| self[key]}
end
|
#with_indifferent_access ⇒ Object
633
|
# File 'lib/map.rb', line 633
def with_indifferent_access; dup end
|
#with_indifferent_access! ⇒ Object
632
|
# File 'lib/map.rb', line 632
def with_indifferent_access!; self end
|