diff --git a/motion/ruby_motion_query/utils.rb b/motion/ruby_motion_query/utils.rb index df837d9..94f1e9b 100644 --- a/motion/ruby_motion_query/utils.rb +++ b/motion/ruby_motion_query/utils.rb @@ -18,6 +18,16 @@ module RubyMotionQuery end end + # Converts any string to a friendly symbol version. + # Example: RubyMotionQuery::RMQ.symbolize("This is a TEST!!") + # #=> :this_is_a_test + # + # @param [String] + # @return [Symbol] + def symbolize(s) + s.to_s.gsub(/\s+/,"_").gsub(/\W+/,"").downcase.to_sym + end + # @param view # @return [UIViewController] The controller the view it is sitting in, or nil if it's not sitting anywhere in particular def controller_for_view(view) diff --git a/spec/utils.rb b/spec/utils.rb index ef46dd2..aff8932 100644 --- a/spec/utils.rb +++ b/spec/utils.rb @@ -19,7 +19,7 @@ describe 'utils' do end it 'should return false if module is passed to is_class?' do - @rmq.is_class?(TestIsClassModule).should == false + @rmq.is_class?(TestIsClassModule).should == false end it 'should return false if non class is passed to is_class?' do @@ -52,6 +52,12 @@ describe 'utils' do @rmq.is_blank?({not_emtpy: true}).should == false end + it 'should return a usable snake_cased symbol' do + @rmq.symbolize("Help").should == :help + @rmq.symbolize("This is a test").should == :this_is_a_test + @rmq.symbolize("This Works Great!!!").should == :this_works_great + end + it 'should return a string of a view' do v = UIView.alloc.initWithFrame(CGRectZero) s = @rmq.view_to_s(v)