Class: Map
- Inherits:
-
Hash
show all
- Includes:
- Ordering
- Defined in:
- lib/map/ordering.rb,
lib/map.rb,
lib/map/_lib.rb,
lib/map/options.rb
Overview
Map::Ordering
This module contains all ordering-related methods that use the @keys array to maintain insertion order. It is conditionally included in Map only when Ruby’s Hash class does not maintain insertion order (Ruby < 1.9), or when explicitly forced via ENV.
On Ruby 1.9+, Hash maintains insertion order natively, so these methods are not needed and Map delegates to Hash’s implementation for memory optimization.
Defined Under Namespace
Modules: Arguments, Options, Ordering
Constant Summary
collapse
- Dup =
instance_method(:dup)
- VERSION =
'8.0.0'
Class Method Summary
collapse
-
._explode(key, value, accum = {:branches => [], :leaves => []}) ⇒ Object
-
.add(*args) ⇒ Object
-
.add_conversion_method!(method) ⇒ 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
-
.convert_value(value) ⇒ Object
-
.define_conversion_method!(method) ⇒ Object
-
.demongoize(object) ⇒ Object
-
.dependencies ⇒ 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
-
.description ⇒ 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
-
.libs ⇒ Object
-
.load(*libs) ⇒ Object
-
.load_dependencies! ⇒ Object
-
.map_for(hash) ⇒ Object
-
.mapify(object) ⇒ Object
-
.match(haystack, needle) ⇒ Object
-
.new(*args, &block) ⇒ Object
(also: [])
allocate method moved to Map::Ordering module (conditionally included) When ordering module is not included (Ruby 1.9+), Hash.allocate is used.
-
.options_for(*args, &block) ⇒ Object
-
.options_for!(*args, &block) ⇒ Object
-
.pairs_for(enumerable, *args, &block) ⇒ Object
-
.repo ⇒ Object
-
.summary ⇒ Object
-
.tap(*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 needs key conversion.
-
#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)
Iterator methods delegate to Hash (ordered in 1.9+).
-
#each_key ⇒ Object
-
#each_value ⇒ Object
-
#each_with_index ⇒ Object
-
#extractable_options? ⇒ Boolean
for rails’ extract_options! compat.
-
#fetch(key, *keys, &block) ⇒ Object
-
#first ⇒ Object
first and last return [key, value] pairs Use keys array since Hash#first/Hash#last don’t exist in all Ruby versions.
-
#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
keys method moved to Map::Ordering module (conditionally included) When ordering module is not included (Ruby 1.9+), Hash#keys is used.
-
#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
-
#klass ⇒ Object
-
#last ⇒ Object
-
#leaf_for(key, options = {}, &block) ⇒ Object
-
#map_for(hash) ⇒ Object
-
#merge(*args) ⇒ Object
(also: #+)
-
#method_missing(*args, **kws, &block) ⇒ Object
a sane method missing that only supports writing values or reading *previously set* values.
-
#mongoize ⇒ 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
-
#stringify_keys ⇒ Object
-
#stringify_keys! ⇒ Object
oh rails - would that map.rb existed before all this non-sense…
-
#symbolize_keys ⇒ Object
-
#symbolize_keys! ⇒ Object
-
#to_array ⇒ Object
(also: #to_a)
-
#to_hash ⇒ Object
(also: #to_h)
-
#to_list ⇒ Object
-
#to_options ⇒ Object
-
#to_options! ⇒ Object
-
#to_s ⇒ Object
-
#to_yaml(*args, &block) ⇒ Object
-
#unshift(*args) ⇒ Object
-
#update(*args) ⇒ Object
(also: #merge!)
-
#values ⇒ Object
values uses Hash#values (ordered in 1.9+).
-
#values_at(*keys) ⇒ Object
-
#with_indifferent_access ⇒ Object
-
#with_indifferent_access! ⇒ Object
Methods included from Ordering
included, #keys
Constructor Details
#initialize(*args, &block) ⇒ Map
keys method moved to Map::Ordering module (conditionally included) When ordering module is not included (Ruby 1.9+), Hash#keys is used
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/map.rb', line 172
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, **kws, &block) ⇒ Object
a sane method missing that only supports writing values or reading *previously set* values
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
# File 'lib/map.rb', line 649
def method_missing(*args, **kws, &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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
|
# File 'lib/map.rb', line 907
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
932
933
934
935
936
937
938
939
|
# File 'lib/map.rb', line 932
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/map.rb', line 67
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
|
.alphanumeric_key_for(key) ⇒ Object
1037
1038
1039
1040
1041
1042
1043
|
# File 'lib/map.rb', line 1037
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
151
152
153
154
|
# File 'lib/map.rb', line 151
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
161
162
163
164
|
# File 'lib/map.rb', line 161
def bcall(*args, &block)
args = Map.args_for_arity(args, block.arity)
block.call(*args)
end
|
.blank?(value) ⇒ Boolean
744
745
746
747
748
749
750
751
752
753
754
755
756
757
|
# File 'lib/map.rb', line 744
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
|
# File 'lib/map.rb', line 1110
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
156
157
158
159
|
# File 'lib/map.rb', line 156
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
23
24
25
26
27
28
29
30
|
# File 'lib/map.rb', line 23
def coerce(other)
case other
when Map
other
else
allocate.update(other.to_hash)
end
end
|
.collection_has?(collection, key, &block) ⇒ Boolean
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
|
# File 'lib/map.rb', line 786
def Map.collection_has?(collection, key, &block)
has_key =
case collection
when Array
key = (Integer(key) rescue nil)
!!collection.fetch(key) rescue false
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
|
# File 'lib/map.rb', line 764
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
|
# File 'lib/map.rb', line 809
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
941
942
943
|
# File 'lib/map.rb', line 941
def Map.combine(*args)
Map.add(*args)
end
|
.conversion_methods ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/map.rb', line 38
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
225
226
227
|
# File 'lib/map.rb', line 225
def Map.convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end
|
.convert_value(value) ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/map.rb', line 248
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/map.rb', line 52
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
1179
1180
1181
|
# File 'lib/map.rb', line 1179
def Map.demongoize(object)
Map.for(object)
end
|
.dependencies ⇒ Object
42
43
44
45
|
# File 'lib/map/_lib.rb', line 42
def dependencies
{
}
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
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
|
# File 'lib/map.rb', line 1059
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
1076
1077
1078
1079
1080
|
# File 'lib/map.rb', line 1076
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
1082
1083
1084
1085
1086
|
# File 'lib/map.rb', line 1082
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
|
.description ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/map/_lib.rb', line 19
def description
<<~____
map.rb is a string/symbol indifferent ordered hash that works in all rubies.
out of the over 200 ruby gems i have written, this is the one i use
every day, in all my projects.
some may be accustomed to using ActiveSupport::HashWithIndiffentAccess
and, although there are some similarities, map.rb is more complete,
works without requiring a mountain of code, and has been in production
usage for over 15 years.
it has no dependencies, and suports a myriad of other, 'tree-ish'
operators that will allow you to slice and dice data like a giraffee
with a giant weed whacker.
____
end
|
.each_pair(*args, &block) ⇒ Object
iterate over arguments in pairs smartly.
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
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
|
# File 'lib/map.rb', line 84
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) ⇒ Object
1183
1184
1185
|
# File 'lib/map.rb', line 1183
def Map.evolve(object)
Map.for(object)
end
|
.explode(hash) ⇒ Object
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
|
# File 'lib/map.rb', line 889
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
16
17
18
19
20
21
|
# File 'lib/map.rb', line 16
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
551
552
553
554
555
|
# File 'lib/map.rb', line 551
def Map.from_hash(hash, order = nil)
map = Map.for(hash)
map.reorder!(order) if order
map
end
|
.intersection(a, b) ⇒ Object
141
142
143
144
145
|
# File 'lib/map.rb', line 141
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
1049
1050
1051
|
# File 'lib/map.rb', line 1049
def self.key_for(*keys)
return keys.flatten
end
|
.keys_for(enumerable) ⇒ Object
1135
1136
1137
1138
|
# File 'lib/map.rb', line 1135
def Map.keys_for(enumerable)
keys = enumerable.respond_to?(:keys) ? enumerable.keys : Array.new(enumerable.size){|i| i}
keys
end
|
.libdir(*args, &block) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/map/_lib.rb', line 47
def libdir(*args, &block)
@libdir ||= File.dirname(File.expand_path(__FILE__))
args.empty? ? @libdir : File.join(@libdir, *args)
ensure
if block
begin
$LOAD_PATH.unshift(@libdir)
block.call
ensure
$LOAD_PATH.shift
end
end
end
|
.libs ⇒ Object
37
38
39
40
|
# File 'lib/map/_lib.rb', line 37
def libs
%w[
]
end
|
.load(*libs) ⇒ Object
61
62
63
64
|
# File 'lib/map/_lib.rb', line 61
def load(*libs)
libs = libs.join(' ').scan(/[^\s+]+/)
libdir { libs.each { |lib| Kernel.load(lib) } }
end
|
.load_dependencies! ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/map/_lib.rb', line 66
def load_dependencies!
libs.each do |lib|
require lib
end
begin
require 'rubygems'
rescue LoadError
nil
end
has_rubygems = defined?(gem)
dependencies.each do |lib, dependency|
gem(*dependency) if has_rubygems
require(lib)
end
end
|
.map_for(hash) ⇒ Object
216
217
218
219
220
|
# File 'lib/map.rb', line 216
def Map.map_for(hash)
map = klass.coerce(hash)
map.default = hash.default
map
end
|
.mapify(object) ⇒ Object
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/map.rb', line 229
def Map.mapify(object)
case
when object.is_a?(Array)
object.each{|it| Map.mapify(it)}
when object.is_a?(Hash)
Map.for(object)
else
object
end
end
|
.match(haystack, needle) ⇒ Object
147
148
149
|
# File 'lib/map.rb', line 147
def match(haystack, needle)
intersection(haystack, needle) == needle
end
|
.new(*args, &block) ⇒ Object
Also known as:
[]
allocate method moved to Map::Ordering module (conditionally included) When ordering module is not included (Ruby 1.9+), Hash.allocate is used
9
10
11
12
13
14
|
# File 'lib/map.rb', line 9
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
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
|
# File 'lib/map.rb', line 1088
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
|
.repo ⇒ Object
9
10
11
|
# File 'lib/map/_lib.rb', line 9
def repo
'https://github.com/ahoward/map'
end
|
.summary ⇒ Object
13
14
15
16
17
|
# File 'lib/map/_lib.rb', line 13
def summary
<<~____
the perfect ruby data structure
____
end
|
.tap(*args, &block) ⇒ Object
32
33
34
35
36
|
# File 'lib/map.rb', line 32
def tap(*args, &block)
new(*args).tap do |map|
map.tap(&block) if block
end
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
5
6
7
|
# File 'lib/map/_lib.rb', line 5
def version
VERSION
end
|
Instance Method Details
#<=>(other) ⇒ Object
525
526
527
528
529
|
# File 'lib/map.rb', line 525
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
511
512
513
514
515
516
517
518
519
520
521
522
523
|
# File 'lib/map.rb', line 511
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
531
532
533
|
# File 'lib/map.rb', line 531
def =~(hash)
to_hash == klass.coerce(hash).to_hash
end
|
#[](key) ⇒ Object
327
328
329
330
|
# File 'lib/map.rb', line 327
def [](key)
key = convert_key(key)
__get__(key)
end
|
#[]=(key, val) ⇒ Object
Also known as:
store
320
321
322
323
|
# File 'lib/map.rb', line 320
def []=(key, val)
key, val = convert(key, val)
__set__(key, val)
end
|
#__get__ ⇒ Object
311
|
# File 'lib/map.rb', line 311
alias_method '__get__', '[]'
|
#__set__ ⇒ Object
310
|
# File 'lib/map.rb', line 310
alias_method '__set__', '[]='
|
#__update__ ⇒ Object
312
|
# File 'lib/map.rb', line 312
alias_method '__update__', 'update'
|
#add(*args) ⇒ Object
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
|
# File 'lib/map.rb', line 863
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) ⇒ Object
#apply(other) ⇒ Object
1030
1031
1032
1033
1034
1035
|
# File 'lib/map.rb', line 1030
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
759
760
761
762
|
# File 'lib/map.rb', line 759
def blank?(*keys)
return empty? if keys.empty?
!has?(*keys) or Map.blank?(get(*keys))
end
|
#breadth_first_each(*args, &block) ⇒ Object
1152
1153
1154
|
# File 'lib/map.rb', line 1152
def breadth_first_each(*args, &block)
Map.breadth_first_each(self, *args, &block)
end
|
#clear ⇒ Object
424
425
426
|
# File 'lib/map.rb', line 424
def clear
Hash.instance_method(:clear).bind(self).call
end
|
#clone ⇒ Object
296
297
298
|
# File 'lib/map.rb', line 296
def clone
copy
end
|
#collection_has?(*args, &block) ⇒ Boolean
805
806
807
|
# File 'lib/map.rb', line 805
def collection_has?(*args, &block)
Map.collection_has?(*args, &block)
end
|
#collection_key(*args, &block) ⇒ Object
782
783
784
|
# File 'lib/map.rb', line 782
def collection_key(*args, &block)
Map.collection_key(*args, &block)
end
|
#collection_set(*args, &block) ⇒ Object
835
836
837
|
# File 'lib/map.rb', line 835
def collection_set(*args, &block)
Map.collection_set(*args, &block)
end
|
#combine(*args, &block) ⇒ Object
949
950
951
952
953
|
# File 'lib/map.rb', line 949
def combine(*args, &block)
dup.tap do |map|
map.combine!(*args, &block)
end
end
|
#combine!(*args, &block) ⇒ Object
945
946
947
|
# File 'lib/map.rb', line 945
def combine!(*args, &block)
add(*args, &block)
end
|
#contains(other) ⇒ Object
Also known as:
contains?
1156
1157
1158
1159
1160
|
# File 'lib/map.rb', line 1156
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
586
587
588
|
# File 'lib/map.rb', line 586
def conversion_methods
self.class.conversion_methods
end
|
#convert(key, val) ⇒ Object
276
277
278
|
# File 'lib/map.rb', line 276
def convert(key, val)
[convert_key(key), convert_value(val)]
end
|
#convert_key(key) ⇒ Object
240
241
242
243
244
245
246
|
# File 'lib/map.rb', line 240
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
267
268
269
270
271
272
273
|
# File 'lib/map.rb', line 267
def convert_value(value)
if klass.respond_to?(:convert_value)
klass.convert_value(value)
else
Map.convert_value(value)
end
end
|
#copy ⇒ Object
280
281
282
283
284
285
286
287
288
|
# File 'lib/map.rb', line 280
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
300
301
302
|
# File 'lib/map.rb', line 300
def default(key = nil)
key.is_a?(Symbol) && include?(key = key.to_s) ? self[key] : super
end
|
#default=(value) ⇒ Object
304
305
306
|
# File 'lib/map.rb', line 304
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
delete needs key conversion
379
380
381
382
|
# File 'lib/map.rb', line 379
def delete(key)
key = convert_key(key)
super(key)
end
|
#delete_if(&block) ⇒ Object
433
434
435
436
437
438
439
440
441
442
443
444
|
# File 'lib/map.rb', line 433
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
1140
1141
1142
|
# File 'lib/map.rb', line 1140
def depth_first_each(*args, &block)
Map.depth_first_each(self, *args, &block)
end
|
#depth_first_keys(*args, &block) ⇒ Object
1144
1145
1146
|
# File 'lib/map.rb', line 1144
def depth_first_keys(*args, &block)
Map.depth_first_keys(self, *args, &block)
end
|
#depth_first_values(*args, &block) ⇒ Object
1148
1149
1150
|
# File 'lib/map.rb', line 1148
def depth_first_values(*args, &block)
Map.depth_first_values(self, *args, &block)
end
|
#deserialize(object) ⇒ Object
1175
1176
1177
|
# File 'lib/map.rb', line 1175
def deserialize(object)
::Map.for(object)
end
|
#dup ⇒ Object
292
293
294
|
# File 'lib/map.rb', line 292
def dup
copy
end
|
#each ⇒ Object
Also known as:
each_pair
Iterator methods delegate to Hash (ordered in 1.9+)
402
403
404
|
# File 'lib/map.rb', line 402
def each
Hash.instance_method(:each).bind(self).call{|k, v| yield(k, v)}
end
|
#each_key ⇒ Object
407
408
409
|
# File 'lib/map.rb', line 407
def each_key
Hash.instance_method(:each_key).bind(self).call{|k| yield(k)}
end
|
#each_value ⇒ Object
411
412
413
|
# File 'lib/map.rb', line 411
def each_value
Hash.instance_method(:each_value).bind(self).call{|v| yield(v)}
end
|
#each_with_index ⇒ Object
415
416
417
418
419
420
421
422
|
# File 'lib/map.rb', line 415
def each_with_index
i = 0
each do |k, v|
yield([k, v], i)
i += 1
end
self
end
|
for rails’ extract_options! compat
1165
1166
1167
|
# File 'lib/map.rb', line 1165
def
true
end
|
#fetch(key, *keys, &block) ⇒ Object
332
333
334
335
336
337
338
339
340
|
# File 'lib/map.rb', line 332
def fetch(key, *keys, &block)
keys.unshift(key)
if has?(*keys)
get(*keys)
else
Map.mapify(yield)
end
end
|
#first ⇒ Object
first and last return [key, value] pairs Use keys array since Hash#first/Hash#last don’t exist in all Ruby versions
386
387
388
389
|
# File 'lib/map.rb', line 386
def first
key = keys.first
[key, self[key]] if key
end
|
#forcing(forcing = nil, &block) ⇒ Object
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
|
# File 'lib/map.rb', line 1009
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
1025
1026
1027
1028
|
# File 'lib/map.rb', line 1025
def forcing?(forcing=nil)
@forcing ||= nil
@forcing == forcing
end
|
#get(*keys) ⇒ Object
support for compound key indexing and depth first iteration
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
# File 'lib/map.rb', line 687
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 else
Map.collection_key(collection, key)
end
end
|
#has?(*keys) ⇒ Boolean
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
# File 'lib/map.rb', line 721
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
679
680
681
682
683
|
# File 'lib/map.rb', line 679
def id
return self[:id] if has_key?(:id)
return self[:_id] if has_key?(:_id)
raise NoMethodError
end
|
#initialize_from_array(array) ⇒ Object
205
206
207
208
|
# File 'lib/map.rb', line 205
def initialize_from_array(array)
map = self
Map.each_pair(array){|key, val| map[key] = val}
end
|
#initialize_from_hash(hash) ⇒ Object
199
200
201
202
203
|
# File 'lib/map.rb', line 199
def initialize_from_hash(hash)
map = self
map.update(hash)
map.default = hash.default
end
|
#inspect(*args, &block) ⇒ Object
579
580
581
582
|
# File 'lib/map.rb', line 579
def inspect(*args, &block)
require 'pp' unless defined?(PP)
PP.pp(self, '')
end
|
#invert ⇒ Object
557
558
559
560
561
562
|
# File 'lib/map.rb', line 557
def invert
inverted = klass.allocate
inverted.default = self.default
keys.each{|key| inverted[self[key]] = key }
inverted
end
|
#keep_if(&block) ⇒ Object
447
448
449
450
451
452
|
# File 'lib/map.rb', line 447
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?
342
343
344
|
# File 'lib/map.rb', line 342
def key?(key)
super(convert_key(key))
end
|
#key_for(*keys) ⇒ Object
1053
1054
1055
|
# File 'lib/map.rb', line 1053
def key_for(*keys)
self.class.key_for(*keys)
end
|
#klass ⇒ Object
212
213
214
|
# File 'lib/map.rb', line 212
def klass
self.class
end
|
#last ⇒ Object
391
392
393
394
|
# File 'lib/map.rb', line 391
def last
key = keys.last
[key, self[key]] if key
end
|
#leaf_for(key, options = {}, &block) ⇒ Object
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
|
# File 'lib/map.rb', line 955
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
221
222
223
|
# File 'lib/map.rb', line 221
def map_for(hash)
klass.map_for(hash)
end
|
#merge(*args) ⇒ Object
Also known as:
+
355
356
357
|
# File 'lib/map.rb', line 355
def merge(*args)
copy.update(*args)
end
|
#mongoize ⇒ Object
1187
1188
1189
|
# File 'lib/map.rb', line 1187
def mongoize
self
end
|
#pop ⇒ Object
500
501
502
503
504
505
506
|
# File 'lib/map.rb', line 500
def pop
unless empty?
key = keys.last
val = delete(key)
[key, val]
end
end
|
#push(*args) ⇒ Object
493
494
495
496
497
498
|
# File 'lib/map.rb', line 493
def push(*args)
Map.each_pair(*args) do |key, val|
self[key] = val end
self
end
|
#reject(&block) ⇒ Object
564
565
566
|
# File 'lib/map.rb', line 564
def reject(&block)
dup.delete_if(&block)
end
|
#reject!(&block) ⇒ Object
568
569
570
571
|
# File 'lib/map.rb', line 568
def reject!(&block)
hash = reject(&block)
self == hash ? nil : hash
end
|
#reorder(order = {}) ⇒ Object
537
538
539
540
541
542
543
|
# File 'lib/map.rb', line 537
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
545
546
547
|
# File 'lib/map.rb', line 545
def reorder!(order = {})
replace(reorder(order))
end
|
#replace(*args) ⇒ Object
454
455
456
457
|
# File 'lib/map.rb', line 454
def replace(*args)
clear
update(*args)
end
|
#respond_to?(method, *args, &block) ⇒ Boolean
673
674
675
676
677
|
# File 'lib/map.rb', line 673
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
360
361
362
363
364
|
# File 'lib/map.rb', line 360
def reverse_merge(hash)
map = copy
hash.each{|key, val| map[key] = val unless map.key?(key)}
map
end
|
#reverse_merge!(hash) ⇒ Object
366
367
368
|
# File 'lib/map.rb', line 366
def reverse_merge!(hash)
replace(reverse_merge(hash))
end
|
#rm(*args) ⇒ Object
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
|
# File 'lib/map.rb', line 982
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 ⇒ Object
573
574
575
576
577
|
# File 'lib/map.rb', line 573
def select
array = []
each{|key, val| array << [key,val] if yield(key, val)}
array
end
|
#serialize(object) ⇒ Object
for mongoid type system support
1171
1172
1173
|
# File 'lib/map.rb', line 1171
def serialize(object)
::Map.for(object)
end
|
#set(*args) ⇒ Object
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
|
# File 'lib/map.rb', line 839
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 ⇒ Object
468
469
470
471
472
473
474
|
# File 'lib/map.rb', line 468
def shift
unless empty?
key = keys.first
val = delete(key)
[key, val]
end
end
|
#stringify_keys ⇒ Object
638
|
# File 'lib/map.rb', line 638
def stringify_keys; dup end
|
#stringify_keys! ⇒ Object
oh rails - would that map.rb existed before all this non-sense…
637
|
# File 'lib/map.rb', line 637
def stringify_keys!; self end
|
#symbolize_keys ⇒ Object
640
|
# File 'lib/map.rb', line 640
def symbolize_keys; dup end
|
#symbolize_keys! ⇒ Object
639
|
# File 'lib/map.rb', line 639
def symbolize_keys!; self end
|
#to_array ⇒ Object
Also known as:
to_a
616
617
618
619
620
|
# File 'lib/map.rb', line 616
def to_array
array = []
each{|*pair| array.push(pair)}
array
end
|
#to_hash ⇒ Object
Also known as:
to_h
602
603
604
605
606
607
608
609
|
# File 'lib/map.rb', line 602
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
623
624
625
626
627
628
629
|
# File 'lib/map.rb', line 623
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
642
|
# File 'lib/map.rb', line 642
def to_options; dup end
|
#to_options! ⇒ Object
641
|
# File 'lib/map.rb', line 641
def to_options!; self end
|
#to_s ⇒ Object
631
632
633
|
# File 'lib/map.rb', line 631
def to_s
to_array.to_s
end
|
#to_yaml(*args, &block) ⇒ Object
612
613
614
|
# File 'lib/map.rb', line 612
def to_yaml(*args, &block)
to_hash.to_yaml(*args, &block)
end
|
#unshift(*args) ⇒ Object
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
# File 'lib/map.rb', line 476
def unshift(*args)
Map.each_pair(*args) do |key, val|
key = convert_key(key)
val = convert_value(val)
temp = {key => val}
each do |k, v|
temp[k] = v unless k == key
end
clear
temp.each{|k, v| __set__(k, v)}
end
self
end
|
#update(*args) ⇒ Object
Also known as:
merge!
349
350
351
352
|
# File 'lib/map.rb', line 349
def update(*args)
Map.each_pair(*args){|key, val| store(key, val)}
self
end
|
#values ⇒ Object
values uses Hash#values (ordered in 1.9+)
397
398
399
|
# File 'lib/map.rb', line 397
def values
Hash.instance_method(:values).bind(self).call
end
|
#values_at(*keys) ⇒ Object
429
430
431
|
# File 'lib/map.rb', line 429
def values_at(*keys)
keys.map{|key| self[key]}
end
|
#with_indifferent_access ⇒ Object
644
|
# File 'lib/map.rb', line 644
def with_indifferent_access; dup end
|
#with_indifferent_access! ⇒ Object
643
|
# File 'lib/map.rb', line 643
def with_indifferent_access!; self end
|