public
Fork of mojombo/grit
Description: Grit is a Ruby library for extracting information from a git repository in an object oriented manner.
Homepage: http://grit.rubyforge.org/
Clone URL: git://github.com/dustin/grit.git
Search Repo:
Merge branch 'defunkt' into local
mojombo (author)
Tue Mar 25 21:25:12 -0700 2008
commit  11d191ef3f04012a78222cb118619c16d5581886
tree    abbd88b5eb9c6a2113cc61f16255ebb0e5541331
parent  f11ceb37cbd72b8c7627aa9e2a7b8dbcbf10d107 parent  ad44b88d69c4b7b61a9ec12445f00f082ca19f41
...
1
2
3
4
5
6
7
8
...
35
36
37
38
39
40
41
42
43
44
...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
 
95
96
97
...
212
213
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
216
217
...
1
2
3
 
 
4
5
6
...
33
34
35
 
 
36
37
38
39
40
...
54
55
56
 
 
 
 
 
 
 
 
 
 
 
 
57
58
 
59
60
61
62
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
65
66
67
68
...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
0
@@ -1,8 +1,6 @@
0
 module Grit
0
   
0
   class Commit
0
- include Lazy
0
-
0
     attr_reader :id
0
     lazy_reader :parents
0
     lazy_reader :tree
0
@@ -35,8 +33,6 @@
0
       @committed_date = committed_date
0
       @message = message.join("\n")
0
       @short_message = message[0] || ''
0
-
0
- __baked__
0
     end
0
     
0
     def id_abbrev
0
0
0
@@ -58,40 +54,15 @@
0
     #
0
     # Returns Grit::Commit (unbaked)
0
     def create_initialize(repo, atts)
0
- @repo = nil
0
- @id = nil
0
- @parents = nil
0
- @tree = nil
0
- @author = nil
0
- @authored_date = nil
0
- @committer = nil
0
- @committed_date = nil
0
- @message = nil
0
- @short_message = nil
0
- @__baked__ = nil
0
-
0
       @repo = repo
0
       atts.each do |k, v|
0
- instance_variable_set("@#{k}".to_sym, v)
0
+ instance_variable_set("@#{k}", v)
0
       end
0
       self
0
     end
0
     
0
- # Use the id of this instance to populate all of the other fields
0
- # when any of them are called.
0
- #
0
- # Returns nil
0
- def __bake__
0
- temp = self.class.find_all(@repo, @id, {:max_count => 1}).first
0
- @parents = temp.parents
0
- @tree = temp.tree
0
- @author = temp.author
0
- @authored_date = temp.authored_date
0
- @committer = temp.committer
0
- @committed_date = temp.committed_date
0
- @message = temp.message
0
- @short_message = temp.short_message
0
- nil
0
+ def lazy_source
0
+ self.class.find_all(@repo, @id, {:max_count => 1}).first
0
     end
0
     
0
     # Count the number of commits reachable from this ref
0
@@ -212,6 +183,25 @@
0
     def self.actor(line)
0
       m, actor, epoch = *line.match(/^.+? (.*) (\d+) .*$/)
0
       [Actor.from_string(actor), Time.at(epoch.to_i)]
0
+ end
0
+
0
+ def to_hash
0
+ {
0
+ 'id' => id,
0
+ 'parents' => parents.map { |p| { 'id' => p.id } },
0
+ 'tree' => tree.id,
0
+ 'message' => message,
0
+ 'author' => {
0
+ 'name' => author.name,
0
+ 'email' => author.email
0
+ },
0
+ 'committer' => {
0
+ 'name' => committer.name,
0
+ 'email' => committer.email
0
+ },
0
+ 'authored_date' => authored_date.xmlschema,
0
+ 'committed_date' => committed_date.xmlschema,
0
+ }
0
     end
0
   end # Commit
0
   
...
 
1
2
 
3
4
5
6
7
 
8
9
 
 
10
11
12
13
14
15
16
17
 
 
18
 
19
20
21
 
 
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
 
 
 
 
 
 
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
 
...
1
2
 
3
4
 
 
 
 
5
6
 
7
8
9
 
 
 
 
 
 
 
10
11
12
13
14
 
 
15
16
17
 
 
 
18
 
 
 
 
 
 
 
 
 
19
20
21
22
23
24
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
0
@@ -1,54 +1,32 @@
0
+##
0
 # Allows attributes to be declared as lazy, meaning that they won't be
0
-# computed until they are asked for. Just mix this module in:
0
+# computed until they are asked for.
0
 #
0
-# class Foo
0
-# include Lazy
0
-# ...
0
-# end
0
+# Works by delegating each lazy_reader to a cached lazy_source method.
0
 #
0
-# To specify a lazy reader:
0
+# class Person
0
+# lazy_reader :eyes
0
 #
0
-# lazy_reader :att
0
-#
0
-# Then, define a method called __bake__ that computes all your lazy
0
-# attributes:
0
-#
0
-# def __bake__
0
-# @att = ...
0
+# def lazy_source
0
+# OpenStruct.new(:eyes => 2)
0
 # end
0
+# end
0
 #
0
-# If you happen to have already done all the hard work, you can mark an instance
0
-# as already baked by calling:
0
+# >> Person.new.eyes
0
+# => 2
0
 #
0
-# __baked__
0
-#
0
-# That's it! (Tom Preston-Werner: rubyisawesome.com)
0
 module Lazy
0
- module ClassMethods
0
- def lazy_reader(*args)
0
- args.each do |arg|
0
- define_method(arg) do
0
- val = instance_variable_get("@#{arg}")
0
- return val if val
0
- self.__prebake__
0
- instance_variable_get("@#{arg}")
0
- end
0
+ def lazy_reader(*args)
0
+ args.each do |arg|
0
+ ivar = "@#{arg}"
0
+ define_method(arg) do
0
+ val = instance_variable_get(ivar)
0
+ return val if val
0
+ instance_variable_set(ivar, (@lazy_source ||= lazy_source).send(arg))
0
       end
0
     end
0
   end
0
-
0
- def __prebake__
0
- return if @__baked__
0
- self.__bake__
0
- @__baked__ = true
0
- end
0
-
0
- def __baked__
0
- @__baked__ = true
0
- end
0
-
0
- def self.included(base)
0
- base.extend(ClassMethods)
0
- end
0
 end
0
+
0
+Object.extend Lazy unless Object.ancestors.include? Lazy
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
29
30
31
32
33
34
35
36
37
38
 
39
40
41
42
43
44
 
 
45
46
47
48
...
60
61
62
63
64
65
66
67
 
68
69
70
...
1
2
3
 
 
4
5
6
7
8
 
 
 
 
 
9
10
11
12
13
...
22
23
24
 
25
26
27
28
29
 
30
31
32
33
 
 
 
34
35
36
37
38
39
...
51
52
53
 
 
54
55
 
56
57
58
59
0
@@ -1,18 +1,11 @@
0
 module Grit
0
   
0
   class Tree
0
- include Lazy
0
-
0
     lazy_reader :contents
0
     attr_reader :id
0
     attr_reader :mode
0
     attr_reader :name
0
     
0
- def initialize
0
- @contents = nil
0
- @__baked__ = nil
0
- end
0
-
0
     # Construct the contents of the tree
0
     # +repo+ is the Repo
0
     # +treeish+ is the reference
0
0
0
@@ -29,19 +22,17 @@
0
       @repo = repo
0
       @id = id
0
       @contents = []
0
- @__baked__ = nil
0
       
0
       text.split("\n").each do |line|
0
         @contents << content_from_string(repo, line)
0
       end
0
       @contents.compact!
0
- __baked__
0
+
0
       self
0
     end
0
     
0
- def __bake__
0
- temp = Tree.construct(@repo, @id, [])
0
- @contents = temp.contents
0
+ def lazy_source
0
+ Tree.construct(@repo, @id, [])
0
     end
0
     
0
     # Create an unbaked Tree containing just the specified attributes
0
0
@@ -60,11 +51,9 @@
0
     # Returns Grit::Tree (unbaked)
0
     def create_initialize(repo, atts)
0
       @repo = repo
0
- @contents = nil
0
- @__baked__ = nil
0
       
0
       atts.each do |k, v|
0
- instance_variable_set("@#{k}".to_sym, v)
0
+ instance_variable_set("@#{k}", v)
0
       end
0
       self
0
     end
...
160
161
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
0
@@ -160,5 +160,24 @@
0
     @c = Commit.create(@r, :id => 'abc')
0
     assert_equal %Q{#<Grit::Commit "abc">}, @c.inspect
0
   end
0
+
0
+ # to_hash
0
+
0
+ def test_to_hash
0
+ @c = Commit.create(@r, :id => '4c8124ffcf4039d292442eeccabdeca5af5c5017')
0
+
0
+ expected = {
0
+ 'parents' => ['id' => "634396b2f541a9f2d58b00be1a07f0c358b999b3"],
0
+ 'committed_date' => "2007-10-10T00:06:12-07:00",
0
+ 'tree' => "672eca9b7f9e09c22dcb128c283e8c3c8d7697a4",
0
+ 'authored_date' => "2007-10-10T00:06:12-07:00",
0
+ 'committer' => {'email' => "tom@mojombo.com", 'name' => "Tom Preston-Werner"},
0
+ 'message' => "implement Grit#heads",
0
+ 'author' => {'email' => "tom@mojombo.com", 'name' => "Tom Preston-Werner"},
0
+ 'id' => "4c8124ffcf4039d292442eeccabdeca5af5c5017"
0
+ }
0
+
0
+ assert_equal expected, @c.to_hash
0
+ end
0
 end

Comments

    No one has commented yet.