madx-metash 1.0.1

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.
Files changed (4) hide show
  1. data/README.textile +17 -0
  2. data/Rakefile +12 -0
  3. data/lib/metash.rb +49 -0
  4. metadata +55 -0
@@ -0,0 +1,17 @@
1
+ h1. Metash
2
+
3
+ Metash is a Ruby library that allow what I call "Meta Hashes".
4
+
5
+ A Metash is a kind of hash that allows method chaining to get the values
6
+ using a bit of method_missing magic. This gives a nice interface for Hashes.
7
+
8
+ h2. Example
9
+
10
+ <pre><code> c = Metash.new :key1 => "value1", :nested => {:key2 => "value2"}
11
+ c.key1 # => "value1"
12
+ c.nested.key2 # => "value2"
13
+ c.nested.key2 "Another value"
14
+ c.nested.key2 # => "Another value"
15
+ c.key3 "value3" # c.key3 # => "value3"
16
+ c.key4 # => nil
17
+ </code></pre>
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+
5
+ METASH_GEMSPEC = eval(File.read('metash.gemspec'))
6
+
7
+ Rake::GemPackageTask.new(METASH_GEMSPEC) do |pkg|
8
+ pkg.need_tar_bz2 = true
9
+ end
10
+ task :default => "pkg/#{METASH_GEMSPEC.name}-#{METASH_GEMSPEC.version}.gem" do
11
+ puts "generated latest version"
12
+ end
@@ -0,0 +1,49 @@
1
+ # A hash with method_missing implemented so you can make calls
2
+ # i.e.: hash.foo.bar => hash[:foo][:bar]
3
+ # hash.foo "bar" => hash[:foo] = "bar"
4
+ class Metash
5
+ instance_methods.each do |meth|
6
+ unless meth =~ /(^__)|(\?$)|^instance_eval$/
7
+ undef_method meth
8
+ end
9
+ end
10
+
11
+ def initialize(hash={})
12
+ @hash = recurse(hash)
13
+ end
14
+
15
+ alias_method :orig_method_missing, :method_missing
16
+
17
+ # Some method_missing magic is used to provide the nice interface.
18
+ def method_missing(meth, *args)
19
+ return nil if !@hash.key?(meth) && args.empty?
20
+ if args.empty?
21
+ @hash[meth]
22
+ else
23
+ @hash[meth] = args.size == 1 ? args.first : args
24
+ end
25
+ end
26
+
27
+ # Returns the internal hash, it may be useful
28
+ def __hash
29
+ @hash
30
+ end
31
+
32
+ # A Hash-like inspect
33
+ def inspect
34
+ strs = []
35
+ @hash.each do |key, val|
36
+ strs << "%s: %s" % [key, val.inspect]
37
+ end
38
+ "{%s}" % strs.join(', ')
39
+ end
40
+
41
+ private
42
+ def recurse(hash)
43
+ hash.each do |key,val|
44
+ hash[key] = val.instance_of?(Hash) ? Metash.new(val) : val
45
+ end
46
+ hash
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: madx-metash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "Fran\xC3\xA7ois Vaux"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Hash that allows method calls to get and set values
17
+ email: root+metash@yapok.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/metash.rb
26
+ - README.textile
27
+ - Rakefile
28
+ has_rdoc: false
29
+ homepage: http://yapok.org/
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: A Hash that allows method calls to get and set values
54
+ test_files: []
55
+