key_struct 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -18,7 +18,7 @@ Then you can create an instance of the class using keywords for the parameters:
18
18
 
19
19
  name = Name.new(:first => "Jack", :last => "Ripper")
20
20
 
21
- and you have the usal readers and writers:
21
+ and you have the usal readers and writers
22
22
 
23
23
  name.first # --> "Jack"
24
24
  name.last # --> "Ripper"
@@ -26,13 +26,28 @@ and you have the usal readers and writers:
26
26
  name.last # --> "Sprat"
27
27
  name.to_s # --> "Sprat, Jack" for the enhanced class example
28
28
 
29
- == Readers and Writers
30
29
 
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:
30
+ == Readers, Writers, and Instance Variables
31
+
32
+ As per above, the normal behavior is to 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
 
33
34
  Writeable = KeyStruct.accesor(:first, :last) # aliased as KeyStruct[]
34
35
  Readonly = KeyStruct.reader(:first, :last) # class has readers but not writers
35
36
 
37
+ The analogy is not just skin deep: +KeyStruct+ actually uses
38
+ +attr_accessor+ or +attr_reader+ to define the accessors for the generated class.
39
+ This means that you also get the corresponding instance variables:
40
+
41
+ name.instance_variable_get("@last") # --> "Sprat"
42
+ name.instance_variable_set("@last", "Sparrow")
43
+ name.last # --> "Sparrow"
44
+
45
+ The instance variables can be useful of course when using KeyStruct to define an anonymous
46
+ base class for your own classes, as shown for the +to_s+ example above.
47
+
48
+ (This is one way that +KeyStruct+ differs from ruby's built-in +Struct+:
49
+ built-in +Struct+ does not define instance variables.)
50
+
36
51
  == Default values
37
52
 
38
53
  If you leave off a keyword when instantiating, normally the member value is nil:
@@ -40,7 +55,8 @@ If you leave off a keyword when instantiating, normally the member value is nil:
40
55
  name = Name.new(:first => "Jack")
41
56
  name.last # --> nil
42
57
 
43
- But the class definition can specify defaults, e.g.
58
+ But when you define the class you can specify defaults that will be filled in by
59
+ the class initializer. For example:
44
60
 
45
61
  Name = KeyStruct[:first, :last => "Doe"]
46
62
 
@@ -48,11 +64,15 @@ But the class definition can specify defaults, e.g.
48
64
  name.first # --> "John"
49
65
  name.last # --> "Doe"
50
66
 
67
+ name = Name.new(:first => "John", :last => "Deere")
68
+ name.first # --> "John"
69
+ name.last # --> "Deere"
70
+
51
71
  == Argument Checking
52
72
 
53
73
  The struct initializer checks for invalid arguments:
54
74
 
55
- name = Name.new(:middle => "Xaviar") # --> raises ArgumentError
75
+ name = Name.new(:this_is_a_typo => "Xavier") # --> raises ArgumentError
56
76
 
57
77
  == Equaltiy
58
78
 
@@ -77,17 +97,17 @@ Requires ruby >= 1.9.2. (Has been tested on MRI 1.9.2 and MRI 1.9.3)
77
97
 
78
98
  == History
79
99
 
100
+ Past: There was some discussion around this idea in this thread:
101
+ http://www.ruby-forum.com/topic/138124 in 2008.
102
+
80
103
  Future: I hope that this gem will be obviated in future versions of ruby.
81
104
 
82
105
  == Note on Patches/Pull Requests
83
106
 
84
107
  * Fork the project.
85
108
  * Make your feature addition or bug fix.
86
- * Add tests for it. This is important so I don't break it in a
87
- future version unintentionally. Make sure that the coverage report
109
+ * Add tests for it. Make sure that the coverage report
88
110
  (generated automatically when you run rspec) is at 100%
89
- * Commit, do not mess with Rakefile, version, or history.
90
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
91
111
  * Send me a pull request.
92
112
 
93
113
  == Copyright
data/lib/key_struct.rb CHANGED
@@ -17,23 +17,22 @@ module KeyStruct
17
17
  private
18
18
 
19
19
  def self.define_key_struct(access, keys)
20
- klass = Class.new
21
- klass.class_eval do
20
+ Class.new.class_eval do
22
21
  keyvalues = Hash[*keys.map{|key| (Hash === key) ? key.to_a : [key, nil]}.flatten(2)]
23
22
  keys = keyvalues.keys
24
23
  send access, *keys
25
24
  define_method(:initialize) do |args={}|
26
25
  args = keyvalues.merge(args)
27
26
  keys.each do |key|
28
- instance_variable_set("@#{key}", args.delete(key))
27
+ instance_variable_set("@#{key}".to_sym, args.delete(key))
29
28
  end
30
29
  raise ArgumentError, "Invalid argument(s): #{args.keys.map(&:inspect).join(' ')}; KeyStruct accepts #{keys.map(&:inspect).join(' ')}" if args.any?
31
30
  end
32
31
  define_method(:==) do |other|
33
32
  keys.all?{|key| self.send(key) == other.send(key)}
34
33
  end
34
+ self
35
35
  end
36
- klass
37
36
  end
38
37
 
39
38
  end
@@ -1,3 +1,3 @@
1
1
  module KeyStruct
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  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.1.1
4
+ version: 0.1.2
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-27 00:00:00.000000000Z
12
+ date: 2011-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70343126940120 !ruby/object:Gem::Requirement
16
+ requirement: &70161596448140 !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: *70343126940120
24
+ version_requirements: *70161596448140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: simplecov
27
- requirement: &70343126939660 !ruby/object:Gem::Requirement
27
+ requirement: &70161596447560 !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: *70343126939660
35
+ version_requirements: *70161596447560
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: simplecov-gem-adapter
38
- requirement: &70343126939240 !ruby/object:Gem::Requirement
38
+ requirement: &70161596438560 !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: *70343126939240
46
+ version_requirements: *70161596438560
47
47
  description: Defines KeyStruct analogous to Struct, but constructor takes keyword
48
48
  arguments
49
49
  email:
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  version: '0'
85
85
  requirements: []
86
86
  rubyforge_project:
87
- rubygems_version: 1.8.6
87
+ rubygems_version: 1.8.10
88
88
  signing_key:
89
89
  specification_version: 3
90
90
  summary: Defines KeyStruct analogous to Struct, but constructor takes keyword arguments