hash_dealer 1.0.1 → 1.1.0

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
data/hash_dealer.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hash_dealer}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dan Langevin"]
12
- s.date = %q{2011-08-15}
12
+ s.date = %q{2011-08-16}
13
13
  s.description = %q{Like Factory Girl but for Hashes only}
14
14
  s.email = %q{dan.langevin@lifebooker.com}
15
15
  s.extra_rdoc_files = [
@@ -20,3 +20,18 @@ class Array
20
20
  end
21
21
  end
22
22
  end
23
+ class Hash
24
+ def to_kief
25
+ ret = HashDealer::Kief.new
26
+ self.each_pair do |k,v|
27
+ if v.instance_of?(Hash)
28
+ ret[k] = v.to_kief
29
+ elsif v.instance_of?(Array)
30
+ ret[k] = v.collect{|n| n.instance_of?(Hash) ? n.to_kief : n}
31
+ else
32
+ ret[k] = v
33
+ end
34
+ end
35
+ ret
36
+ end
37
+ end
data/lib/hash_dealer.rb CHANGED
@@ -35,18 +35,18 @@ class HashDealer
35
35
  # method missing
36
36
  def method_missing(meth, *args, &block)
37
37
  raise Exception.new("Please provide either a String or a block to #{meth}") unless (args.length == 1 || (args.empty? && block_given?))
38
- @attributes ||= {}
38
+ @attributes ||= Kief.new
39
39
  if block_given?
40
40
  @attributes[meth.to_sym] = block
41
41
  else
42
- @attributes[meth.to_sym] = args.first
42
+ @attributes[meth.to_sym] = args.first.is_a?(Hash) ? args.first.to_kief : args.first
43
43
  end
44
44
  end
45
45
 
46
46
  def attributes(*args)
47
47
  # allows us to set a root value
48
48
  return @attributes unless @attributes.is_a?(Hash)
49
- att = @parent ? HashDealer.roll(@parent.to_sym) : {}
49
+ att = @parent ? HashDealer.roll(@parent.to_sym) : Kief.new
50
50
  @attributes.each do |k,v|
51
51
  att[k] = v.is_a?(Proc) ? v.call(*args) : v
52
52
  end
@@ -58,5 +58,28 @@ class HashDealer
58
58
  end
59
59
  att
60
60
  end
61
+ # subclass of Hash that defines the matcher method, returning strings like :xyz
62
+ class Kief < Hash
63
+ def make_matcher(val)
64
+ case val
65
+ when String
66
+ return ":#{val}"
67
+ when Kief
68
+ return val.matcher
69
+ when Array
70
+ return val.collect{|n| make_matcher(n)}
71
+ else
72
+ return val
73
+ end
74
+ end
75
+ # get this Kief as a matcher - works recursively
76
+ def matcher
77
+ ret = self.class.new
78
+ self.each_pair do |k,v|
79
+ ret[k] = make_matcher(v)
80
+ end
81
+ ret
82
+ end
83
+ end
61
84
 
62
85
  end
@@ -51,6 +51,14 @@ describe HashDealer do
51
51
  HashDealer.roll(:variable, {"abc" => "123"})[:abc].should eql("123")
52
52
  end
53
53
 
54
-
55
-
54
+ it "should extend itself to matchers - preventing us from having to re-define them" do
55
+ HashDealer.define(:variable) do
56
+ abc("test")
57
+ val({
58
+ :k => "v"
59
+ })
60
+ end
61
+ String.new(HashDealer.roll(:variable).matcher[:abc]).should eql ":test"
62
+ String.new(HashDealer.roll(:variable).matcher[:val][:k]).should eql ":v"
63
+ end
56
64
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: hash_dealer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dan Langevin
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-15 00:00:00 Z
13
+ date: 2011-08-16 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - ">="
131
131
  - !ruby/object:Gem::Version
132
- hash: -4256885510828417571
132
+ hash: -2413462662380501703
133
133
  segments:
134
134
  - 0
135
135
  version: "0"