Class: WatchTower::Server::Presenters::ApplicationPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/watch_tower/server/presenters/application_presenter.rb

Direct Known Subclasses

FilePresenter, ProjectPresenter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, template) ⇒ ApplicationPresenter

Initialise the presenter

Parameters:

  • Model (ActiveRecord::Base)
  • Template (Object)


23
24
25
26
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 23

def initialize(model, template)
  @model = model
  @template = template
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Overwrite Kernel#method_missing to invoke either the model or the template’s method if present, if not Kernel#method_missing will be called

Parameters:

  • method: (Symbol)

    The method name

  • arguments (Array)
  • (Block)


35
36
37
38
39
40
41
42
43
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 35

def method_missing(method, *args, &block)
  if model.respond_to?(method)
    model.send(method, *args, &block)
  elsif template.respond_to?(method)
    template.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 8

def model
  @model
end

#templateObject (readonly)

Returns the value of attribute template.



8
9
10
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 8

def template
  @template
end

Class Method Details

.presents(name) ⇒ Object

Presents a model

Parameters:

  • name (Symbol)


13
14
15
16
17
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 13

def self.presents(name)
  define_method name do
    @model
  end
end

Instance Method Details

#approximate_elapsed(elapsed_time = nil) ⇒ String

Returns an approximate elapsed time

Parameters:

  • elapsed_time (Integer) (defaults to: nil)

Returns:

  • (String)

    The approximate elapsed time



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 59

def approximate_elapsed(elapsed_time = nil)
  return "" if elapsed_time.nil? && !model.respond_to?(:elapsed_time)
  elapsed_time ||= model.elapsed_time

  if elapsed_time > 1.day
    elapsed_t = (elapsed_time / 1.day).to_i * 1.day
    elapsed_f = elapsed(elapsed_t)

    if elapsed_time % 1.day >= 20.hours
      elapsed(elapsed_t + 1.day)
    elsif elapsed_time % 1.day >= 5.hours
      "#{elapsed_f} and a half"
    else
      elapsed_f
    end
  elsif elapsed_time > 1.hour
    elapsed_t = (elapsed_time / 1.hour).to_i * 1.hour
    elapsed_f = elapsed(elapsed_t)

    if elapsed_time % 1.hour >= 50.minutes
      elapsed(elapsed_t + 1.hour)
    elsif elapsed_time % 1.hour >= 25.minutes
      "#{elapsed_f} and a half"
    else
      elapsed_f
    end
  elsif elapsed_time > 60.seconds
    elapsed_time = (elapsed_time / 60).to_i * 60
    elapsed(elapsed_time)
  else
    '1 minute'
  end
end

#elapsed(elapsed_time = nil) ⇒ String

Returns a human formatted time

Parameters:

  • elapsed_time (Integer) (defaults to: nil)

Returns:

  • (String)

    The elapsed time formatted



49
50
51
52
53
# File 'lib/watch_tower/server/presenters/application_presenter.rb', line 49

def elapsed(elapsed_time = nil)
  return "" if elapsed_time.nil? && !model.respond_to?(:elapsed_time)
  elapsed_time ||= model.elapsed_time
  humanize_time elapsed_time
end