Skip to content

Instantly share code, notes, and snippets.

@jacklynrose
Last active December 27, 2015 01:19
Show Gist options
  • Save jacklynrose/7244091 to your computer and use it in GitHub Desktop.
Save jacklynrose/7244091 to your computer and use it in GitHub Desktop.
The crazy method I created to refactor out code duplication around creating views and making them testable! Ewwww...
# This one!
def viewNamed(view_name, ofClass: class_reference)
var_name = view_name[0].downcase + view_name[1, view_name.length - 1].gsub(/\s/, '')
instance_variable_get("@#{var_name}") or instance_variable_set("@#{var_name}", class_reference.new).tap do |view|
view.accessibilityLabel = view_name
yield view if block_given?
end
end
# How I'm using it looks nice though
def toolbar
viewNamed('Toolbar', ofClass: UIToolbar) do |view|
view.items = [nextTweetButton]
end
end
def nextTweetButton
viewNamed('Next Tweet', ofClass: UIBarButtonItem) do |view|
view.title = 'Next Tweet'
end
end
def userImage
viewNamed('User Image', ofClass: UIImageView)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment