Sha256: 66b796f917d40a064a7bdd817f01b40af407d177dc50d1fe78c73f3db08d102d

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

module Grit

  class Actor
    attr_reader :name
    attr_reader :email

    def initialize(name, email)
      @name = name
      @email = email
    end
    alias_method :to_s, :name

    # Create an Actor from a string.
    #
    # str - The String in this format: 'John Doe <jdoe@example.com>'
    #
    # Returns Git::Actor.
    def self.from_string(str)
      case str
        when /<.+>/
          m, name, email = *str.match(/(.*) <(.+?)>/)
          return self.new(name, email)
        else
          return self.new(str, nil)
      end
    end

    # Outputs an actor string for Git commits.
    #
    #   actor = Actor.new('bob', 'bob@email.com')
    #   actor.output(time) # => "bob <bob@email.com> UNIX_TIME +0700"
    #
    # time - The Time the commit was authored or committed.
    #
    # Returns a String.
    def output(time)
      out = @name.to_s.dup
      if @email
        out << " <#{@email}>"
      end
      hours = (time.utc_offset.to_f / 3600).to_i # 60 * 60, seconds to hours
      rem   = time.utc_offset.abs % 3600
      out << " #{time.to_i} #{hours >= 0 ? :+ : :-}#{hours.abs.to_s.rjust(2, '0')}#{rem.to_s.rjust(2, '0')}"
    end

    # Pretty object inspection
    def inspect
      %Q{#<Grit::Actor "#{@name} <#{@email}>">}
    end
  end # Actor

end # Grit

Version data entries

8 entries across 8 versions & 5 rubygems

Version Path
jugyo-grit-2.4.2 lib/grit/actor.rb
rhomobile-grit-2.4.2 lib/grit/actor.rb
rhomobile-grit-2.4.1 lib/grit/actor.rb
grit-2.4.1 lib/grit/actor.rb
grit-2.4.0 lib/grit/actor.rb
schleyfox-grit-2.3.0.1 lib/grit/actor.rb
schleyfox-grit-2.3.0 lib/grit/actor.rb
madrox-0.2.0 vendor/grit/lib/grit/actor.rb