Skip to content

Commit

Permalink
Use defined_method to define shorthand methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seanhotw committed Sep 30, 2012
1 parent 59387d7 commit f430b01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
14 changes: 13 additions & 1 deletion lib/simple_view/extensions/string.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
module SimpleView
module String
def underscore
word = self.dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end

def to_color
html_colour = self.gsub(%r{[#;]}, '')
html_colour = self.dup
html_colour.gsub!(%r{[#;]}, '')

case html_colour.size
when 3
colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
Expand Down
33 changes: 11 additions & 22 deletions lib/simple_view/view_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,16 @@ def add klass, options = {}, &block
subview
end

def activity_indicator(options = {}, &block) add(::UIActivityIndicatorView, options, &block); end
def button(options = {}, &block) add(::UIButton, options, &block); end
def date_picker(options = {}, &block) add(::UIDatePicker, options, &block); end
def image_view(options = {}, &block) add(::UIImageView, options, &block); end
def label(options = {}, &block) add(::UILabel, options, &block); end
def page_control(options = {}, &block) add(::UIPageControl, options, &block); end
def picker_view(options = {}, &block) add(::UIPickerView, options, &block); end
def progress_view(options = {}, &block) add(::UIProgressView, options, &block); end
def rect(options = {}, &block) add(::UIView, options, &block); end
def scroll_view(options = {}, &block) add(::UIScrollView, options, &block); end
def search_bar(options = {}, &block) add(::UISearchBar, options, &block); end
def segmented_control(options = {}, &block) add(::UISegmentedControl, options, &block); end
def slider(options = {}, &block) add(::UISlider, options, &block); end
def stepper(options = {}, &block) add(::UIStepper, options, &block); end
def switch(options = {}, &block) add(::UISwitch, options, &block); end
def tab_bar(options = {}, &block) add(::UITabBar, options, &block); end
def table_view(options = {}, &block) add(::UITableView, options, &block); end
def table_view_cell(options = {}, &block) add(::UITableViewCell, options, &block); end
def text_field(options = {}, &block) add(::UITextField, options, &block); end
def text_view(options = {}, &block) add(::UITextView, options, &block); end
def toolbar(options = {}, &block) add(::UIToolbar, options, &block); end
def web_view(options = {}, &block) add(::UIWebView, options, &block); end
[::UIActivityIndicatorView, ::UIButton, ::UIDatePicker, ::UIImageView, ::UILabel, ::UIPageControl,
::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl, ::UISlider,
::UIStepper, ::UISwitch, ::UITabBar, ::UITableView, ::UITableViewCell, ::UITextField, ::UITextView,
::UIToolbar, ::UIWebView].each do |klass|
shorthand = "#{klass}"[2..-1].underscore.to_sym
define_method(shorthand) do |*args, &block|
options = args[0] || {}
add(klass, options, &block)
end
end
def rect(options = {}, &block) add(::UIView, options, &block); end
end
end
4 changes: 4 additions & 0 deletions spec/simple_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class DummyController
@proxy = SimpleView::ViewProxy.new
end

it "should create UIActivityIndicatorView" do
@proxy.activity_indicator_view.class.should == UIActivityIndicatorView
end

it "should create UIButton" do
@proxy.button.class.should == UIRoundedRectButton
end
Expand Down

0 comments on commit f430b01

Please sign in to comment.