Class: Mumukit::Auth::Slug

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/auth/slug.rb

Direct Known Subclasses

Normalized

Defined Under Namespace

Classes: Normalized

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, second) ⇒ Slug

Returns a new instance of Slug.



17
18
19
20
21
22
23
# File 'lib/mumukit/auth/slug.rb', line 17

def initialize(first, second)
  raise 'slug first part must be non-nil' unless first
  raise 'slug second part must be non-nil' unless second

  @first = first
  @second = second
end

Instance Attribute Details

#firstObject Also known as: organization

Returns the value of attribute first.



9
10
11
# File 'lib/mumukit/auth/slug.rb', line 9

def first
  @first
end

#secondObject Also known as: repository, course, content

Returns the value of attribute second.



9
10
11
# File 'lib/mumukit/auth/slug.rb', line 9

def second
  @second
end

Class Method Details

.anyObject



105
106
107
# File 'lib/mumukit/auth/slug.rb', line 105

def self.any
  parse '_/_'
end

.from_options(hash) ⇒ Object



79
80
81
82
83
# File 'lib/mumukit/auth/slug.rb', line 79

def self.from_options(hash)
  first = hash[:first] || hash[:organization]
  second = hash[:second] || hash[:repository] || hash[:course] || hash[:content]
  new(first, second)
end

.join(*parts) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/mumukit/auth/slug.rb', line 85

def self.join(*parts)
  raise Mumukit::Auth::InvalidSlugFormatError, 'Slugs must have up to two parts' if parts.length > 2

  if parts.first.is_a? Hash
    from_options parts.first
  else
    new(*parts.pad_with('_', 2))
  end
end

.join_s(*args) ⇒ Object



95
96
97
# File 'lib/mumukit/auth/slug.rb', line 95

def self.join_s(*args)
  join(*args).to_s
end

.normalize(first, second) ⇒ Object



109
110
111
# File 'lib/mumukit/auth/slug.rb', line 109

def self.normalize(first, second)
  Normalized.new(first, second)
end

.parse(slug) ⇒ Object



99
100
101
102
103
# File 'lib/mumukit/auth/slug.rb', line 99

def self.parse(slug)
  validate_slug! slug

  self.new *slug.split('/')
end

Instance Method Details

#==(o) ⇒ Object



37
38
39
# File 'lib/mumukit/auth/slug.rb', line 37

def ==(o)
  o.is_a?(Mumukit::Auth::Slug) && self.normalize.eql?(o.normalize)
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mumukit/auth/slug.rb', line 41

def eql?(o)
  o.is_a?(Mumukit::Auth::Slug) && to_s == o.to_s
end

#hashObject



45
46
47
# File 'lib/mumukit/auth/slug.rb', line 45

def hash
  to_s.hash
end

#inspectObject



71
72
73
# File 'lib/mumukit/auth/slug.rb', line 71

def inspect
  "<Mumukit::Auth::Slug #{to_s}>"
end

#match_first(first) ⇒ Object



25
26
27
# File 'lib/mumukit/auth/slug.rb', line 25

def match_first(first)
  match self.first, first
end

#match_second(second) ⇒ Object



29
30
31
# File 'lib/mumukit/auth/slug.rb', line 29

def match_second(second)
  match self.second, second
end

#normalizeObject



59
60
61
# File 'lib/mumukit/auth/slug.rb', line 59

def normalize
  Normalized.new(first, second)
end

#normalize!Object



53
54
55
56
57
# File 'lib/mumukit/auth/slug.rb', line 53

def normalize!
  @first = normalize_part @first
  @second = normalize_part @second
  self
end

#normalized?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/mumukit/auth/slug.rb', line 67

def normalized?
  normalize.eql? self
end

#normalized_sObject



63
64
65
# File 'lib/mumukit/auth/slug.rb', line 63

def normalized_s
  normalize.to_s
end

#rebase(new_organizaton) ⇒ Object



33
34
35
# File 'lib/mumukit/auth/slug.rb', line 33

def rebase(new_organizaton)
  self.class.new new_organizaton, repository
end

#to_mumukit_slugObject



75
76
77
# File 'lib/mumukit/auth/slug.rb', line 75

def to_mumukit_slug
  self
end

#to_sObject



49
50
51
# File 'lib/mumukit/auth/slug.rb', line 49

def to_s
  "#{first}/#{second}"
end