shard-container 0.6.8 → 0.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/shard/formatting.rb +6 -6
- data/lib/shard.rb +55 -82
- data/readme.md +39 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68945a79f70d617c69093a4eff7f7b6e4aad83fc
|
4
|
+
data.tar.gz: 55c03290f51c15778eb500e935a675b042265fae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7498be0290b087006beb3331c25785bb280adf16f1be7040ae6251c7fad781c0d63246948178134bb647a08fd528b58a94237c9ba9852edde5af9991f7487ae1
|
7
|
+
data.tar.gz: 2a133c018db0f5b9c9a7eb37c0662b8c754815458dfa7deb8ae87d1d859b44030b2d0cb852280acb676ef5b649b1c1036d1dbe6871455bec4dcdba61ec7b0a2d
|
data/lib/shard/formatting.rb
CHANGED
@@ -4,11 +4,9 @@ class Shard
|
|
4
4
|
@@clean=[]
|
5
5
|
if index
|
6
6
|
if endnum
|
7
|
-
@values[index..endnum].each{|v| @@clean<<[v
|
7
|
+
@values[index..endnum].each{|v| @@clean<<[*v]}
|
8
8
|
else
|
9
|
-
@@clean<<@values[index]
|
10
|
-
@@clean<<@values[index].val
|
11
|
-
@@clean<<@values[index].util
|
9
|
+
@@clean<<@values[index][*v]
|
12
10
|
end
|
13
11
|
else
|
14
12
|
@values.each{|v| @@clean<<[*v]}
|
@@ -19,11 +17,12 @@ class Shard
|
|
19
17
|
|
20
18
|
#prune duplicates
|
21
19
|
def prune(*type)
|
22
|
-
type.each
|
20
|
+
type.each{|t|
|
23
21
|
@@uniqs=[]
|
24
22
|
@values.each{|v| @@uniqs.include?(v[t]) ? nil : @@uniqs<<v[t]}
|
25
23
|
@values.each{|v| @@uniqs.include?(v[t]) ? @@uniqs.delete(v[t]) : delete(@values.index(v),:pos)}
|
26
|
-
|
24
|
+
}
|
25
|
+
update
|
27
26
|
end
|
28
27
|
|
29
28
|
#fix incorrect entries
|
@@ -39,5 +38,6 @@ class Shard
|
|
39
38
|
delete(@values.index(v),:pos)
|
40
39
|
end
|
41
40
|
}
|
41
|
+
update
|
42
42
|
end
|
43
43
|
end
|
data/lib/shard.rb
CHANGED
@@ -4,95 +4,75 @@ class Shard
|
|
4
4
|
#add attribute readers for meta and value hashes
|
5
5
|
attr_reader :meta,:values
|
6
6
|
|
7
|
-
#
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
#updates meta hash
|
8
|
+
private def update
|
9
|
+
@meta.size=@values.length
|
10
|
+
@meta.is_empty=@meta.size<0
|
11
|
+
@meta.editStamp=Time.new
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
#class constructor, creates instaces for meta table, values array, and Dict2 struct using args
|
16
|
+
def initialize(*args)
|
17
|
+
args.map{|e| e.to_sym}
|
18
|
+
|
19
|
+
@@elements=args.map{|e| e.to_sym}
|
20
|
+
@Dict2=Struct.new(*@@elements)
|
21
|
+
@meta=Struct.new(:size,:is_empty,:num_indentifiers,:identifiers,:creationStamp,:editStamp)
|
22
|
+
@meta=@meta.new(0,true,@@elements.length,@@elements,Time.new,Time.new)
|
15
23
|
@values=[]
|
24
|
+
|
25
|
+
update
|
16
26
|
end
|
27
|
+
|
28
|
+
#inset method, adds a Dict2 struct to values at set position
|
29
|
+
def insert(pos=-1,*args)
|
30
|
+
if args.length==@@elements.length
|
31
|
+
@values.insert(pos,@Dict2.new(*args))
|
32
|
+
elsif args.length<@@elements.length
|
33
|
+
raise "Not enough arguments to fill new Dict2 Struct. #{@@elements.length} required."
|
34
|
+
else
|
35
|
+
raise "Too many arguments supplied to fill new Dict2 Struct. #{@@elements.length} required."
|
36
|
+
end
|
17
37
|
|
18
|
-
|
19
|
-
def add(key=nil,val=nil,util=nil,pos=-1)
|
20
|
-
@values.insert(pos,@Dict2.new(key,val,util))
|
21
|
-
|
22
|
-
@meta.size=@values.length
|
23
|
-
@meta.is_empty=@meta.size<0
|
38
|
+
update
|
24
39
|
end
|
25
40
|
|
26
41
|
#deletes an element from the values array based on type parameter
|
27
|
-
def delete(
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
@values.delete(
|
42
|
+
def delete(type,val=nil,index=0,endnum=-1)
|
43
|
+
if type
|
44
|
+
@@pntr=@@elements.index(type.to_sym)
|
45
|
+
@values[index..endnum].each{|e|
|
46
|
+
if e[@@pntr]==val
|
47
|
+
@values.delete(e)
|
48
|
+
end
|
49
|
+
}
|
50
|
+
elsif type==false
|
51
|
+
if val!=nil
|
52
|
+
index,endnum=val,index
|
53
|
+
end
|
54
|
+
[index..endnum].to_a.each{|i| @values.delete(i)}
|
40
55
|
else
|
41
56
|
raise TypeError("No type supplied")
|
42
57
|
end
|
43
58
|
|
44
|
-
|
45
|
-
@meta.is_empty=@meta.size<0
|
59
|
+
update
|
46
60
|
end
|
47
61
|
|
48
62
|
#return element(s) from shard (optional range)
|
49
|
-
def list(element
|
50
|
-
if
|
63
|
+
def list(element,index=0,endnum=-1)
|
64
|
+
if @@elements.include?(element)
|
51
65
|
@@temp=[]
|
52
66
|
@values[index..endnum].each{|v| @@temp<<v[element]}
|
53
67
|
return @@temp
|
54
68
|
else
|
55
|
-
raise
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
#delete by key (alias method)
|
60
|
-
def delKey(element)
|
61
|
-
delete(element,:key)
|
62
|
-
end
|
63
|
-
|
64
|
-
#delete by value (alias method)
|
65
|
-
def delVal(element)
|
66
|
-
delete(element,:val)
|
67
|
-
end
|
68
|
-
|
69
|
-
#delete by position or range (alias)
|
70
|
-
def delPos(element,endnum=nil)
|
71
|
-
case endnum.class.name
|
72
|
-
when 'Fixnum'
|
73
|
-
endnum.downto(element){|i|
|
74
|
-
delete(i,:pos)
|
75
|
-
}
|
76
|
-
when 'NilClass'
|
77
|
-
delete(element,:pos)
|
69
|
+
raise "Improper or no type supplied."
|
78
70
|
end
|
79
71
|
end
|
80
72
|
|
81
|
-
#prune
|
82
|
-
def pKeys
|
83
|
-
prune(:key)
|
84
|
-
end
|
85
|
-
#prune shard by values
|
86
|
-
def pVals
|
87
|
-
prune(:val)
|
88
|
-
end
|
89
|
-
#prune shard by utils
|
90
|
-
def pUtils
|
91
|
-
prune(:util)
|
92
|
-
end
|
93
|
-
#prune all shard elements
|
73
|
+
#prune by all elements
|
94
74
|
def pAll
|
95
|
-
prune(
|
75
|
+
prune(*@@elements)
|
96
76
|
end
|
97
77
|
|
98
78
|
#list element by single index
|
@@ -100,20 +80,13 @@ class Shard
|
|
100
80
|
list(element,index,index)
|
101
81
|
end
|
102
82
|
|
103
|
-
#
|
104
|
-
alias :keyDelete :delKey
|
105
|
-
alias :valDelete :delVal
|
106
|
-
alias :indexDelete :delPos
|
107
|
-
#pruning aliases
|
108
|
-
alias :pruneKeys :pKeys
|
109
|
-
alias :pruneVals :pVals
|
110
|
-
alias :pruneUtils :pUtils
|
111
|
-
alias :pruneAll :pAll
|
112
|
-
#listing aliases
|
83
|
+
#aliases
|
113
84
|
alias :listPos :lPos
|
85
|
+
alias :pruneAll :pAll
|
114
86
|
end
|
115
87
|
|
116
|
-
x=Shard.new(:
|
117
|
-
x.
|
118
|
-
x.
|
119
|
-
|
88
|
+
x=Shard.new(:key,:val,:util)
|
89
|
+
x.insert(0,'foo','baz',123)
|
90
|
+
x.insert(0,'foo','bar',123)
|
91
|
+
x.delete(:val,'bar')
|
92
|
+
p x.clean
|
data/readme.md
CHANGED
@@ -6,50 +6,61 @@
|
|
6
6
|
> Shard is an intuitive container class that allows extended capabilities of Hashes and arrays, as well as extra methods for formatting and aliases.
|
7
7
|
|
8
8
|
## Methods
|
9
|
-
>
|
10
|
-
> A shard's initial
|
9
|
+
> ##### Initialization
|
10
|
+
> A shard's initial parameters are the symbols that you can access via method calls (ex `Shard.values[0].name`) and can be set to any non-iterable. For ease of documenting my code, we'll just use key/val/util.
|
11
11
|
>
|
12
|
-
> test1=Shard.new(
|
13
|
-
> test2=Shard.new('testShard') #different id than the test1
|
14
|
-
> test3=Shard.new(3)
|
12
|
+
> test1=Shard.new(key,val,util)
|
15
13
|
>
|
16
|
-
>
|
14
|
+
> ##### Inserting a new segment to a shard
|
17
15
|
> Adding to a shard can be done one of two ways.
|
18
16
|
>
|
19
|
-
> test=Shard.new(:
|
20
|
-
>
|
21
|
-
> test.
|
17
|
+
> test=Shard.new(:key,:val,:util) #initialize the shard
|
18
|
+
>
|
19
|
+
> test.insert(-1,'foo',:bar,123) #inserts at last position
|
20
|
+
> test.insert(0,'foo',:baz,123) #inserts at position 0
|
21
|
+
> test.values<<['foo',:bar,123] #this can be cleaned up later
|
22
22
|
>
|
23
|
-
>
|
24
|
-
> Deleting from a shard is also easily done.
|
23
|
+
> ##### Deleting shard elements
|
24
|
+
> Deleting from a shard is also easily done. The type parameter is automatically converted to a symbol, so it can be any non-iterable that matches an identifier.
|
25
25
|
>
|
26
|
-
> test=Shard.new(:
|
27
|
-
> test.add('foo',:bar,123)
|
26
|
+
> test=Shard.new(:key,:val,:util) #initialize the shard
|
27
|
+
> test.add(-1,'foo',:bar,123) #add to the shard
|
28
28
|
>
|
29
|
-
> test.
|
30
|
-
> test.
|
31
|
-
> test.
|
29
|
+
> test.delete('key','foo') #delete by key
|
30
|
+
> test.delete(:val,':bar') #delete by value
|
31
|
+
> test.delete(false,0,2) #delete everything from position 0 to 2
|
32
|
+
> test.delete(:util,123,0,2) #delete all utils that match 123 from position 0 to 2
|
32
33
|
>
|
33
|
-
>
|
34
|
+
> ##### Correction
|
34
35
|
> Shards also supply a few formatting methods.
|
35
36
|
>
|
36
|
-
> test=Shard.new(:
|
37
|
-
> x.values<<[1,2,3]
|
37
|
+
> test=Shard.new(:key,:val,:util) #initialize the shard
|
38
|
+
> x.values<<[1,2,3] #add to the shard manually
|
38
39
|
> => [[1, 2, 3]]
|
39
|
-
> test.corr
|
40
|
+
> test.corr #convert all manually inserted array to formatted Dict2 Structs
|
40
41
|
> => [#<struct key=1, val=2, util=3>]
|
41
42
|
>
|
42
|
-
>
|
43
|
-
> Pruning a shard
|
43
|
+
> ##### Pruning
|
44
|
+
> Pruning a shard down to its unique identifiers is accomplished using the following aliases, or a raw `Shard.prune` method call using any combination of the shard identifiers.
|
44
45
|
>
|
45
|
-
> Shard.
|
46
|
-
> Shard.
|
47
|
-
> Shard.
|
48
|
-
> Shard.pruneAll #prune by all parameters
|
46
|
+
> Shard.pAll #prune all
|
47
|
+
> Shard.pruneAll #alias for pAll
|
48
|
+
> Shard.prune(:key,:val) #prune both keys and values
|
49
49
|
>
|
50
|
-
>
|
51
|
-
> Cleaning a shard using the `.clean` method returns an Array (nested, if more than one
|
50
|
+
> ##### Cleaning shards
|
51
|
+
> Cleaning a shard using the `.clean` method returns an Array (nested, if more than one set is selected) of readable sets with all elements in their original types.
|
52
52
|
>
|
53
53
|
> Shard.clean #cleans and returns entire shard
|
54
54
|
> Shard.clean(0) #cleans and returns shard element at index 0
|
55
55
|
> Shard.clean(0,2) #cleans and returns shard elements at indexes 0 to 2
|
56
|
+
>
|
57
|
+
> ##### Listing
|
58
|
+
> Listing shard elements is done with the `.list` method, like so
|
59
|
+
>
|
60
|
+
> Shard.list(:key,0,2) #returns all keys between index 0 and 2
|
61
|
+
> Shard.listPos(:key,0) #returns the key at index 0
|
62
|
+
>
|
63
|
+
> ##### Meta tags
|
64
|
+
> Getting a shard's meta information is done with the following methods or aliases
|
65
|
+
>
|
66
|
+
>
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shard-container
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6
|
4
|
+
version: 0.7.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chitoge
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An intuitive new container class for everyone from the everyday programmer
|
14
14
|
to the expert rubyist.
|