Class: SimpleFeed::Event
- Inherits:
-
Object
- Object
- SimpleFeed::Event
- 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
-
.is_time ⇒ Object
Returns the value of attribute is_time.
Instance Attribute Summary collapse
-
#at ⇒ Object
Returns the value of attribute at.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(*args, value: nil, at: Time.now) ⇒ Event
constructor
A new instance of Event.
- #inspect ⇒ Object
- #time ⇒ Object
- #to_color_s ⇒ Object
- #to_h ⇒ Object
- #to_json(*_args) ⇒ Object
- #to_s ⇒ Object
- #to_yaml ⇒ Object
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_time ⇒ Object
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
#at ⇒ Object
Returns the value of attribute at.
7 8 9 |
# File 'lib/simplefeed/event.rb', line 7 def at @at end |
#value ⇒ Object
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
52 53 54 |
# File 'lib/simplefeed/event.rb', line 52 def eql?(other) self == other end |
#hash ⇒ Object
61 62 63 |
# File 'lib/simplefeed/event.rb', line 61 def hash self.value.hash end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/simplefeed/event.rb', line 107 def inspect super end |
#time ⇒ Object
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_s ⇒ Object
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_h ⇒ Object
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_s ⇒ Object
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_yaml ⇒ Object
69 70 71 |
# File 'lib/simplefeed/event.rb', line 69 def to_yaml YAML.dump(to_h) end |