Skip to content

Instantly share code, notes, and snippets.

@jacklynrose
Created March 5, 2014 11:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacklynrose/9365600 to your computer and use it in GitHub Desktop.
Save jacklynrose/9365600 to your computer and use it in GitHub Desktop.
SUPER basic clone of RMQ's stylesheet and selectors (no symbol based selection or fancy stylers, and other missing functionality like chaining)
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = MyViewController.new
@window.makeKeyAndVisible
true
end
end
class MyStylesheet
def bw_big(v)
v.backgroundColor = UIColor.blackColor
v.textColor = UIColor.whiteColor
v.font = UIFont.systemFontOfSize(36)
v.frame = [[20, 40], [280, 50]]
v.textAlignment = UITextAlignmentCenter
end
def white_bg(v)
v.backgroundColor = UIColor.whiteColor
end
end
class MyViewController < UIViewController
def viewDidLoad
super
rmq.stylesheet = MyStylesheet.new # works absolutely anywhere
rmq(self.view).style(:white_bg)
self.view.addSubview(rmq(UILabel).style(:bw_big) do |v|
v.text = "Testing"
end)
end
end
class Object
def rmq(selector = nil)
RMQ.shared.selector = selector
RMQ.shared
end
end
class RMQ
attr_accessor :stylesheet, :selector
def self.shared
Dispatch.once { @shared ||= new }
@shared
end
def style(style_name)
begin
puts "\[RMQ ERROR] no stylesheet selected" unless self.stylesheet
view = self.selector
view = view.new if view.class == Class
self.stylesheet.public_send(style_name, view)
yield view if block_given?
view
rescue NoMethodError => e
if e.message =~ /.*#{style_name.to_s}.*/
puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(v)' to #{stylesheet.class.name} class\n\n"
else
raise e
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment