block_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in block_hash.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2011 Maurycy Pawlowski-Wieronski <maurycy@g.pl>
2
+
3
+ Permission to use, copy, modify, and distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,61 @@
1
+ = BlockHash
2
+
3
+ BlockHash evaluates a block to a hash.
4
+
5
+ ==Quickstart
6
+
7
+ The simplest usage:
8
+
9
+ config = BlockHash.evaluate do |o|
10
+ o.name "maurycy"
11
+ o.email "maurycy@g.pl"
12
+ end
13
+
14
+ => {:email=>"maurycy@g.pl", :name=>"maurycy"}
15
+
16
+ Please note that keys are symbols.
17
+
18
+ To set default values, try:
19
+
20
+ defaults = {
21
+ :name => "maurycy"
22
+ }
23
+
24
+ config = BlockHash.evaluate(defaults) do |o|
25
+ o.email "maurycy@g.pl"
26
+ end
27
+
28
+ => {:email=>"maurycy@g.pl", :name=>"maurycy"}
29
+
30
+ An example with a method:
31
+
32
+ class Application
33
+ acts_as_serious :how => :deadly
34
+
35
+ def self.configuration(&block)
36
+ @@config = BlockHash.evaluate({}, &block)
37
+ end
38
+ end
39
+
40
+ Application.configuration do |config|
41
+ config.format "XML"
42
+ config.rpc "XML-RPC"
43
+ end
44
+
45
+ Please note that values are overwritten, and only the last value is stored:
46
+
47
+ config = BlockHash.evaluate do |o|
48
+ o.name "moritz"
49
+ o.name "maurycy"
50
+ end
51
+
52
+ => {:name=>"maurycy"}
53
+
54
+ Of course, BlockHash removes a trailing '=':
55
+
56
+ config = BlockHash.evaluate do |o|
57
+ o.name "maurycy"
58
+ o.email = "maurycy@g.pl"
59
+ end
60
+
61
+ => {:email=>"maurycy@g.pl", :name=>"maurycy"}
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "block_hash/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "block_hash"
7
+ s.version = BlockHash::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Maurycy Pawlowski-Wieronski"]
10
+ s.email = ["maurycy@g.pl"]
11
+ s.homepage = "https://github.com/maurycy/block_hash"
12
+ s.summary = %q{Evaluate a block to a hash}
13
+
14
+ s.rubyforge_project = "block_hash"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
data/lib/block_hash.rb ADDED
@@ -0,0 +1,19 @@
1
+ class BlockHash
2
+
3
+ def self.evaluate(hash=nil, &block)
4
+ silly = self.new(hash)
5
+ block.call(silly) if block_given?
6
+ silly.hash
7
+ end
8
+
9
+ attr_accessor :hash
10
+
11
+ def initialize(h)
12
+ self.hash = h || {}
13
+ end
14
+
15
+ def method_missing(sym, *args, &block)
16
+ key = sym.to_s.gsub(/\=$/, '').to_sym
17
+ self.hash[key] = args.pop
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module BlockHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,104 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'ruby-debug'
4
+
5
+ require '../lib/block_hash'
6
+
7
+ class BlockHashTest < Test::Unit::TestCase
8
+
9
+ def test_evaluate
10
+ assert_equal({}, BlockHash.evaluate)
11
+ end
12
+
13
+ def test_evaluate_inheritance
14
+ assert_equal({1 => 2}, BlockHash.evaluate({1 => 2}))
15
+ end
16
+
17
+ def test_evaluate_with_block
18
+ config = BlockHash.evaluate do |o|
19
+ o.name "maurycy"
20
+ o.email "maurycy@g.pl"
21
+ end
22
+
23
+ assert config.is_a?(Hash)
24
+ assert_equal "maurycy", config[:name]
25
+ assert_equal "maurycy@g.pl", config[:email]
26
+ end
27
+
28
+ def test_evaluate_with_proc
29
+ block = proc {|o|
30
+ o.name "maurycy"
31
+ o.email "maurycy@g.pl"
32
+ }
33
+
34
+ config = BlockHash.evaluate(nil, &block)
35
+
36
+ assert config.is_a?(Hash)
37
+ assert_equal "maurycy", config[:name]
38
+ assert_equal "maurycy@g.pl", config[:email]
39
+ end
40
+
41
+ def test_evaluate_within_method
42
+
43
+ def sappy(&block)
44
+ BlockHash.evaluate(&block)
45
+ end
46
+
47
+ config = sappy do |o|
48
+ o.name "maurycy"
49
+ o.email "maurycy@g.pl"
50
+ end
51
+
52
+ assert config.is_a?(Hash)
53
+ assert_equal "maurycy", config[:name]
54
+ assert_equal "maurycy@g.pl", config[:email]
55
+ end
56
+
57
+ def test_evaluate_empty
58
+ config = BlockHash.evaluate do |o|
59
+ end
60
+
61
+ assert config.is_a?(Hash)
62
+ assert config.empty?
63
+ end
64
+
65
+ def test_evaluate_with_empty_param
66
+ config = BlockHash.evaluate do |o|
67
+ o.test
68
+ end
69
+
70
+ assert config.is_a?(Hash)
71
+ assert_equal nil, config[:test]
72
+ end
73
+
74
+ def test_evaluate_setter_filtered
75
+ config = BlockHash.evaluate do |o|
76
+ o.test = 'a'
77
+ end
78
+
79
+ assert config.is_a?(Hash)
80
+ assert_equal 'a', config[:test]
81
+ end
82
+
83
+ def test_evaluate_overwritten
84
+ config = BlockHash.evaluate do |o|
85
+ o.test = 'a'
86
+ o.test 'b'
87
+ end
88
+
89
+ assert config.is_a?(Hash)
90
+ assert_equal 'b', config[:test]
91
+ end
92
+
93
+ def test_evaluate_inheritance
94
+ config = {}
95
+ config[:test] = 'a'
96
+
97
+ config = BlockHash.evaluate(config) do |o|
98
+ o.test 'c'
99
+ end
100
+
101
+ assert config.is_a?(Hash)
102
+ assert_equal 'c', config[:test]
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: block_hash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Maurycy Pawlowski-Wieronski
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-28 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email:
24
+ - maurycy@g.pl
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - block_hash.gemspec
38
+ - lib/block_hash.rb
39
+ - lib/block_hash/version.rb
40
+ - test/block_hash_test.rb
41
+ has_rdoc: true
42
+ homepage: https://github.com/maurycy/block_hash
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: block_hash
71
+ rubygems_version: 1.6.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Evaluate a block to a hash
75
+ test_files:
76
+ - test/block_hash_test.rb