class Shard

Attributes

meta[R]

add attribute readers for meta and value hashes

values[R]

add attribute readers for meta and value hashes

Public Class Methods

new(*args) click to toggle source

class constructor, creates instaces for meta table, values array, and Dict2 struct using args

# File lib/shard.rb, line 7
def initialize(*args)
        args.map{|e| e.to_sym}

        @@elements=args.map{|e| e.to_sym}
        @Dict2=Struct.new(*@@elements)
        @meta=Struct.new(:size,:is_empty,:num_indentifiers,:identifiers,:creationStamp,:editStamp,:all)
        @meta=@meta.new(0,true,@@elements.length,@@elements,Time.new,nil,Hash.new)
        @values=[]

        update
end

Public Instance Methods

clean(index=nil,endnum=nil) click to toggle source

return cleaned array of k/v/u sets by index or range

# File lib/shard/formatting.rb, line 3
def clean(index=nil,endnum=nil)
        @@clean=[]
        if index
                if endnum
                        @values[index..endnum].each{|v| @@clean<<[*v]}
                else
                        @@clean<<@values[index][*v]
                end
        else
                @values.each{|v| @@clean<<[*v]}
        end

        return @@clean.length>1 ? @@clean : @@clean.flatten
end
corr() click to toggle source

fix incorrect entries

# File lib/shard/formatting.rb, line 29
def corr
        @values.each{|v|
                if not v.is_a?(Struct)
                        if v.length!=3||!v.is_a?(Array)
                                raise "segment #{v} is not a 3 value array."
                        end
                        @values<<@Dict2.new(v[0],v[1],v[2])
                end
                if v.is_a?(Array)
                        delete(@values.index(v),:pos)
                end
        }
        update
end
delete(type,val=nil,index=0,endnum=-1) click to toggle source

deletes an element from the values array based on type parameter

# File lib/shard/core.rb, line 16
def delete(type,val=nil,index=0,endnum=-1)
        if type
                @@pntr=@@elements.index(type.to_sym)
                @values[index..endnum].each{|e|
                        if e[@@pntr]==val
                                @values.delete(e)
                        end
                }
        elsif type==false
                if val!=nil
                        index,endnum=val,index
                end
                [index..endnum].to_a.each{|i| @values.delete(i)}
        else
                raise TypeError("No type supplied")
        end

        update
end
insert(pos=-1,*args) click to toggle source

insert method, adds a Dict2 struct to values at set position

# File lib/shard/core.rb, line 3
def insert(pos=-1,*args)
        if args.length==@@elements.length
                @values.insert(pos,@Dict2.new(*args))
        elsif args.length<@@elements.length
                raise "Not enough arguments to fill new Dict2 Struct. #{@@elements.length} required."
        else
                raise "Too many arguments supplied to fill new Dict2 Struct. #{@@elements.length} required."
        end

        update
end
lPos(element,index) click to toggle source

list element by single index

# File lib/shard/util.rb, line 35
def lPos(element,index)
        list(element,index,index)
end
Also aliased as: listPos
list(element,index=0,endnum=-1) click to toggle source

return element(s) from shard (optional range)

# File lib/shard/util.rb, line 19
def list(element,index=0,endnum=-1)
        if @@elements.include?(element)
                @@temp=[]
                @values[index..endnum].each{|v| @@temp<<v[element]}
                return @@temp
        else
                raise "Improper or no type supplied."
        end
end
listPos(element,index)

aliases

Alias for: lPos
pAll() click to toggle source

prune by all elements

# File lib/shard/util.rb, line 30
def pAll
        prune(*@@elements)
end
Also aliased as: pruneAll
prune(*type) click to toggle source

prune duplicates

# File lib/shard/formatting.rb, line 19
def prune(*type)
        type.each{|t|
                @@uniqs=[]
                @values.each{|v| @@uniqs.include?(v[t]) ? nil : @@uniqs<<v[t]}
                @values.each{|v| @@uniqs.include?(v[t]) ? @@uniqs.delete(v[t]) : delete(@values.index(v),:pos)}
        }
        update
end
pruneAll()
Alias for: pAll

Private Instance Methods

update() click to toggle source

updates meta hash

# File lib/shard/util.rb, line 6
        def update
        @meta.size=@values.length
        @meta.is_empty=@meta.size<0
        @meta.editStamp=Time.new
        @meta.all[:size]             = @meta.size
        @meta.all[:is_empty]         = @meta.is_empty
        @meta.all[:num_indentifiers] = @meta.num_indentifiers
        @meta.all[:identifiers]      = @meta.identifiers
        @meta.all[:creationStamp]    = @meta.creationStamp
        @meta.all[:editStamp]        = @meta.editStamp
end