map 5.8.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/map.rb +104 -44
  2. data/map.gemspec +1 -1
  3. metadata +2 -2
data/lib/map.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  class Map < Hash
3
- Version = '5.8.0' unless defined?(Version)
3
+ Version = '6.0.0' unless defined?(Version)
4
4
  Load = Kernel.method(:load) unless defined?(Load)
5
5
 
6
6
  class << Map
@@ -648,6 +648,7 @@ class Map < Hash
648
648
  #
649
649
  def get(*keys)
650
650
  keys = key_for(keys)
651
+
651
652
  if keys.size <= 1
652
653
  if !self.has_key?(keys.first) && block_given?
653
654
  return yield
@@ -655,50 +656,51 @@ class Map < Hash
655
656
  return self[keys.first]
656
657
  end
657
658
  end
659
+
658
660
  keys, key = keys[0..-2], keys[-1]
659
661
  collection = self
662
+
660
663
  keys.each do |k|
661
- k = alphanumeric_key_for(k)
662
- if collection_has_key?(collection, k)
663
- collection = collection[k]
664
+ if Map.collection_has?(collection, k)
665
+ collection = Map.collection_key(collection, k)
664
666
  else
665
667
  collection = nil
666
668
  end
669
+
667
670
  unless collection.respond_to?('[]')
668
671
  leaf = collection
669
672
  return leaf
670
673
  end
671
674
  end
672
- alphanumeric_key = alphanumeric_key_for(key)
673
675
 
674
- if !collection_has_key?(collection, alphanumeric_key) && block_given?
675
- yield
676
+ if !Map.collection_has?(collection, key) && block_given?
677
+ default_value = yield
676
678
  else
677
- collection[alphanumeric_key]
679
+ Map.collection_key(collection, key)
678
680
  end
679
681
  end
680
682
 
681
683
  def has?(*keys)
682
684
  keys = key_for(keys)
683
685
  collection = self
684
- return collection_has_key?(collection, keys.first) if keys.size <= 1
686
+
687
+ return Map.collection_has?(collection, keys.first) if keys.size <= 1
688
+
685
689
  keys, key = keys[0..-2], keys[-1]
690
+
686
691
  keys.each do |k|
687
- k = alphanumeric_key_for(k)
688
- if collection_has_key?(collection, k)
689
- collection = collection[k]
692
+ if Map.collection_has?(collection, k)
693
+ collection = Map.collection_key(collection, k)
690
694
  else
691
695
  collection = nil
692
696
  end
697
+
693
698
  return collection unless collection.respond_to?('[]')
694
699
  end
700
+
695
701
  return false unless(collection.is_a?(Hash) or collection.is_a?(Array))
696
- collection_has_key?(collection, alphanumeric_key_for(key))
697
- end
698
702
 
699
- def blank?(*keys)
700
- return empty? if keys.empty?
701
- !has?(*keys) or Map.blank?(get(*keys))
703
+ Map.collection_has?(collection, key)
702
704
  end
703
705
 
704
706
  def Map.blank?(value)
@@ -716,13 +718,84 @@ class Map < Hash
716
718
  end
717
719
  end
718
720
 
719
- def collection_has_key?(collection, key)
721
+ def blank?(*keys)
722
+ return empty? if keys.empty?
723
+ !has?(*keys) or Map.blank?(get(*keys))
724
+ end
725
+
726
+ def Map.collection_key(collection, key, &block)
720
727
  case collection
728
+ when Array
729
+ begin
730
+ key = Integer(key)
731
+ rescue
732
+ raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]")
733
+ end
734
+ collection[key]
735
+
721
736
  when Hash
722
- collection.has_key?(key)
737
+ collection[key]
738
+
739
+ else
740
+ raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]")
741
+ end
742
+ end
743
+
744
+ def collection_key(*args, &block)
745
+ Map.collection_key(*args, &block)
746
+ end
747
+
748
+ def Map.collection_has?(collection, key, &block)
749
+ has_key =
750
+ case collection
751
+ when Array
752
+ key = (Integer(key) rescue -1)
753
+ (0...collection.size).include?(key)
754
+
755
+ when Hash
756
+ collection.has_key?(key)
757
+
758
+ else
759
+ raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]")
760
+ end
761
+
762
+ block.call(key) if(has_key and block)
763
+
764
+ has_key
765
+ end
766
+
767
+ def collection_has?(*args, &block)
768
+ Map.collection_has?(*args, &block)
769
+ end
770
+
771
+ def Map.collection_set(collection, key, value, &block)
772
+ set_key = false
773
+
774
+ case collection
723
775
  when Array
724
- (0...collection.size).include?((Integer(key) rescue -1))
776
+ begin
777
+ key = Integer(key)
778
+ rescue
779
+ raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]=#{ value.inspect }")
780
+ end
781
+ set_key = true
782
+ collection[key] = value
783
+
784
+ when Hash
785
+ set_key = true
786
+ collection[key] = value
787
+
788
+ else
789
+ raise(IndexError, "(#{ collection.inspect })[#{ key.inspect }]=#{ value.inspect }")
725
790
  end
791
+
792
+ block.call(key) if(set_key and block)
793
+
794
+ [key, value]
795
+ end
796
+
797
+ def collection_set(*args, &block)
798
+ Map.collection_set(*args, &block)
726
799
  end
727
800
 
728
801
  def set(*args)
@@ -742,7 +815,7 @@ class Map < Hash
742
815
 
743
816
  strategy.each do |key, value|
744
817
  leaf_for(key, :autovivify => true) do |leaf, k|
745
- leaf[k] = value
818
+ Map.collection_set(leaf, k, value)
746
819
  end
747
820
  end
748
821
 
@@ -844,40 +917,24 @@ class Map < Hash
844
917
  def leaf_for(key, options = {}, &block)
845
918
  leaf = self
846
919
  key = Array(key).flatten
847
- k = alphanumeric_key_for(key.first)
920
+ k = key.first
848
921
 
849
922
  key.each_cons(2) do |a, b|
850
- a, b = alphanumeric_key_for(a), alphanumeric_key_for(b)
851
-
852
- exists = collection_has_key?(leaf, a)
923
+ exists = Map.collection_has?(leaf, a)
853
924
 
854
925
  case b
855
926
  when Numeric
856
927
  if options[:autovivify]
857
- leaf[a] = [] unless exists
858
- end
859
-
860
- case
861
- when Array, Hash
862
- nil
863
- else
864
- raise(IndexError, "(#{ leaf.inspect })[#{ a.inspect }][#{ b.inspect }]")
928
+ Map.collection_set(leaf, a, Array.new) unless exists
865
929
  end
866
930
 
867
931
  when String, Symbol
868
932
  if options[:autovivify]
869
- leaf[a] = Map.new unless exists
870
- end
871
-
872
- case
873
- when Hash
874
- nil
875
- else
876
- raise(IndexError, "(#{ leaf.inspect })[#{ a.inspect }][#{ b.inspect }]")
933
+ Map.collection_set(leaf, a, Map.new) unless exists
877
934
  end
878
935
  end
879
936
 
880
- leaf = leaf[a]
937
+ leaf = Map.collection_key(leaf, a)
881
938
  k = b
882
939
  end
883
940
 
@@ -940,8 +997,11 @@ class Map < Hash
940
997
  end
941
998
 
942
999
  def Map.alphanumeric_key_for(key)
943
- return key if Numeric===key
944
- key.to_s =~ %r/^\d+$/ ? Integer(key) : key
1000
+ return key if key.is_a?(Numeric)
1001
+
1002
+ digity, stringy, digits = %r/^(~)?(\d+)$/iomx.match(key).to_a
1003
+
1004
+ digity ? stringy ? String(digits) : Integer(digits) : key
945
1005
  end
946
1006
 
947
1007
  def alphanumeric_key_for(key)
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "map"
6
- spec.version = "5.8.0"
6
+ spec.version = "6.0.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "map"
9
9
  spec.description = "description: map kicks the ass"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: map
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.8.0
4
+ version: 6.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-22 00:00:00.000000000 Z
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'description: map kicks the ass'
15
15
  email: ara.t.howard@gmail.com