Class: SimpleFeed::Event

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/simplefeed/event.rb

Constant Summary collapse

COLOR_MAP =
{
  1 => ->(word) { word.green.bold },
  3 => ->(word) { word.yellow.bold },
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, value: nil, at: Time.now) ⇒ Event

Returns a new instance of Event.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simplefeed/event.rb', line 26

def initialize(*args, value: nil, at: Time.now)
  if args && !args.empty?
    self.value = args[0]
    self.at    = args[1]
  end

  self.value ||= value
  self.at    ||= at

  self.at = self.at.to_f

  validate!
end

Class Attribute Details

.is_timeObject

Returns the value of attribute is_time.



12
13
14
# File 'lib/simplefeed/event.rb', line 12

def is_time
  @is_time
end

Instance Attribute Details

#atObject

Returns the value of attribute at.



7
8
9
# File 'lib/simplefeed/event.rb', line 7

def at
  @at
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/simplefeed/event.rb', line 7

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object



48
49
50
# File 'lib/simplefeed/event.rb', line 48

def <=>(other)
  -self.at <=> -other.at
end

#==(other) ⇒ Object



56
57
58
59
# File 'lib/simplefeed/event.rb', line 56

def ==(other)
  other.is_a?(SimpleFeed::Event) &&
    self.value == other.value
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/simplefeed/event.rb', line 52

def eql?(other)
  self == other
end

#hashObject



61
62
63
# File 'lib/simplefeed/event.rb', line 61

def hash
  self.value.hash
end

#inspectObject



107
108
109
# File 'lib/simplefeed/event.rb', line 107

def inspect
  super
end

#timeObject



40
41
42
43
44
45
46
# File 'lib/simplefeed/event.rb', line 40

def time
  return nil unless Event.is_time[at]

  Time.at(at)
rescue ArgumentError
  nil
end

#to_color_sObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/simplefeed/event.rb', line 96

def to_color_s
  return @to_color_s if @to_color_s

  output = StringIO.new
  to_s.split(/[\[\]]/).each_with_index do |word, index|
    output.print(COLOR_MAP[index]&.call(word) || word.cyan)
  end
  output.print '>'
  @to_color_s = output.string
end

#to_hObject



73
74
75
76
77
78
79
# File 'lib/simplefeed/event.rb', line 73

def to_h
  return @to_h if @to_h

  @to_h ||= { value: value, at: at }
  @to_h.merge!(time: time) if time
  @to_h
end

#to_json(*_args) ⇒ Object



65
66
67
# File 'lib/simplefeed/event.rb', line 65

def to_json(*_args)
  to_h.to_json
end

#to_sObject



81
82
83
84
85
86
87
88
89
# File 'lib/simplefeed/event.rb', line 81

def to_s
  return @to_s if @to_s

  output = StringIO.new
  output.print "<SimpleFeed::Event: "
  output.print(time.nil? ? "[#{at}]" : "[#{time&.strftime(::SimpleFeed::TIME_FORMAT)}]")
  output.print ", [\"#{value}\"]"
  @to_s = output.string
end

#to_yamlObject



69
70
71
# File 'lib/simplefeed/event.rb', line 69

def to_yaml
  YAML.dump(to_h)
end