Sha256: 69023256a13710e93fef71b63e913ace955bfc380acd69a22cb67018a0e33ef6

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

module GitSu
    class User
        NONE = User.new
        def NONE.to_s
            "(none)"
        end
        def NONE.to_ansi_s(name_color, email_color, reset_color)
            "#{name_color}(none)#{reset_color}"
        end

        class ParseError < RuntimeError
        end

        attr_accessor :name, :email

        def User.parse(string)
            fully_qualified_user_regex = /^[^<]+<[^>]+>$/ 
            if string =~ fully_qualified_user_regex
                name = string[/^[^<]+/].strip
                email = string[/<.*>/].delete "[<>]" 
                User.new(name, email)
            else
                raise ParseError, "Couldn't parse '#{string}' as user (expected user in format: 'John Smith <jsmith@example.com>')"
            end
        end

        def initialize(name, email)
            @name, @email = name, email
        end

        def none?
            self === NONE
        end

        def ==(other)
            eql? other
        end

        def eql?(other)
            @name == other.name && @email == other.email
        end

        def hash
            to_s.hash
        end

        def to_ansi_s(name_color, email_color, reset_color)
            "#{name_color}#{@name}#{reset_color} #{email_color}<#{@email}>#{reset_color}"
        end

        def to_s
            to_ansi_s("", "", "")
        end
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gitsu-1.0.0 lib/gitsu/user.rb
gitsu-0.0.3 lib/gitsu/user.rb
gitsu-0.0.2 lib/gitsu/user.rb
gitsu-0.0.1 lib/gitsu/user.rb