key_struct 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +29 -14
- data/lib/key_struct.rb +3 -0
- data/lib/key_struct/version.rb +1 -1
- data/spec/key_struct_spec.rb +12 -0
- metadata +8 -8
data/README.rdoc
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
= key_struct
|
2
2
|
|
3
|
-
Defines KeyStruct, which
|
4
|
-
takes keyword args (using a hash, rails-style). Use it to
|
3
|
+
Defines KeyStruct, which acts the same as ruby's Struct but the struct's
|
4
|
+
initializer takes keyword args (using a hash, rails-style). Use it to
|
5
|
+
define a class via:
|
5
6
|
|
6
7
|
Name = KeyStruct[:first, :last]
|
7
8
|
|
@@ -19,32 +20,46 @@ Then you can create an instance of the class using keywords for the parameters:
|
|
19
20
|
|
20
21
|
and you have the usal readers and writers:
|
21
22
|
|
22
|
-
name.first --> "Jack"
|
23
|
-
name.last --> "Ripper"
|
23
|
+
name.first # --> "Jack"
|
24
|
+
name.last # --> "Ripper"
|
24
25
|
name.last = "Sprat"
|
25
|
-
name.last --> "Sprat"
|
26
|
-
name.to_s --> "Sprat, Jack" for the enhanced class example
|
26
|
+
name.last # --> "Sprat"
|
27
|
+
name.to_s # --> "Sprat, Jack" for the enhanced class example
|
27
28
|
|
28
|
-
|
29
|
+
== Readers and Writers
|
30
|
+
|
31
|
+
As per above, the normal behavior is get readers and writers. But, by analogy with +attr_reader+ vs +attr_accessor+, you can choose whether you want read/write or just read:
|
32
|
+
|
33
|
+
Writeable = KeyStruct.accesor(:first, :last) # aliased as KeyStruct[]
|
34
|
+
Readonly = KeyStruct.reader(:first, :last) # class has readers but not writers
|
35
|
+
|
36
|
+
== Default values
|
37
|
+
|
38
|
+
If you leave off a keyword when instantiating, normally the member value is nil:
|
29
39
|
|
30
40
|
name = Name.new(:first => "Jack")
|
31
|
-
name.last --> nil
|
41
|
+
name.last # --> nil
|
32
42
|
|
33
43
|
But the class definition can specify defaults, e.g.
|
34
44
|
|
35
45
|
Name = KeyStruct[:first, :last => "Doe"]
|
46
|
+
|
36
47
|
name = Name.new(:first => "John")
|
37
|
-
name.first --> "John"
|
38
|
-
name.last --> "Doe"
|
48
|
+
name.first # --> "John"
|
49
|
+
name.last # --> "Doe"
|
50
|
+
|
51
|
+
== Argument Checking
|
39
52
|
|
40
53
|
The struct initializer checks for invalid arguments:
|
41
54
|
|
42
|
-
name = Name.new(:middle => "Xaviar") --> raises ArgumentError
|
55
|
+
name = Name.new(:middle => "Xaviar") # --> raises ArgumentError
|
56
|
+
|
57
|
+
== Equaltiy
|
43
58
|
|
44
|
-
|
59
|
+
KeyStruct classes define the == operator, which returns true iff all corresponding struct members are equal (likewise via ==)
|
45
60
|
|
46
|
-
|
47
|
-
|
61
|
+
Name.new(:first => "John", :last => "Doe") == Name.new(:first => "John", :last => "Doe") # --> true
|
62
|
+
Name.new(:first => "John", :last => "Doe") == Name.new(:first => "Jane", :last => "Doe") # --> false
|
48
63
|
|
49
64
|
== Installation
|
50
65
|
|
data/lib/key_struct.rb
CHANGED
@@ -29,6 +29,9 @@ module KeyStruct
|
|
29
29
|
end
|
30
30
|
raise ArgumentError, "Invalid argument(s): #{args.keys.map(&:inspect).join(' ')}; KeyStruct accepts #{keys.map(&:inspect).join(' ')}" if args.any?
|
31
31
|
end
|
32
|
+
define_method(:==) do |other|
|
33
|
+
keys.all?{|key| self.send(key) == other.send(key)}
|
34
|
+
end
|
32
35
|
end
|
33
36
|
klass
|
34
37
|
end
|
data/lib/key_struct/version.rb
CHANGED
data/spec/key_struct_spec.rb
CHANGED
@@ -109,4 +109,16 @@ describe "KeyStruct" do
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
+
context "==" do
|
113
|
+
before(:all) do
|
114
|
+
@klass = KeyStruct.accessor(:a, :b)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "returns true iff all members are ==" do
|
118
|
+
@klass.new(:a => 1, :b => 2).should == @klass.new(:a => 1, :b => 2)
|
119
|
+
@klass.new(:a => 1, :b => 2).should_not == @klass.new(:a => 1, :b => 3)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
112
124
|
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.0
|
4
|
+
version: 0.1.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-11-
|
12
|
+
date: 2011-11-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70365334363320 !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: *
|
24
|
+
version_requirements: *70365334363320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: simplecov
|
27
|
-
requirement: &
|
27
|
+
requirement: &70365334361480 !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: *
|
35
|
+
version_requirements: *70365334361480
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: simplecov-gem-adapter
|
38
|
-
requirement: &
|
38
|
+
requirement: &70365334359460 !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: *
|
46
|
+
version_requirements: *70365334359460
|
47
47
|
description: Defines KeyStruct analogous to Struct, but constructor takes keyword
|
48
48
|
arguments
|
49
49
|
email:
|