key_struct 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -94,12 +94,19 @@ primary comparison key, and so on down the line. Thus:
94
94
  LastFirst = KeyStruct[:last, :first]
95
95
  LastFirst.new(:first => "Abigail", :last => "Zither") <=> LastFirst.new(:first => "Zenobia", :last => "Aardvark") # --> +1
96
96
 
97
+ == to_s and inspect
98
+
99
+ KeyStruct classes define reasonable default +to_s+ and +inspect+ methods, along the lines of:
100
+
101
+ Name.new(:first => "Jack", :last => "Ripper").to_s # --> '[Name first:Jack last:Ripper]'
102
+ Name.new(:first => "Jack", :last => "Ripper").inspect # --> '<Name:0x1234abcd first:"Jack" last:"Ripper">'
103
+
97
104
  == Converting to a hash
98
105
 
99
106
  KeyStruct classes define a +to_hash+ method that returns a hash containing
100
107
  all members and their values:
101
108
 
102
- Name.new(:first => "Jack", :last => "Ripper").to_hash # --> {:first => "Jack", :last => "Ripper")
109
+ Name.new(:first => "Jack", :last => "Ripper").to_hash # --> {:first => "Jack", :last => "Ripper")
103
110
 
104
111
  == Installation
105
112
 
@@ -119,6 +126,7 @@ Requires ruby >= 1.9.2. (Has been tested on MRI 1.9.2 and MRI 1.9.3)
119
126
 
120
127
  Release Notes:
121
128
 
129
+ * 0.3.0 - Introduced to_s and inspect
122
130
  * 0.2.1 - Bug fix: return false for == with an incompatible object. Was raising NoMethodError
123
131
  * 0.2.0 - Introduced <=> and to_hash
124
132
  * 0.1.0 - Introduced ==
@@ -40,6 +40,12 @@ module KeyStruct
40
40
  define_method(:to_hash) do
41
41
  Hash[*keys.map{ |key| [key, self.send(key)]}.flatten(2)]
42
42
  end
43
+ define_method(:to_s) do
44
+ "[#{self.class.name} #{keys.map{|key| "#{key}:#{self.send(key)}"}.join(' ')}]"
45
+ end
46
+ define_method(:inspect) do
47
+ "<#{self.class.name}:0x#{self.object_id.to_s(16)} #{keys.map{|key| "#{key}:#{self.send(key).inspect}"}.join(' ')}>"
48
+ end
43
49
  self
44
50
  end
45
51
  end
@@ -1,3 +1,3 @@
1
1
  module KeyStruct
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -149,5 +149,16 @@ describe "KeyStruct" do
149
149
  KeyStruct.accessor(:a => 3, :b => 4).new.to_hash.should == {:a => 3, :b => 4}
150
150
  end
151
151
 
152
+ context "display as a string" do
153
+ PrintMe = KeyStruct[:a => 3, :b => "hello"]
154
+
155
+ it "should be nice for :to_s" do
156
+ PrintMe.new.to_s.should == "[PrintMe a:3 b:hello]"
157
+ end
158
+
159
+ it "should be detailed for :inspect" do
160
+ PrintMe.new.inspect.should match /<PrintMe:0x[0-9a-f]+ a:3 b:"hello">/
161
+ end
162
+ end
152
163
 
153
164
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: key_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-15 00:00:00.000000000 Z
12
+ date: 2011-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70104882335960 !ruby/object:Gem::Requirement
16
+ requirement: &70193269639900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70104882335960
24
+ version_requirements: *70193269639900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: simplecov
27
- requirement: &70104882335360 !ruby/object:Gem::Requirement
27
+ requirement: &70193269639420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70104882335360
35
+ version_requirements: *70193269639420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: simplecov-gem-adapter
38
- requirement: &70104882334760 !ruby/object:Gem::Requirement
38
+ requirement: &70193269638820 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70104882334760
46
+ version_requirements: *70193269638820
47
47
  description: Defines KeyStruct analogous to Struct, but constructor takes keyword
48
48
  arguments
49
49
  email: