Class: Vishnu::StandardTime::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/vishnu/standard_time.rb

Defined Under Namespace

Classes: Block

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlan

Returns a new instance of Plan.



142
143
144
# File 'lib/vishnu/standard_time.rb', line 142

def initialize
  @blocks = []
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



140
141
142
# File 'lib/vishnu/standard_time.rb', line 140

def blocks
  @blocks
end

Instance Method Details

#block(name, flows: nil, seconds: nil, minutes: nil, hours: nil) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
# File 'lib/vishnu/standard_time.rb', line 146

def block(name, flows: nil, seconds: nil, minutes: nil, hours: nil)
  count = flows || Vishnu::StandardTime.flows_for(seconds: seconds, minutes: minutes, hours: hours)
  count = Integer(count)
  raise ArgumentError, "block flows must be positive" unless count.positive?

  @blocks << Block.new(name: name.to_s, flows: count)
end

#percent_of_dayObject



162
163
164
# File 'lib/vishnu/standard_time.rb', line 162

def percent_of_day
  (total_flows.to_f / Vishnu::StandardTime::FLOWS_PER_DAY) * 100
end

#summaryObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/vishnu/standard_time.rb', line 175

def summary
  width = [blocks.map { |b| b.name.length }.max || 4, 4].max
  lines = blocks.map do |entry|
    "%#{width}s  %9s flows  %7.2f min  %6.2f%%" % [
      entry.name,
      Vishnu::StandardTime.format_number(entry.flows),
      entry.minutes,
      entry.percent_of_day
    ]
  end
  lines << ""
  lines << "%#{width}s  %9s flows  %7.2f min  %6.2f%%" % [
    "Total",
    Vishnu::StandardTime.format_number(total_flows),
    total_minutes,
    percent_of_day
  ]
  lines.join("\n")
end

#to_hObject



166
167
168
169
170
171
172
173
# File 'lib/vishnu/standard_time.rb', line 166

def to_h
  {
    blocks: blocks.map(&:to_h),
    total_flows: total_flows,
    total_minutes: total_minutes,
    percent_of_day: percent_of_day.round(4)
  }
end

#total_flowsObject



154
155
156
# File 'lib/vishnu/standard_time.rb', line 154

def total_flows
  blocks.sum(&:flows)
end

#total_minutesObject



158
159
160
# File 'lib/vishnu/standard_time.rb', line 158

def total_minutes
  Vishnu::StandardTime.minutes_for(flows: total_flows)
end