class Simulacrum::Component

Component Class is responsible for managing the testing of a component defined in a test suite

Attributes

name[R]
options[RW]

Public Class Methods

new(name, options = {}) click to toggle source
# File lib/simulacrum/component.rb, line 14
def initialize(name, options = {})
  @name = name
  @options = options
  @renderer = Simulacrum::Renderer.new(options.url)
end

Public Instance Methods

candidate?() click to toggle source
# File lib/simulacrum/component.rb, line 34
def candidate?
  File.exist?(candidate_path)
end
candidate_path() click to toggle source
# File lib/simulacrum/component.rb, line 53
def candidate_path
  # TODO: These should probably be `configuration.defaults....`
  filename = Simulacrum.configuration.candidate_filename
  File.join(root_path, "#{filename}.png")
end
delta_threshold() click to toggle source
# File lib/simulacrum/component.rb, line 42
def delta_threshold
  # TODO: These should probably be `configuration.defaults....`
  options.delta_threshold || Simulacrum.configuration.delta_threshold
end
diff?() click to toggle source
# File lib/simulacrum/component.rb, line 38
def diff?
  File.exist?(diff_path)
end
diff_path() click to toggle source
# File lib/simulacrum/component.rb, line 59
def diff_path
  # TODO: These should probably be `configuration.defaults....`
  filename = Simulacrum.configuration.diff_filename
  File.join(root_path, "#{filename}.png")
end
reference?() click to toggle source
# File lib/simulacrum/component.rb, line 30
def reference?
  File.exist?(reference_path)
end
reference_path() click to toggle source
# File lib/simulacrum/component.rb, line 47
def reference_path
  # TODO: These should probably be `configuration.defaults....`
  filename = Simulacrum.configuration.reference_filename
  File.join(root_path, "#{filename}.png")
end
remove_candidate() click to toggle source
# File lib/simulacrum/component.rb, line 65
def remove_candidate
  FileUtils.rm(candidate_path) if candidate?
end
remove_diff() click to toggle source
# File lib/simulacrum/component.rb, line 69
def remove_diff
  FileUtils.rm(diff_path) if diff?
end
render() click to toggle source
# File lib/simulacrum/component.rb, line 20
def render
  ensure_example_path
  tmp_path = @renderer.render
  save_candidate(tmp_path)
  crop_candidate_to_selector
  true
ensure
  cleanup
end

Private Instance Methods

capture_selector() click to toggle source
# File lib/simulacrum/component.rb, line 108
def capture_selector
  options.capture_selector || Simulacrum.configuration.capture_selector
end
cleanup() click to toggle source
# File lib/simulacrum/component.rb, line 75
def cleanup
  @renderer.cleanup
end
crop_candidate_to_selector() click to toggle source
# File lib/simulacrum/component.rb, line 83
def crop_candidate_to_selector
  unless capture_selector.nil?
    candidate_image = Magick::Image.read(candidate_path).first
    bounds = @renderer.get_bounds_for_selector(capture_selector)
    candidate_image.crop!(*bounds)
    candidate_image.write(candidate_path)
  end
end
driver_path() click to toggle source
# File lib/simulacrum/component.rb, line 100
def driver_path
  Capybara.current_driver == :default ? '' : Capybara.current_driver.to_s
end
ensure_example_path() click to toggle source
# File lib/simulacrum/component.rb, line 104
def ensure_example_path
  FileUtils.mkdir_p(root_path)
end
root_path() click to toggle source
# File lib/simulacrum/component.rb, line 92
def root_path
  File.join(
    Simulacrum.configuration.references_path,
    name.to_s,
    driver_path
  )
end
save_candidate(tmp_image_path) click to toggle source
# File lib/simulacrum/component.rb, line 79
def save_candidate(tmp_image_path)
  FileUtils.mv(tmp_image_path, candidate_path)
end