Class: Madrox::Timeline

Inherits:
Object
  • Object
show all
Defined in:
lib/madrox/timeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, user, email = nil) ⇒ Timeline

Returns a new instance of Timeline.



31
32
33
34
35
36
# File 'lib/madrox/timeline.rb', line 31

def initialize(repo, user, email = nil)
  @user  = user
  @email = email
  @repo  = repo
  @grit  = repo.grit
end

Instance Attribute Details

#emailObject

Public: Sets the email for this timeline.

email - The String email.

Returns nothing.



12
13
14
# File 'lib/madrox/timeline.rb', line 12

def email
  @email
end

#gritObject (readonly)

Gets the Grit object for this Madrox::Repo.

Returns a Grit::Repo instance.



29
30
31
# File 'lib/madrox/timeline.rb', line 29

def grit
  @grit
end

#repoObject (readonly)

Gets the Madrox object for this timeline.

Returns a Madrox::Repo instance.



24
25
26
# File 'lib/madrox/timeline.rb', line 24

def repo
  @repo
end

#userObject (readonly)

Public: Gets the user name for this timeline.

Returns a String.



6
7
8
# File 'lib/madrox/timeline.rb', line 6

def user
  @user
end

Instance Method Details

#actorObject

Public: Builds a Git actor object for any posted updates to this timeline. Uses the timelines user and email.

Returns a Grit::Actor.



110
111
112
# File 'lib/madrox/timeline.rb', line 110

def actor
  Grit::Actor.new(@user, @email)
end

#fave(commit) ⇒ Object

Public: Marks a given commit as a favorite. The commit is stored in a separate branch named “##user-favorites”. The commit’s original committed author and date remain the same, and the new commit tracks the date it was favorited.



101
102
103
104
# File 'lib/madrox/timeline.rb', line 101

def fave(commit)
  post(commit.message, :head => "#{@user}-favorites",
    :author => commit.author, :authored_date => commit.authored_date)
end

#messages(options = {}) ⇒ Object

Public: Gets the messages for this timeline. Automatically removes any merge commits.

options - Hash of options to filter the message output.

:max_count - Fixnum specifying the number of commits to show.  
             Default: 30.
:skip      - Fixnum specifying the number of commits to skip.
:page      - Fixnum of the current page.  This is used to 
             implicitly calculate the :skip option.  
             Default: 1

Returns an Array of Grit::Commit instances.



50
51
52
53
54
55
56
57
58
# File 'lib/madrox/timeline.rb', line 50

def messages(options = {})
  options[:no_merges]   = true
  options[:max_count] ||= 30
  options[:skip]      ||= begin
    options[:max_count] * ([options.delete(:page).to_i, 1].max - 1)
  end
  @grit.log(@user, nil, options).
    delete_if { |commit| commit.parents.size != 1 }
end

#post(message, options = {}) ⇒ Object

Public: Posts the given message to the timeline. This is a simple commit with no changed content. Just a message.

message - String message for the timeline update. options - Hash of options passed to Grit::Index#commit.

Returns a String SHA1 of the created Git commit.



67
68
69
70
71
72
73
# File 'lib/madrox/timeline.rb', line 67

def post(message, options = {})
  idx     = @grit.index
  options = {:committer => actor, :head => @user}.update(options)
  options[:parents] ||= [@grit.commit(@user) || @grit.commit("HEAD")]
  options[:parents].compact!
  @grit.index.commit(message, options)
end

#retweet(commit, message, options = {}) ⇒ Object

Public: Retweets a given commit. The author name and date is taken from the commit. The message can optionally be annotated.

commit - The Grit::Commit that is being retweeted. message - An optional String annotation to the retweet content. options - An optional Hash that is passed to #post.

Returns a String SHA1 of the created Git commit.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/madrox/timeline.rb', line 83

def retweet(commit, message, options = {})
  if message.is_a?(Hash)
    options = message
    message = nil
  end
  if message
    message << " RT @#{commit.author.name}"
  end
  message = "#{message} #{commit.message}"
  message.strip!
  post(message, options.update(:author => commit.author, 
    :authored_date => commit.authored_date))
end