Class: PlanMyStuff::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/plan_my_stuff/repo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, organization:, key: nil) ⇒ Repo

Returns a new instance of Repo.

Parameters:

  • name (String)
  • organization (String)
  • key (Symbol, nil) (defaults to: nil)


97
98
99
100
101
# File 'lib/plan_my_stuff/repo.rb', line 97

def initialize(name:, organization:, key: nil)
  @key = key
  @name = name
  @organization = organization
end

Instance Attribute Details

#keySymbol? (readonly)

Returns configured key (e.g. :my_repo).

Returns:

  • (Symbol, nil)

    configured key (e.g. :my_repo)



6
7
8
# File 'lib/plan_my_stuff/repo.rb', line 6

def key
  @key
end

#nameString (readonly)

Returns repo name (e.g. “MyRepository”).

Returns:

  • (String)

    repo name (e.g. “MyRepository”)



9
10
11
# File 'lib/plan_my_stuff/repo.rb', line 9

def name
  @name
end

#organizationString (readonly)

Returns organization name (e.g. “YourOrgName”).

Returns:

  • (String)

    organization name (e.g. “YourOrgName”)



12
13
14
# File 'lib/plan_my_stuff/repo.rb', line 12

def organization
  @organization
end

Class Method Details

.from_nickname!(nickname) ⇒ PlanMyStuff::Repo

Reverse lookup for the Issue#to_param prefix: finds the configured repo whose nickname (per config.repo_nickname_for) matches nickname and returns its Repo instance.

Parameters:

  • nickname (String)

Returns:

Raises:

  • (ArgumentError)

    if no configured repo has the given nickname



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

def from_nickname!(nickname)
  config = PlanMyStuff.configuration
  match = config.repos.keys.find { |key| config.repo_nickname_for(key) == nickname }
  raise(ArgumentError, "Unknown repo nickname: #{nickname.inspect}") if match.nil?

  resolve!(match)
end

.resolve!(repo = nil) ⇒ PlanMyStuff::Repo

Builds a Repo instance from a Symbol key, full name String, or nil (default).

Parameters:

Returns:

Raises:

  • (PlanMyStuff::ConfigurationError)

    if repo is not provided and cannot be resolved from config

  • (ArgumentError)

    if repo cannot be resolved

  • (ArgumentError)

    if repo is invalid format



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
54
55
56
# File 'lib/plan_my_stuff/repo.rb', line 27

def resolve!(repo = nil)
  return repo if repo.is_a?(PlanMyStuff::Repo)

  repo ||= PlanMyStuff.configuration.default_repo

  if repo.nil?
    raise(
      PlanMyStuff::ConfigurationError,
      'No repo provided and config.default_repo is not set. ' \
        'Either pass repo: explicitly or set config.default_repo in your initializer.',
    )
  end

  case repo
  when Symbol
    full_name = PlanMyStuff.configuration.repos[repo]
    raise(ArgumentError, "Unknown repo key: #{repo.inspect}") if full_name.nil?

    from_full_name!(full_name, key: repo)
  when String
    if PlanMyStuff.configuration.repos.has_key?(repo.to_sym)
      resolve!(repo.to_sym)
    else
      key = PlanMyStuff.configuration.repos.key(repo)
      from_full_name!(repo, key: key)
    end
  else
    raise(ArgumentError, "Cannot resolve repo: #{repo.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compares by full_name. Accepts another Repo or a String.

Parameters:

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
# File 'lib/plan_my_stuff/repo.rb', line 134

def ==(other)
  case other
  when PlanMyStuff::Repo
    full_name == other.full_name
  when String
    full_name == other
  else
    super
  end
end

#full_nameString Also known as: to_s, to_str

Returns full repo path (e.g. “YourOrgName/MyRepository”).

Returns:

  • (String)

    full repo path (e.g. “YourOrgName/MyRepository”)



104
105
106
# File 'lib/plan_my_stuff/repo.rb', line 104

def full_name
  "#{organization}/#{name}"
end

#nicknameString

Human-readable repo label used as the Issue#to_param prefix. Resolves through config.repo_nickname_for when this repo carries a configured key; falls back to the bare repo name for unconfigured repos.

Returns:

  • (String)


113
114
115
116
117
# File 'lib/plan_my_stuff/repo.rb', line 113

def nickname
  return PlanMyStuff.configuration.repo_nickname_for(key) if key

  name
end