Skip to content

Commit

Permalink
Merge branch 'lazy_delegator'
Browse files Browse the repository at this point in the history
Conflicts:

	lib/grit.rb
	lib/grit/tree.rb
  • Loading branch information
defunkt committed Feb 23, 2008
2 parents 991ac75 + 4c59690 commit 4f0ea0c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 89 deletions.
2 changes: 1 addition & 1 deletion lib/grit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class << self
self.debug = false

VERSION = '0.7.0'
end
end
33 changes: 3 additions & 30 deletions lib/grit/commit.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Grit

class Commit
include Lazy

attr_reader :id
lazy_reader :parents
lazy_reader :tree
Expand Down Expand Up @@ -33,8 +31,6 @@ def initialize(repo, id, parents, tree, author, authored_date, committer, commit
@committer = committer
@committed_date = committed_date
@message = message

__baked__
end

def id_abbrev
Expand All @@ -56,38 +52,15 @@ def self.create(repo, atts)
#
# Returns Grit::Commit (unbaked)
def create_initialize(repo, atts)
@repo = nil
@id = nil
@parents = nil
@tree = nil
@author = nil
@authored_date = nil
@committer = nil
@committed_date = nil
@message = nil
@__baked__ = nil

@repo = repo
atts.each do |k, v|
instance_variable_set("@#{k}".to_sym, v)
instance_variable_set("@#{k}", v)
end
self
end

# Use the id of this instance to populate all of the other fields
# when any of them are called.
#
# Returns nil
def __bake__
temp = self.class.find_all(@repo, @id, {:max_count => 1}).first
@parents = temp.parents
@tree = temp.tree
@author = temp.author
@authored_date = temp.authored_date
@committer = temp.committer
@committed_date = temp.committed_date
@message = temp.message
nil
def lazy_source
self.class.find_all(@repo, @id, {:max_count => 1}).first
end

# Count the number of commits reachable from this ref
Expand Down
62 changes: 20 additions & 42 deletions lib/grit/lazy.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
##
# Allows attributes to be declared as lazy, meaning that they won't be
# computed until they are asked for. Just mix this module in:
# computed until they are asked for.
#
# class Foo
# include Lazy
# ...
# end
#
# To specify a lazy reader:
#
# lazy_reader :att
# Works by delegating each lazy_reader to a cached lazy_source method.
#
# Then, define a method called __bake__ that computes all your lazy
# attributes:
# class Person
# lazy_reader :eyes
#
# def __bake__
# @att = ...
# def lazy_source
# OpenStruct.new(:eyes => 2)
# end
# end
#
# If you happen to have already done all the hard work, you can mark an instance
# as already baked by calling:
# >> Person.new.eyes
# => 2
#
# __baked__
#
# That's it! (Tom Preston-Werner: rubyisawesome.com)
module Lazy
module ClassMethods
def lazy_reader(*args)
args.each do |arg|
define_method(arg) do
val = instance_variable_get("@#{arg}")
return val if val
self.__prebake__
instance_variable_get("@#{arg}")
end
def lazy_reader(*args)
args.each do |arg|
ivar = "@#{arg}"
define_method(arg) do
val = instance_variable_get(ivar)
return val if val
instance_variable_set(ivar, (@lazy_source ||= lazy_source).send(arg))
end
end
end

def __prebake__
return if @__baked__
self.__bake__
@__baked__ = true
end

def __baked__
@__baked__ = true
end

def self.included(base)
base.extend(ClassMethods)
end
end
end

Object.extend Lazy unless Object.ancestors.include? Lazy
21 changes: 5 additions & 16 deletions lib/grit/tree.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
module Grit

class Tree
include Lazy

lazy_reader :contents
attr_reader :id
attr_reader :mode
attr_reader :name

def initialize
@contents = nil
@__baked__ = nil
end

# Construct the contents of the tree
# +repo+ is the Repo
# +treeish+ is the reference
Expand All @@ -29,19 +22,17 @@ def construct_initialize(repo, id, text)
@repo = repo
@id = id
@contents = []
@__baked__ = nil

text.split("\n").each do |line|
@contents << content_from_string(repo, line)
end
@contents.compact!
__baked__

self
end

def __bake__
temp = Tree.construct(@repo, @id, [])
@contents = temp.contents
def lazy_source
Tree.construct(@repo, @id, [])
end

# Create an unbaked Tree containing just the specified attributes
Expand All @@ -60,11 +51,9 @@ def self.create(repo, atts)
# Returns Grit::Tree (unbaked)
def create_initialize(repo, atts)
@repo = repo
@contents = nil
@__baked__ = nil

atts.each do |k, v|
instance_variable_set("@#{k}".to_sym, v)
instance_variable_set("@#{k}", v)
end
self
end
Expand Down Expand Up @@ -107,4 +96,4 @@ def inspect
end
end # Tree

end # Grit
end # Grit

0 comments on commit 4f0ea0c

Please sign in to comment.