Class: Map
- Inherits:
-
Hash
- Object
- Hash
- Map
- Defined in:
- lib/map.rb,
lib/map/struct.rb,
lib/map/options.rb
Defined Under Namespace
Modules: Arguments, Options Classes: Struct
Constant Summary collapse
- Version =
'4.2.0'
- Load =
Kernel.method(:load)
- Dup =
instance_method(:dup)
Class Method Summary collapse
- .add_conversion_method!(method) ⇒ Object
- .allocate ⇒ Object
- .alphanumeric_key_for(key) ⇒ Object
- .blank?(value) ⇒ Boolean
- .breadth_first_each(enumerable, accum = [], &block) ⇒ Object
- .coerce(other) ⇒ Object
- .conversion_methods ⇒ Object
- .convert_key(key) ⇒ Object
- .convert_value(value) ⇒ Object
- .define_conversion_method!(method) ⇒ 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.
- .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
- .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
writer/reader methods.
- #__update__ ⇒ Object
- #alphanumeric_key_for(key) ⇒ Object
- #apply(other) ⇒ Object
- #as_hash ⇒ Object
- #blank?(*keys) ⇒ Boolean
- #breadth_first_each(*args, &block) ⇒ Object
- #class ⇒ Object
- #clear ⇒ Object
- #clone ⇒ Object
- #collection_has_key?(collection, key) ⇒ Boolean
- #contains(other) ⇒ Object (also: #contains?)
-
#conversion_methods ⇒ Object
conversions.
- #convert(key, val) ⇒ Object
- #convert_key(key) ⇒ Object
- #convert_value(value) ⇒ Object (also: #convert_val)
-
#copy ⇒ Object
maps are aggressive with copy operations.
- #default(key = nil) ⇒ Object
-
#delete(key) ⇒ Object
mutators.
- #delete_if ⇒ Object
- #depth_first_each(*args, &block) ⇒ Object
- #depth_first_keys(*args, &block) ⇒ Object
- #depth_first_values(*args, &block) ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #each_key ⇒ Object
- #each_value ⇒ Object
-
#each_with_index ⇒ Object
iterator methods.
-
#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
A new instance of Map.
- #initialize_from_array(array) ⇒ Object
- #initialize_from_hash(hash) ⇒ Object
- #inspect(*args, &block) ⇒ Object
- #invert ⇒ Object
- #key?(key) ⇒ Boolean (also: #include?, #has_key?, #member?)
-
#keys ⇒ Object
instance constructor.
-
#klass ⇒ Object
support methods.
- #last ⇒ Object
- #map_for(hash) ⇒ Object
- #merge(*args) ⇒ Object
-
#method_missing(*args, &block) ⇒ Object
a sane method missing that only supports writing values or reading *previously set* values.
- #pop ⇒ Object
- #push(*args) ⇒ Object
- #reject(&block) ⇒ Object
- #reject!(&block) ⇒ Object
-
#reorder(order = {}) ⇒ Object
reordering support.
- #reorder!(order = {}) ⇒ Object
- #replace(*args) ⇒ Object
- #reverse_merge(hash) ⇒ Object
- #reverse_merge!(hash) ⇒ Object
- #rm(*args) ⇒ Object
- #select ⇒ Object
- #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_s ⇒ Object
- #to_yaml(*args, &block) ⇒ 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.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/map.rb', line 179 def initialize(*args, &block) case args.size when 0 super(&block) when 1 first = args.first case first 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
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'lib/map.rb', line 604 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 else key = method unless has_key?(key) return(block ? fetch(*args, &block) : super(*args)) end self[key] end end |
Class Method Details
.add_conversion_method!(method) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/map.rb', line 87 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
28 29 30 31 32 33 |
# File 'lib/map.rb', line 28 def allocate super.instance_eval do @keys = [] self end end |
.alphanumeric_key_for(key) ⇒ Object
783 784 785 786 |
# File 'lib/map.rb', line 783 def Map.alphanumeric_key_for(key) return key if Numeric===key key.to_s =~ %r/^\d+$/ ? Integer(key) : key end |
.blank?(value) ⇒ Boolean
659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/map.rb', line 659 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
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 |
# File 'lib/map.rb', line 845 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 |
.coerce(other) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/map.rb', line 49 def coerce(other) case other when Map other else allocate.update(other.to_hash) end end |
.conversion_methods ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/map.rb', line 58 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
230 231 232 |
# File 'lib/map.rb', line 230 def self.convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end |
.convert_value(value) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/map.rb', line 241 def self.convert_value(value) conversion_methods.each do |method| #return convert_value(value.send(method)) if value.respond_to?(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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/map.rb', line 72 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 |
.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
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/map.rb', line 794 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
811 812 813 814 815 |
# File 'lib/map.rb', line 811 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
817 818 819 820 821 |
# File 'lib/map.rb', line 817 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.
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/map.rb', line 104 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 |
.for(*args, &block) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/map.rb', line 42 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
500 501 502 503 504 |
# File 'lib/map.rb', line 500 def Map.from_hash(hash, order = nil) map = Map.for(hash) map.reorder!(order) if order map end |
.intersection(a, b) ⇒ Object
162 163 164 165 166 |
# File 'lib/map.rb', line 162 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 |
.keys_for(enumerable) ⇒ Object
870 871 872 |
# File 'lib/map.rb', line 870 def Map.keys_for(enumerable) keys = enumerable.respond_to?(:keys) ? enumerable.keys : Array.new(enumerable.size){|i| i} end |
.libdir(*args, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/map.rb', line 10 def libdir(*args, &block) @libdir ||= File.(__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
24 25 26 |
# File 'lib/map.rb', line 24 def load(*args, &block) libdir{ Load.call(*args, &block) } end |
.map_for(hash) ⇒ Object
221 222 223 224 225 |
# File 'lib/map.rb', line 221 def Map.map_for(hash) map = klass.coerce(hash) map.default = hash.default map end |
.match(haystack, needle) ⇒ Object
168 169 170 |
# File 'lib/map.rb', line 168 def match(haystack, needle) intersection(haystack, needle) == needle end |
.new(*args, &block) ⇒ Object Also known as: []
35 36 37 38 39 40 |
# File 'lib/map.rb', line 35 def new(*args, &block) allocate.instance_eval do initialize(*args, &block) self end end |
.options_for(*args, &block) ⇒ Object
145 146 147 |
# File 'lib/map/options.rb', line 145 def Map.(*args, &block) Map::Options.for(*args, &block) end |
.options_for!(*args, &block) ⇒ Object
149 150 151 |
# File 'lib/map/options.rb', line 149 def Map.(*args, &block) Map::Options.for(*args, &block).pop end |
.pairs_for(enumerable, *args, &block) ⇒ Object
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/map.rb', line 823 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
45 46 47 |
# File 'lib/map/struct.rb', line 45 def Map.struct(*args, &block) new(*args, &block).struct end |
Instance Method Details
#<=>(other) ⇒ Object
474 475 476 477 478 |
# File 'lib/map.rb', line 474 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
460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/map.rb', line 460 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
480 481 482 |
# File 'lib/map.rb', line 480 def =~(hash) to_hash == klass.coerce(hash).to_hash end |
#[](key) ⇒ Object
314 315 316 |
# File 'lib/map.rb', line 314 def [](key) __get__(convert_key(key)) end |
#[]=(key, val) ⇒ Object Also known as: store
307 308 309 310 311 |
# File 'lib/map.rb', line 307 def []=(key, val) key, val = convert(key, val) keys.push(key) unless has_key?(key) __set__(key, val) end |
#__get__ ⇒ Object
304 |
# File 'lib/map.rb', line 304 alias_method '__get__', '[]' |
#__set__ ⇒ Object
writer/reader methods
303 |
# File 'lib/map.rb', line 303 alias_method '__set__', '[]=' |
#__update__ ⇒ Object
305 |
# File 'lib/map.rb', line 305 alias_method '__update__', 'update' |
#alphanumeric_key_for(key) ⇒ Object
788 789 790 |
# File 'lib/map.rb', line 788 def alphanumeric_key_for(key) Map.alphanumeric_key_for(key) end |
#apply(other) ⇒ Object
776 777 778 779 780 781 |
# File 'lib/map.rb', line 776 def apply(other) Map.for(other).depth_first_each do |keys, value| set(keys => value) unless !get(keys).nil? end self end |
#as_hash ⇒ Object
556 557 558 559 560 561 |
# File 'lib/map.rb', line 556 def as_hash @class = Hash yield ensure @class = nil end |
#blank?(*keys) ⇒ Boolean
654 655 656 657 |
# File 'lib/map.rb', line 654 def blank?(*keys) return empty? if keys.empty? !has?(*keys) or Map.blank?(get(*keys)) end |
#breadth_first_each(*args, &block) ⇒ Object
886 887 888 |
# File 'lib/map.rb', line 886 def breadth_first_each(*args, &block) Map.breadth_first_each(enumerable=self, *args, &block) end |
#class ⇒ Object
563 564 565 |
# File 'lib/map.rb', line 563 def class @class || super end |
#clear ⇒ Object
399 400 401 402 |
# File 'lib/map.rb', line 399 def clear keys.clear super end |
#clone ⇒ Object
293 294 295 |
# File 'lib/map.rb', line 293 def clone copy end |
#collection_has_key?(collection, key) ⇒ Boolean
674 675 676 677 678 679 680 681 682 |
# File 'lib/map.rb', line 674 def collection_has_key?(collection, key) case collection when Hash collection.has_key?(key) when Array return false unless key (0...collection.size).include?(Integer(key)) end end |
#contains(other) ⇒ Object Also known as: contains?
890 891 892 893 894 |
# File 'lib/map.rb', line 890 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
conversions
535 536 537 |
# File 'lib/map.rb', line 535 def conversion_methods self.class.conversion_methods end |
#convert(key, val) ⇒ Object
269 270 271 |
# File 'lib/map.rb', line 269 def convert(key, val) [convert_key(key), convert_value(val)] end |
#convert_key(key) ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/map.rb', line 233 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
260 261 262 263 264 265 266 |
# File 'lib/map.rb', line 260 def convert_value(value) if klass.respond_to?(:convert_value) klass.convert_value(value) else Map.convert_value(value) end end |
#copy ⇒ Object
maps are aggressive with copy operations. they are all deep copies. make a new one if you really want a shallow copy
TODO - fallback to shallow if objects cannot be marshal’d.…
277 278 279 280 281 282 283 284 285 |
# File 'lib/map.rb', line 277 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
297 298 299 |
# File 'lib/map.rb', line 297 def default(key = nil) key.is_a?(Symbol) && include?(key = key.to_s) ? self[key] : super end |
#delete(key) ⇒ Object
mutators
393 394 395 396 397 |
# File 'lib/map.rb', line 393 def delete(key) key = convert_key(key) keys.delete(key) super(key) end |
#delete_if ⇒ Object
404 405 406 407 408 409 |
# File 'lib/map.rb', line 404 def delete_if to_delete = [] keys.each{|key| to_delete.push(key) if yield(key)} to_delete.each{|key| delete(key)} self end |
#depth_first_each(*args, &block) ⇒ Object
874 875 876 |
# File 'lib/map.rb', line 874 def depth_first_each(*args, &block) Map.depth_first_each(enumerable=self, *args, &block) end |
#depth_first_keys(*args, &block) ⇒ Object
878 879 880 |
# File 'lib/map.rb', line 878 def depth_first_keys(*args, &block) Map.depth_first_keys(enumerable=self, *args, &block) end |
#depth_first_values(*args, &block) ⇒ Object
882 883 884 |
# File 'lib/map.rb', line 882 def depth_first_values(*args, &block) Map.depth_first_values(enumerable=self, *args, &block) end |
#dup ⇒ Object
289 290 291 |
# File 'lib/map.rb', line 289 def dup copy end |
#each ⇒ Object Also known as: each_pair
385 386 387 388 |
# File 'lib/map.rb', line 385 def each keys.each{|key| yield(key, self[key])} self end |
#each_key ⇒ Object
375 376 377 378 |
# File 'lib/map.rb', line 375 def each_key keys.each{|key| yield(key)} self end |
#each_value ⇒ Object
380 381 382 383 |
# File 'lib/map.rb', line 380 def each_value keys.each{|key| yield self[key]} self end |
#each_with_index ⇒ Object
iterator methods
370 371 372 373 |
# File 'lib/map.rb', line 370 def each_with_index keys.each_with_index{|key, index| yield([key, self[key]], index)} self end |
#extractable_options? ⇒ Boolean
for rails’ extract_options! compat
899 900 901 |
# File 'lib/map.rb', line 899 def true end |
#fetch(key, *args, &block) ⇒ Object
318 319 320 |
# File 'lib/map.rb', line 318 def fetch(key, *args, &block) super(convert_key(key), *args, &block) end |
#first ⇒ Object
360 361 362 |
# File 'lib/map.rb', line 360 def first [keys.first, self[keys.first]] end |
#forcing(forcing = nil, &block) ⇒ Object
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
# File 'lib/map.rb', line 755 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
771 772 773 774 |
# File 'lib/map.rb', line 771 def forcing?(forcing=nil) @forcing ||= nil @forcing == forcing end |
#get(*keys) ⇒ Object
support for compound key indexing and depth first iteration
627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/map.rb', line 627 def get(*keys) keys = keys.flatten return self[keys.first] if keys.size <= 1 keys, key = keys[0..-2], keys[-1] collection = self keys.each do |k| k = alphanumeric_key_for(k) collection = collection[k] return collection unless collection.respond_to?('[]') end collection[alphanumeric_key_for(key)] end |
#has?(*keys) ⇒ Boolean
640 641 642 643 644 645 646 647 648 649 650 651 652 |
# File 'lib/map.rb', line 640 def has?(*keys) keys = keys.flatten collection = self return collection_has_key?(collection, keys.first) if keys.size <= 1 keys, key = keys[0..-2], keys[-1] keys.each do |k| k = alphanumeric_key_for(k) collection = collection[k] return collection unless collection.respond_to?('[]') end return false unless(collection.is_a?(Hash) or collection.is_a?(Array)) collection_has_key?(collection, alphanumeric_key_for(key)) end |
#id ⇒ Object
620 621 622 623 |
# File 'lib/map.rb', line 620 def id raise NoMethodError unless has_key?(:id) self[:id] end |
#initialize_from_array(array) ⇒ Object
210 211 212 213 |
# File 'lib/map.rb', line 210 def initialize_from_array(array) map = self Map.each_pair(array){|key, val| map[key] = val} end |
#initialize_from_hash(hash) ⇒ Object
204 205 206 207 208 |
# File 'lib/map.rb', line 204 def initialize_from_hash(hash) map = self map.update(hash) map.default = hash.default end |
#inspect(*args, &block) ⇒ Object
528 529 530 531 |
# File 'lib/map.rb', line 528 def inspect(*args, &block) require 'pp' unless defined?(PP) PP.pp(self, '') end |
#invert ⇒ Object
506 507 508 509 510 511 |
# File 'lib/map.rb', line 506 def invert inverted = klass.allocate inverted.default = self.default keys.each{|key| inverted[self[key]] = key } inverted end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
322 323 324 |
# File 'lib/map.rb', line 322 def key?(key) super(convert_key(key)) end |
#keys ⇒ Object
instance constructor
175 176 177 |
# File 'lib/map.rb', line 175 def keys @keys ||= [] end |
#klass ⇒ Object
support methods
217 218 219 |
# File 'lib/map.rb', line 217 def klass self.class end |
#last ⇒ Object
364 365 366 |
# File 'lib/map.rb', line 364 def last [keys.last, self[keys.last]] end |
#map_for(hash) ⇒ Object
226 227 228 |
# File 'lib/map.rb', line 226 def map_for(hash) klass.map_for(hash) end |
#merge(*args) ⇒ Object
335 336 337 |
# File 'lib/map.rb', line 335 def merge(*args) copy.update(*args) end |
#pop ⇒ Object
450 451 452 453 454 455 456 |
# File 'lib/map.rb', line 450 def pop unless empty? key = keys.last val = delete(key) [key, val] end end |
#push(*args) ⇒ Object
438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/map.rb', line 438 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
513 514 515 |
# File 'lib/map.rb', line 513 def reject(&block) dup.delete_if(&block) end |
#reject!(&block) ⇒ Object
517 518 519 520 |
# File 'lib/map.rb', line 517 def reject!(&block) hash = reject(&block) self == hash ? nil : hash end |
#reorder(order = {}) ⇒ Object
reordering support
486 487 488 489 490 491 492 |
# File 'lib/map.rb', line 486 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
494 495 496 |
# File 'lib/map.rb', line 494 def reorder!(order = {}) replace(reorder(order)) end |
#replace(*args) ⇒ Object
411 412 413 414 |
# File 'lib/map.rb', line 411 def replace(*args) clear update(*args) end |
#reverse_merge(hash) ⇒ Object
339 340 341 342 343 |
# File 'lib/map.rb', line 339 def reverse_merge(hash) map = copy hash.each{|key, val| map[key] = val unless map.key?(key)} map end |
#reverse_merge!(hash) ⇒ Object
345 346 347 |
# File 'lib/map.rb', line 345 def reverse_merge!(hash) replace(reverse_merge(hash)) end |
#rm(*args) ⇒ Object
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
# File 'lib/map.rb', line 728 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
522 523 524 525 526 |
# File 'lib/map.rb', line 522 def select array = [] each{|key, val| array << [key,val] if yield(key, val)} array end |
#set(*args) ⇒ Object
684 685 686 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 720 721 722 723 724 725 726 |
# File 'lib/map.rb', line 684 def set(*args) if args.size == 1 and args.first.is_a?(Hash) spec = args.shift else spec = {} value = args.pop keys = args spec[keys] = value end spec.each do |keys, value| keys = Array(keys).flatten collection = self if keys.size <= 1 key = keys.first collection[key] = value next end key = nil keys.each_cons(2) do |a, b| a, b = alphanumeric_key_for(a), alphanumeric_key_for(b) case b when Numeric collection[a] ||= [] raise(IndexError, "(#{ collection.inspect })[#{ a.inspect }]=#{ value.inspect }") unless collection[a].is_a?(Array) when String, Symbol collection[a] ||= {} raise(IndexError, "(#{ collection.inspect })[#{ a.inspect }]=#{ value.inspect }") unless collection[a].is_a?(Hash) end collection = collection[a] key = b end collection[key] = value end return spec.values end |
#shift ⇒ Object
ordered container specific methods
418 419 420 421 422 423 424 |
# File 'lib/map.rb', line 418 def shift unless empty? key = keys.first val = delete(key) [key, val] end end |
#stringify_keys ⇒ Object
593 |
# File 'lib/map.rb', line 593 def stringify_keys; dup end |
#stringify_keys! ⇒ Object
oh rails - would that map.rb existed before all this non-sense…
592 |
# File 'lib/map.rb', line 592 def stringify_keys!; self end |
#struct ⇒ Object
41 42 43 |
# File 'lib/map/struct.rb', line 41 def struct @struct ||= Struct.new(map=self) end |
#symbolize_keys ⇒ Object
595 |
# File 'lib/map.rb', line 595 def symbolize_keys; dup end |
#symbolize_keys! ⇒ Object
594 |
# File 'lib/map.rb', line 594 def symbolize_keys!; self end |
#to_array ⇒ Object Also known as: to_a
571 572 573 574 575 |
# File 'lib/map.rb', line 571 def to_array array = [] each{|*pair| array.push(pair)} array end |
#to_hash ⇒ Object
547 548 549 550 551 552 553 554 |
# File 'lib/map.rb', line 547 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
578 579 580 581 582 583 584 |
# File 'lib/map.rb', line 578 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
597 |
# File 'lib/map.rb', line 597 def ; dup end |
#to_options! ⇒ Object
596 |
# File 'lib/map.rb', line 596 def ; self end |
#to_s ⇒ Object
586 587 588 |
# File 'lib/map.rb', line 586 def to_s to_array.to_s end |
#to_yaml(*args, &block) ⇒ Object
567 568 569 |
# File 'lib/map.rb', line 567 def to_yaml(*args, &block) as_hash{ super } end |
#unshift(*args) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/map.rb', line 426 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!
329 330 331 332 |
# File 'lib/map.rb', line 329 def update(*args) Map.each_pair(*args){|key, val| store(key, val)} self end |
#values ⇒ Object Also known as: vals
349 350 351 352 353 |
# File 'lib/map.rb', line 349 def values array = [] keys.each{|key| array.push(self[key])} array end |
#values_at(*keys) ⇒ Object
356 357 358 |
# File 'lib/map.rb', line 356 def values_at(*keys) keys.map{|key| self[key]} end |
#with_indifferent_access ⇒ Object
599 |
# File 'lib/map.rb', line 599 def with_indifferent_access; dup end |
#with_indifferent_access! ⇒ Object
598 |
# File 'lib/map.rb', line 598 def with_indifferent_access!; self end |