cwyckoff-hash_builder 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008-2009 David Brady github@shinybit.com
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,106 @@
1
+ = Hash Builder
2
+
3
+ Are you tired of ugly, cumbersome hashes cluttering up your Rails models and controllers. Well, Hash Builder is a simple tool for building hashes that allows you define hashes separate from your code, keeping your code clean and focused on its core responsibilities. The hashes themselves are then easy to organize and manipulate.
4
+
5
+
6
+ == Author
7
+
8
+ * Chris Wyckoff -- cwyckoff@leadmediapartners.com
9
+
10
+
11
+ == Examples
12
+
13
+
14
+ === Definitions
15
+
16
+ Let's say you want to build a hash of user contact information. In a separate file, set up a hash definition:
17
+
18
+ HashBuilder.define(:contact_info) do |b|
19
+
20
+ bldr.build_hash(:name) do
21
+ bldr[:title] = "Dr."
22
+ bldr[:f_name] = "Hawkeye"
23
+ bldr[:l_name] = "Pierce"
24
+ end
25
+
26
+ bldr.build_hash(:address) do
27
+ bldr[:street] = "123 Main St."
28
+ bldr[:city] = "Crabapple Cove"
29
+ bldr[:state] = "ME"
30
+ bldr[:zip] = "04656"
31
+ end
32
+
33
+ end
34
+
35
+
36
+ === Building
37
+
38
+ Each definition requires a key -- in this case :contact_info. Moreover, within the contact definition, you may set up namespaces (:name and :address) to further organize your hash content.
39
+
40
+ In order to build your hash, simply call
41
+
42
+ HashBuilder.build(:contact_info)
43
+
44
+ which will return
45
+
46
+ {
47
+ :title => "Dr.",
48
+ :f_name => "Hawkeye",
49
+ :l_name => "Pierce",
50
+ :street => "123 Main St.",
51
+ :city => "Crabapple Cove",
52
+ :state => "ME",
53
+ :zip => "04656",
54
+ }
55
+
56
+ If you want to build an individual namespace, call
57
+
58
+ HashBuilder.build_namespace(:contact_info, :address)
59
+
60
+ which will return only
61
+
62
+ {
63
+ :street => "123 Main St.",
64
+ :city => "Crabapple Cove",
65
+ :state => "ME",
66
+ :zip => "04656",
67
+ }
68
+
69
+ To select multiple namespaces, set them in a block:
70
+
71
+ HashBuilder.build(:contact_info) do |b|
72
+ b.using :name
73
+ b.using :address
74
+ end
75
+
76
+
77
+ === Additional Variables
78
+
79
+ You may pass additional variables to your hash definition when you build it. Simply pass them after the definition key.
80
+
81
+ HashBuilder.build(:contact_info, user)
82
+
83
+ For namespaces, pass them after the namespace key
84
+
85
+ HashBuilder.build(:contact_info, :address, user)
86
+
87
+ All of this assumes that you will now yield the additional variables in the hash definition:
88
+
89
+ HashBuilder.define(:contact_info) do |b|
90
+
91
+ bldr.build_hash(:name) do |user|
92
+ bldr[:title] = user.title
93
+ bldr[:f_name] = user.first_name
94
+ bldr[:l_name] = user.last_name
95
+ end
96
+
97
+ bldr.build_hash(:address) do |user|
98
+ bldr[:street] = user.street
99
+ bldr[:city] = user.city
100
+ bldr[:state] = user.state
101
+ bldr[:zip] = user.zip
102
+ end
103
+
104
+ end
105
+
106
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "hash_builder")
@@ -0,0 +1,91 @@
1
+ module HashBuilderExtensions
2
+ def with(key, *args)
3
+ self.update(HashBuilder.build(key, *args))
4
+ end
5
+ end
6
+
7
+ class Hash; include HashBuilderExtensions; end
8
+ class HashBuilderError < Exception; end
9
+
10
+ class HashBuilder
11
+
12
+ class << self
13
+
14
+ def define(key=nil)
15
+ raise HashBuilderError, "You need to pass in a hash builder key (e.g., HashBuilder.define(:foo))" if key.nil?
16
+ # raise HashBuilderError, "The hash builder key ':#{key}' is already being used." if builders[key.to_sym]
17
+
18
+ builders[key.to_sym] = self.new
19
+ yield builders[key.to_sym]
20
+ end
21
+
22
+ def build(key=nil, *args)
23
+ raise HashBuilderError, "You need to pass in a hash builder key (e.g., HashBuilder.build(:foo))" if key.nil?
24
+ raise HashBuilderError, "Unable to find hash builder definition for key #{key}" unless (builder = builders[key.to_sym])
25
+
26
+ yield builder if block_given?
27
+ builder.call(*args)
28
+ end
29
+
30
+ def build_namespace(key, namespace, *args)
31
+ raise HashBuilderError, "Unable to find hash builder definition for key #{key}" unless (builder = builders[key.to_sym])
32
+ builder.call_namespace(namespace.to_sym, *args)
33
+ end
34
+
35
+ def builders
36
+ @builders ||= {}
37
+ end
38
+
39
+ def reset(*keys)
40
+ keys.each do |key|
41
+ @builders[key.to_sym].builder_hash = {} if @builders[key.to_sym]
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ attr_reader :hash_blocks
48
+ attr_accessor :builder_hash
49
+
50
+ def []=(key, value)
51
+ hash[key] = value
52
+ end
53
+
54
+ def build_hash(key=nil, &block)
55
+ @hash_blocks ||= {}
56
+ if(key)
57
+ @hash_blocks[key.to_sym] = block
58
+ else
59
+ @hash_blocks[:namespace] = block
60
+ end
61
+ end
62
+
63
+ def call(*args)
64
+ if(included_namespace_keys.size > 0)
65
+ included_namespace_keys.each do |key|
66
+ @hash_blocks[key].call(*args)
67
+ end
68
+ else
69
+ @hash_blocks.each { |k,v| v.call(*args) }
70
+ end
71
+ hash
72
+ end
73
+
74
+ def call_namespace(key, *args)
75
+ @hash_blocks[key].call(*args)
76
+ hash
77
+ end
78
+
79
+ def hash
80
+ @builder_hash ||= Hash.new
81
+ end
82
+
83
+ def included_namespace_keys
84
+ @included_namespace_keys ||= []
85
+ end
86
+
87
+ def using(key)
88
+ included_namespace_keys << key unless included_namespace_keys.include?(key)
89
+ end
90
+
91
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cwyckoff-hash_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Chris Wyckoff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Hash Builder is a simple tool for offloading the building of hashes.
17
+ email: github@cwyckoff.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - MIT-LICENSE
25
+ files:
26
+ - init.rb
27
+ - lib/hash_builder/hash_builder.rb
28
+ - README.rdoc
29
+ - MIT-LICENSE
30
+ has_rdoc: true
31
+ homepage: http://github.com/cwyckoff/hash_builder
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --line-numbers
35
+ - --inline-source
36
+ - --main
37
+ - README.rdoc
38
+ - --title
39
+ - HashBuilder - A Hash Building Tool for the Ages
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Hash Builder is a simple tool for offloading the building of hashes.
61
+ test_files: []
62
+