yack 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +31 -4
- data/lib/yack.rb +19 -20
- metadata +39 -21
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
Yack
|
1
|
+
Yack ![Railsonfire Status for flomotlik/yack](https://railsonfire-dev.heroku.com/projects/48d5cb30-3ba5-012f-d8f0-123139181d2d/status)
|
2
2
|
===
|
3
3
|
|
4
|
-
Yack is a library to create simple callbacks for
|
4
|
+
Yack (Yet another callback kit) is a library to create simple callbacks for your Hashes. It is especially useful when loading config files. Instead of pulling the values out of the config file you simply add callbacks and when the attributes exist in the Hash a code block is run.
|
5
5
|
|
6
6
|
Installation
|
7
7
|
------------
|
@@ -23,15 +23,42 @@ Simply create a new callback object
|
|
23
23
|
then simply add some callbacks
|
24
24
|
|
25
25
|
yack.parent.child do |value|
|
26
|
-
|
26
|
+
//value is "TestString"
|
27
27
|
end
|
28
28
|
|
29
29
|
and start the processing
|
30
30
|
|
31
|
-
yack.
|
31
|
+
yack.process(YAML.load(YOUR_YAML_FILE))
|
32
32
|
|
33
33
|
Your Code block will be called with the value of the attribute which in the last example would be "TestString".
|
34
34
|
|
35
|
+
As it processes Hashes directly you can use JSON, Yaml or any other format that can be represented by a Hash. Currently only on callback can be registered for a specific point in the hash.
|
36
|
+
|
37
|
+
If you want your code block to handle a child hash this is also possible by only adding a callback for the parent
|
38
|
+
|
39
|
+
yack.parent do |value|
|
40
|
+
//value is {:test => "TestString"}
|
41
|
+
end
|
42
|
+
|
43
|
+
Will be called with {:test => "TestString"} considering the example before.
|
44
|
+
|
45
|
+
If you want to add callbacks for several child attributes simply add several callbacks
|
46
|
+
|
47
|
+
---
|
48
|
+
:parent:
|
49
|
+
:child1: 1
|
50
|
+
:child2: 2
|
51
|
+
|
52
|
+
The following code blocks will be called with the correct attributes:
|
53
|
+
|
54
|
+
yack.parent.child1 do |value|
|
55
|
+
//value is 1
|
56
|
+
end
|
57
|
+
|
58
|
+
yack.parent.child2 do |value|
|
59
|
+
//value is 2
|
60
|
+
end
|
61
|
+
|
35
62
|
License
|
36
63
|
-------
|
37
64
|
|
data/lib/yack.rb
CHANGED
@@ -1,46 +1,45 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
1
|
module Yack
|
4
2
|
class Callback
|
5
3
|
def initialize
|
6
|
-
@
|
4
|
+
@callbacks = {}
|
7
5
|
end
|
8
6
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
_call_back yaml, @groups
|
7
|
+
def process hash
|
8
|
+
@callbacks.keys.each{|key|
|
9
|
+
_call_back @callbacks[key], hash[key]
|
13
10
|
}
|
14
11
|
end
|
15
12
|
|
16
13
|
def method_missing m, *args, &block
|
17
|
-
block_given? ? @
|
14
|
+
block_given? ? @callbacks[m] = block : Handler.new(m, @callbacks)
|
18
15
|
end
|
19
16
|
|
20
17
|
private
|
21
|
-
def _call_back
|
22
|
-
return if
|
23
|
-
case(
|
18
|
+
def _call_back callback, hash
|
19
|
+
return if callback.nil? or hash.nil?
|
20
|
+
case(callback)
|
24
21
|
when Hash
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
callback.keys.each do |key|
|
23
|
+
_call_back callback[key], hash[key]
|
24
|
+
end
|
25
|
+
when Proc
|
26
|
+
callback.call(hash)
|
28
27
|
end
|
29
28
|
end
|
30
29
|
end
|
31
30
|
|
32
31
|
class Handler
|
33
|
-
def initialize method,
|
34
|
-
|
35
|
-
@
|
32
|
+
def initialize method, callbacks
|
33
|
+
callbacks[method] ||= {}
|
34
|
+
@callbacks = callbacks[method]
|
36
35
|
end
|
37
36
|
|
38
37
|
def method_missing m, *args, &block
|
39
38
|
if block_given?
|
40
|
-
@
|
39
|
+
@callbacks[m] = block
|
41
40
|
else
|
42
|
-
@
|
43
|
-
@
|
41
|
+
@callbacks[m] ||= {}
|
42
|
+
@callbacks = @callbacks[m]
|
44
43
|
return self
|
45
44
|
end
|
46
45
|
end
|
metadata
CHANGED
@@ -1,49 +1,67 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: yack
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Florian Motlik
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
17
|
+
|
18
|
+
date: 2012-02-20 00:00:00 Z
|
13
19
|
dependencies: []
|
14
|
-
|
15
|
-
|
20
|
+
|
21
|
+
description: Churn Hashes via callbacks. Define rules for specific parameters, load Hashes from config files and get callbacks if the routes match
|
16
22
|
email: flo@railsonfire.com
|
17
23
|
executables: []
|
24
|
+
|
18
25
|
extensions: []
|
26
|
+
|
19
27
|
extra_rdoc_files: []
|
20
|
-
|
28
|
+
|
29
|
+
files:
|
21
30
|
- LICENSE
|
22
31
|
- README.md
|
23
32
|
- lib/yack.rb
|
24
33
|
homepage: https://github.com/flomotlik/yack
|
25
34
|
licenses: []
|
35
|
+
|
26
36
|
post_install_message:
|
27
37
|
rdoc_options: []
|
28
|
-
|
38
|
+
|
39
|
+
require_paths:
|
29
40
|
- lib
|
30
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
42
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
|
36
|
-
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
51
|
none: false
|
38
|
-
requirements:
|
39
|
-
- -
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
42
59
|
requirements: []
|
60
|
+
|
43
61
|
rubyforge_project:
|
44
62
|
rubygems_version: 1.8.10
|
45
63
|
signing_key:
|
46
64
|
specification_version: 3
|
47
|
-
summary: Churn
|
48
|
-
load the yaml file and get callbacks if the routes match
|
65
|
+
summary: Churn Hashes via callbacks. Define rules for specific parameters, load Hashes from config files and get callbacks if the routes match
|
49
66
|
test_files: []
|
67
|
+
|