Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/svenfuchs/couch_potato
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Mar 31, 2010
2 parents fce907d + 4a8eb41 commit ae0a743
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
22 changes: 11 additions & 11 deletions lib/core_ext/string.rb
Expand Up @@ -4,16 +4,16 @@ def camelize
$1.upcase
end
end

# Source
# http://github.com/rails/rails/blob/b600bf2cd728c90d50cc34456c944b2dfefe8c8d/activesupport/lib/active_support/inflector.rb
def underscore
gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
String.send :include, ActiveSupportMethods unless String.new.respond_to?(:underscore)

String.send :include, ActiveSupportMethods unless String.new.respond_to?(:underscore)
class String
# inspired by http://github.com/rails/rails/blob/b600bf2cd728c90d50cc34456c944b2dfefe8c8d/activesupport/lib/active_support/inflector.rb
def snake_case(seperator = '/')
string = seperator == '::' ? dup : gsub(/::/, '/')
string.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
string.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
string.tr!("-", "_")
string.downcase
end
end
2 changes: 1 addition & 1 deletion lib/couch_potato/view/base_view_spec.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(klass, view_name, options, view_parameters)
normalized_view_parameters = normalize_view_parameters view_parameters
assert_valid_view_parameters normalized_view_parameters
@klass = klass
@design_document = klass.to_s.underscore
@design_document = klass.to_s.snake_case('::')
@view_name = view_name
@options = options
@view_parameters = {}
Expand Down
11 changes: 8 additions & 3 deletions spec/unit/base_view_spec_spec.rb
Expand Up @@ -33,14 +33,19 @@
spec = CouchPotato::View::BaseViewSpec.new Object, 'all', {}, {:key => '1'..'2'}
spec.view_parameters.should == {:startkey => '1', :endkey => '2'}
end

it "should convert a plain value to a hash with a key" do
spec = CouchPotato::View::BaseViewSpec.new Object, 'all', {}, '2'
spec.view_parameters.should == {:key => '2'}
end


it "generates the design document path by snake_casing the class name but keeping double colons" do
spec = CouchPotato::View::BaseViewSpec.new 'Foo::BarBaz', '', {}, ''
spec.design_document.should == 'foo::bar_baz'
end

end

end


10 changes: 7 additions & 3 deletions spec/unit/string_spec.rb
Expand Up @@ -6,8 +6,12 @@
end
end

describe String, 'underscore' do
it "should underscore a string" do
'MyString'.underscore.should == 'my_string'
describe String, 'snake_case' do
it "should snake_case a string" do
'MyString'.snake_case.should == 'my_string'
end

it "should snake_case a string using a custom separator" do
'My::String'.snake_case('::').should == 'my::string'
end
end

0 comments on commit ae0a743

Please sign in to comment.