Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Apr 21, 2010
0 parents commit a3e96d4
Show file tree
Hide file tree
Showing 37 changed files with 3,375 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
log
doc/rdoc
doc/ri
pkg
4 changes: 4 additions & 0 deletions HISTORY
@@ -0,0 +1,4 @@
== 1.0.0 // 2009-07-01

* Ported from Ruby Facets.

23 changes: 23 additions & 0 deletions LICENSE
@@ -0,0 +1,23 @@
The MIT License

Copyright (c) 2005 Thomas Sawyer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


29 changes: 29 additions & 0 deletions MANIFEST
@@ -0,0 +1,29 @@
#!mast -x Syckfile bin lib meta test [A-Z]*
lib/castinghash.rb
lib/dictionary.rb
lib/lrucache.rb
lib/memoizer.rb
lib/opencascade.rb
lib/opencollection.rb
lib/openhash.rb
lib/openobject.rb
lib/openquery.rb
lib/orderedhash.rb
lib/ostructable.rb
lib/stash.rb
lib/statichash.rb
meta/abstract
meta/authors
meta/created
meta/homepage
meta/license
meta/name
meta/project
meta/release
meta/repository
meta/summary
meta/version
RELEASE
LICENSE
README
HISTORY
47 changes: 47 additions & 0 deletions README
@@ -0,0 +1,47 @@
= Facets Hashery

* home: http://rubyworks.github.com/hashery
* work: http://github.com/rubyworks/hashery

== DESCRIPTION

Among Ruby Facets most common additions were an assortment
of Hash-like classes. To better support this collection of
of libraries it was deemed prudent to create a side project
specifically for them. Hence the Facets Hashery.

Included in this collect are the widely used OrderedHash,
the related but more featured Dictionary class, a number
of _open_ classes, similiar to the standard OpenStruct,
and variations of the standard Hash.


== RELEASE NOTES

Please see the HISTORY file.


== HOW TO USE

For usage information, see the individual library files include
in this collection.


== HOW TO INSTALL

To install with RubyGems simply open a console and type:

$ sudo gem install hashery

Tarball packages are available for manual site installations
via <a href="http://proutils.github.com/setup">Setup.rb</a>.


== LICENSE

Copyright (c) 2005,2010 Thomas Sawyer

This program is ditributed unser the terms of the MIT license.

See LICENSE file for details.

77 changes: 77 additions & 0 deletions Syckfile
@@ -0,0 +1,77 @@
---
email:
service : Email
file : ~
subject : ~
mailto : ruby-talk@ruby-lang.org
active : true

grancher:
service: grancher
active: true

gemcutter:
service: GemCutter
active: true

box:
service: Box
active : true
types : [gem, gz]
include: [bin, demo, lib, meta, plug, test, "[A-Z]*"]
exclude: ~
master : false

ridoc:
service: RIDoc
include: ~
exclude: ~
active : true

rdoc:
service : RDoc
template: newfish
include : ~
exclude : [ Syckfile ]
active : true

syntax:
service : Syntax
loadpath : ~
exclude : ~
active : false

testrb:
service : testrb
tests : ~
exclude : ~
loadpath : ~
requires : ~
live : false
active : false

dnote:
service : DNote
loadpath : ~
labels : ~
exclude : [work]
output : ~
format : ~
active : true

stats:
service : Stats
title : ~
loadpath : ~
exclude : ~
output : ~
active : true

vclog:
service : VClog
formats : [json]
#layout : rel # gnu
typed : false
output : ~
active : false

172 changes: 172 additions & 0 deletions lib/hashery/castinghash.rb
@@ -0,0 +1,172 @@
# CastingHash is just like Hash, except that all keys and values
# are passed through casting procedures.
#--
# TODO: Handle default_proc.
#++
class CastingHash < Hash

# Default key conversion procedure.
KEY_PROC = lambda{ |x| x } #.to_s }

# Default value conversion procedure.
VAL_PROC = lambda{ |x| x }

#
def self.[](hash)
s = new
hash.each{ |k,v| s[k] = v }
s
end

#
def initialize(hash, value_cast=nil, &key_cast)
@key_proc = key_cast || KEY_PROC
@value_proc = value_cast.to_proc || VAL_PROC
hash.each{ |k,v| self[k] = v }
end

#
def key_proc
@key_proc
end

#
def key_proc=(proc)
@key_proc = proc.to_proc
end

#
def value_proc
@value_proc
end

#
def value_proc=(proc)
@value_proc = proc.to_proc
end

#
def [](k)
super(key_proc[k])
end

#
def []=(k,v)
super(key_proc[k], value_proc[v])
end

#
def <<(other)
case other
when Hash
super(cast(other))
when Array
self[other[0]] = other[1]
else
raise ArgumentError
end
end

def fetch(k)
super(key_proc[k])
end

#
def store(k, v)
super(key_proc[k], value_proc[v])
end

#
def key?(k)
super(key_proc[k])
end

#
def has_key?(k)
super(key_proc[k])
end

# Synonym for Hash#rekey, but modifies the receiver in place (and returns it).
#
# foo = { :name=>'Gavin', :wife=>:Lisa }.to_stash
# foo.rekey!{ |k| k.upcase } #=> { "NAME"=>"Gavin", "WIFE"=>:Lisa }
# foo.inspect #=> { "NAME"=>"Gavin", "WIFE"=>:Lisa }
#
def rekey!(*args, &block)
# for backward comptability (DEPRECATE?).
block = args.pop.to_sym.to_proc if args.size == 1
if args.empty?
block = lambda{|k| k} unless block
keys.each do |k|
nk = block[k]
self[nk] = delete(k) #if nk
end
else
raise ArgumentError, "3 for 2" if block
to, from = *args
self[to] = delete(from) if has_key?(from)
end
self
end

#
def rekey(*args, &block)
dup.rekey!(*args, &block)
end

#
def delete(k)
super(key_proc[k])
end

#
def update(other)
super(cast(other))
end

# Same as #update.
def merge!(other)
super(cast(other))
end

#
def replace(other)
super(cast(other))
end

#
def values_at(*keys)
super(keys.map(&key_proc))
end

#
def to_hash
h = {}; each{ |k,v| h[k] = v }; h
end

#
alias_method :to_h, :to_hash

private

#
def cast(hash)
h
hash.each do |k,v|
h[key_proc[k]] = value_proc[v]
end
h
end

end


class Hash

# Convert a Hash to a Stash object.
def to_casting_hash(value_cast=nil, &key_cast)
CastingHash.new(self, value_cast, &key_cast)
end

end

0 comments on commit a3e96d4

Please sign in to comment.