Class: Mumukit::Auth::Slug

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

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



97
98
99
# File 'lib/mumukit/auth/slug.rb', line 97

def self.any
  parse '_/_'
end

.from_options(hash) ⇒ Object



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

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



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

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



87
88
89
# File 'lib/mumukit/auth/slug.rb', line 87

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

.normalize(first, second) ⇒ Object



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

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

.parse(slug) ⇒ Object



91
92
93
94
95
# File 'lib/mumukit/auth/slug.rb', line 91

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)
  self.class == o.class && 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)
  self.class == o.class && 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



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

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
  dup.normalize!
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

#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



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

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