Class: Publicacion::Pub

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/linkedlist/publicacion.rb

Overview

Clase Publicacion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(autores, titulo, fecha) ⇒ Pub

Returns a new instance of Pub.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
# File 'lib/linkedlist/publicacion.rb', line 10

def initialize(autores, titulo, fecha)
  # Comprobamos tipo
  fail ArgumentError, 'Autores debe ser un array' unless autores.is_a?(Array)
  #

  cadena = ''
  autores.each do |au|
    fail ArgumentError, 'Uno de los autores no es un string' unless au.is_a?(String)
    fail ArgumentError, 'Se especifica unicamente el nombre o el apellido' unless au.split(/\W+/).length > 1
    aux = au.split(/\W+/)
    cadena += aux[1]
    cadena += ', '
    unless aux[2].nil?
      cadena += aux[2][0]
      cadena += '. '
    end
    cadena += aux[0][0]
    cadena += '.'
    cadena += ' & ' unless au == autores.last
  end

  @autores = cadena

  # Comprobamos tipos
  fail ArgumentError, 'El titulo no es un string' unless titulo.is_a?(String)
  fail ArgumentError, 'La fecha no es de tipo Date' unless fecha.is_a?(Date)
  #

  aux2 = titulo.split(/\W+/)
  aux2.each do |aux3|
    if aux3.length > 3
      aux3.capitalize!
    else
      aux3.downcase!
    end
  end

  # Asignamos el titulo formateado
  @titulo = aux2.join(' ')
  #

  # Asignamos fecha
  @fecha = fecha
  #
end

Instance Attribute Details

#autoresObject

Se tiene acceso de lectura y escritura a todos los atributos



9
10
11
# File 'lib/linkedlist/publicacion.rb', line 9

def autores
  @autores
end

#fechaObject

Se tiene acceso de lectura y escritura a todos los atributos



9
10
11
# File 'lib/linkedlist/publicacion.rb', line 9

def fecha
  @fecha
end

#tituloObject

Se tiene acceso de lectura y escritura a todos los atributos



9
10
11
# File 'lib/linkedlist/publicacion.rb', line 9

def titulo
  @titulo
end

Instance Method Details

#<=>(other) ⇒ Object

Fin constructor



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/linkedlist/publicacion.rb', line 56

def <=>(other)
    if @autores == other.autores
      if @fecha == other.fecha
        if @titulo == other.titulo # Iguales
          return 0
        else
          aux = [@titulo, other.titulo]
          aux.sort_by!(&:downcase)
          return -1 if aux.first == @titulo
          return 1
          end
      elsif fecha > other.fecha
        return 1
      else
        return -1
      end
    else
      aux = [@autores, other.autores]
      aux.sort_by!(&:downcase)
      return 1 if aux.first == @autores
      return -1
      end
end