capitalism-journal

Track any event.

Payment, temperature, sleep, mood, thought, etc.

Installation

Add this line to your application's Gemfile:

gem 'capitalism-journal'

And execute:

$ bundle

Or install without bundler:

$ gem install capitalism-journal

Creating a journal

journal = Capitalism::Journal.new

Adding events

Explicit timestamp and value:

journal.add(timestamp: Time.now, value: true)

Default values:

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

Helper methods:

# journal.add(timestamp: Time.now, value: 1)
journal.add_value(1)

# journal.add(timestamp: timestamp, value: true)
journal.add_timestamp(timestamp)

Validating events

By default journal accepts events with arbitrary timestamps and values.

For a custom validation inject a class that responds to #call.(event):

event_validator = -> (_event) { false }
journal = Capitalism::Journal.new(event_validator)
journal.add
=> Capitalism::Journal::InvalidEvent

Reading events

Journal stores and returns events in the order they were added.

Think of Journal#add as an alias for Array.push.

Getting events with timestamps and values:

journal = Capitalism::Journal.new
journal.add_timestamp(Time.now)
journal.add_timestamp((DateTime.now - 3).to_time)    
journal.events
=> [
    #<OpenStruct timestamp=2017-10-22 21:13:14 +0300 value=true>, 
    #<OpenStruct timestamp=2017-10-19 21:13:17 +0300 value=true>
]
puts journal.events.map(&:to_h)
=> {:timestamp=>2017-10-22 21:13:14 +0300, :value=>true}
=> {:timestamp=>2017-10-19 21:13:17 +0300, :value=>true}

Timestamps only:

# journal.events.map(&:timestamp)
journal.timestamps

Values only:

# journal.events.map(&:value)
journal.values

Removing events

# always returns nil
journal.delete_at(index)

Usage examples

TrueClass

lightning.add

Boolean

target_hit.add_value(false)

Integer

pushups.add_value(18)

Float

temperature.add_value(36.6)

Decimal

currency_exchange_rate.add_value 2.0001.to_d    

Duration

formula_one_lap_time.add_value 84125 # in milliseconds

String

diary.add_value('Released Journal gem.')

Symbol

coin_flip.add_value(:heads)

Hash

contact_form.add(value: { from: '[email protected]', message: 'Good news!' })