lib/simulacrum/component.rb in simulacrum-0.0.3 vs lib/simulacrum/component.rb in simulacrum-0.1.0
- old
+ new
@@ -1,98 +1,101 @@
require 'capybara/dsl'
+require 'fileutils'
+require 'rmagick'
+require_relative 'renderer'
module Simulacrum
class Component
- include Capybara::DSL
-
attr_reader :name, :browser
attr_accessor :options
- def initialize(name, browser = nil, options = {})
+ def initialize(name, options = {})
@name = name
@options = options
- @browser = browser
+ @renderer = Simulacrum::Renderer.new(options.url)
end
# Load up the component url and capture an image, returns a File object
def render
- use_browser
ensure_example_path
- visit(@options.url)
- page.driver.save_screenshot(candidate_path, options)
- kill_driver
+ save_candidate(@renderer.render)
+ crop_candidate_to_selector
+ ensure
+ cleanup
end
- def render_with(browser)
- self.class.new(name, browser, @options)
+ def reference?
+ File.exists?(reference_path)
end
- def remove_candidate
- FileUtils.rm(candidate_path) if candidate?
+ def candidate?
+ File.exists?(candidate_path)
end
- def remove_diff
- FileUtils.rm(diff_path) if diff?
+ def diff?
+ File.exists?(diff_path)
end
- def root_path
- File.join(Simulacrum.configuration.images_path, render_path)
+ def acceptable_delta
+ options.acceptable_delta || Simulacrum.configuration.acceptable_delta
end
def reference_path
- File.join(root_path, Simulacrum.configuration.reference_filename)
+ filename = Simulacrum.configuration.reference_filename
+ File.join(root_path, "#{filename}.png")
end
def candidate_path
- File.join(root_path, Simulacrum.configuration.candidate_filename)
+ filename = Simulacrum.configuration.candidate_filename
+ File.join(root_path, "#{filename}.png")
end
def diff_path
- File.join(root_path, Simulacrum.configuration.diff_filename)
+ filename = Simulacrum.configuration.diff_filename
+ File.join(root_path, "#{filename}.png")
end
- def reference?
- File.exists?(reference_path)
+ def remove_candidate
+ FileUtils.rm(candidate_path) if candidate?
end
- def candidate?
- File.exists?(candidate_path)
+ def remove_diff
+ FileUtils.rm(diff_path) if diff?
end
- def diff?
- File.exists?(diff_path)
+ private
+
+ def cleanup
+ @renderer.cleanup
+ # FileUtils.remove_entry(root_path) unless reference? || candidate? || diff?
end
- def acceptable_delta
- @options.acceptable_delta || Simulacrum.configuration.acceptable_delta
+ def save_candidate(tmp_image_path)
+ FileUtils.mv(tmp_image_path, candidate_path)
end
- private
-
- def kill_driver
- if not @browser.nil? and @browser.remote?
- page.driver.quit
+ 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
- def render_path
- path = [name.to_s]
- path << @browser.name unless @browser.nil?
- File.join(path.map(&:to_s))
+ def root_path
+ File.join(Simulacrum.configuration.references_path, name.to_s, driver_path)
end
- def use_browser
- unless @browser.nil?
- @browser.use
- sleep @browser.capture_delay.to_i
- end
+ def driver_path
+ Capybara.current_driver == :default ? '' : Capybara.current_driver.to_s
end
def ensure_example_path
FileUtils.mkdir_p(root_path)
end
def capture_selector
- @options.capture_selector || Simulacrum.configuration.capture_selector
+ options.capture_selector || Simulacrum.configuration.capture_selector
end
end
end