Class: Fiveruns::Tuneup::Step

Inherits:
RootStep
  • Object
show all
Defined in:
lib/fiveruns/tuneup/step.rb

Direct Known Subclasses

DisparityStep

Defined Under Namespace

Classes: Extra

Instance Attribute Summary collapse

Attributes inherited from RootStep

#bar, #children, #parent, #time

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RootStep

#add_child, #disparity, #format_time, #proportion, #record, #root, #to_html_with_children

Methods included from Templating

#h, #to_html

Constructor Details

#initialize(name, layer, raw_extras = {}, time = nil) ⇒ Step

Returns a new instance of Step.



120
121
122
123
124
125
# File 'lib/fiveruns/tuneup/step.rb', line 120

def initialize(name, layer, raw_extras = {}, time = nil)
  super(time)
  @name = name
  @layer = layer.to_sym
  @extras = build_extras(raw_extras)
end

Instance Attribute Details

#extrasObject (readonly)

Returns the value of attribute extras.



119
120
121
# File 'lib/fiveruns/tuneup/step.rb', line 119

def extras
  @extras
end

#layerObject (readonly)

Returns the value of attribute layer.



119
120
121
# File 'lib/fiveruns/tuneup/step.rb', line 119

def layer
  @layer
end

#nameObject (readonly)

Returns the value of attribute name.



119
120
121
# File 'lib/fiveruns/tuneup/step.rb', line 119

def name
  @name
end

Class Method Details

.inside(step) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/fiveruns/tuneup/step.rb', line 109

def self.inside(step)
  unless stack.empty?
    stack.last.add_child(step)
  end
  stack << step
  result = yield
  stack.pop
  result
end

.load(source, depth = 0) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fiveruns/tuneup/step.rb', line 85

def self.load(source, depth = 0)
  hash = source.is_a?(Hash) ? source : JSON.load(source)
  step = if hash['layer']
    Step.new(hash['name'], hash['layer'], hash['extras'], hash['time'])
  elsif depth == 0
    RootStep.new(hash['time'])
  else
    raise ArgumentError, "Could not find data for step in #{hash.inspect}"
  end
  hash['children'].each do |child_hash|
    child = load(child_hash)
    step.add_child(child)
  end
  step
end

.reset!Object



105
106
107
# File 'lib/fiveruns/tuneup/step.rb', line 105

def self.reset!
  stack.clear
end

.stackObject



101
102
103
# File 'lib/fiveruns/tuneup/step.rb', line 101

def self.stack
  @stack ||= []
end

Instance Method Details

#children_with_disparityObject



127
128
129
130
131
132
133
# File 'lib/fiveruns/tuneup/step.rb', line 127

def children_with_disparity
  return children if children.empty?
  layer_name = layer if respond_to?(:layer)
  extra_step = DisparityStep.new(layer_name, disparity)
  extra_step.parent = parent
  children + [extra_step]
end

#layer_portionsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fiveruns/tuneup/step.rb', line 135

def layer_portions  
  @layer_portions ||= begin
    result = {:model => 0, :view => 0, :controller => 0}
    if children.empty?
      result[layer] = 1
    else
      times = layer_times
      
      times[layer] ||= 0
      times.inject(result) do |all, (l, t)|
        result[l] = t / time
        result            
      end
    end
    result
  end
end

#layer_timesObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fiveruns/tuneup/step.rb', line 153

def layer_times
  if children.empty?
    return {layer => time}
  end
  
  totals = {:model => 0, :view => 0, :controller => 0}
  children_with_disparity.inject(totals) do |all, child|
    breakdown = child.layer_times
    breakdown.each { |l,t| all[l] += t }
    all
  end
  totals
end

#to_jsonObject



167
168
169
170
# File 'lib/fiveruns/tuneup/step.rb', line 167

def to_json
  extras_hash = extras.inject({}) { |all, extra| all[extra.name] = extra; all }
  {:name => name, :children => children, :time => time, :extras => extras_hash, :layer => layer}.to_json
end