Class: Capitalism::Journal

Inherits:
Object
  • Object
show all
Defined in:
lib/capitalism/journal.rb,
lib/capitalism/journal/version.rb

Defined Under Namespace

Classes: InvalidEvent

Constant Summary collapse

VERSION =
'0.8.2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_validator = ->(_event) { true }) ⇒ Journal

Returns a new instance of Journal.



10
11
12
13
# File 'lib/capitalism/journal.rb', line 10

def initialize(event_validator = ->(_event) { true })
  @event_validator = event_validator
  @events = []
end

Instance Attribute Details

#event_validatorObject (readonly)

Returns the value of attribute event_validator.



8
9
10
# File 'lib/capitalism/journal.rb', line 8

def event_validator
  @event_validator
end

#eventsObject (readonly)

Returns the value of attribute events.



8
9
10
# File 'lib/capitalism/journal.rb', line 8

def events
  @events
end

Instance Method Details

#add(timestamp: Time.now, value: true) ⇒ Object

Raises:



15
16
17
18
19
20
# File 'lib/capitalism/journal.rb', line 15

def add(timestamp: Time.now, value: true)
  event = OpenStruct.new({ journal: self, timestamp: timestamp, value: value })
  raise InvalidEvent unless event_validator.call(event)
  @events << event
  nil
end

#add_timestamp(timestamp) ⇒ Object



26
27
28
# File 'lib/capitalism/journal.rb', line 26

def add_timestamp(timestamp)
  add(timestamp: timestamp)
end

#add_value(value) ⇒ Object



22
23
24
# File 'lib/capitalism/journal.rb', line 22

def add_value(value)
  add(value: value)
end

#delete_at(index) ⇒ Object



38
39
40
41
# File 'lib/capitalism/journal.rb', line 38

def delete_at(index)
  @events.delete_at(index)
  nil
end

#timestampsObject



30
31
32
# File 'lib/capitalism/journal.rb', line 30

def timestamps
  events.map(&:timestamp)
end

#valuesObject



34
35
36
# File 'lib/capitalism/journal.rb', line 34

def values
  events.map(&:value)
end