Skip to content

Commit

Permalink
Merge branch 'master' into 0.5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jan 12, 2016
2 parents ce80e1a + 1f542c3 commit 18443dd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/lotus/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def registry
#
# ApplicationLayout.template # => 'application'
def template
super.gsub(suffix, '')
super.sub(suffix, '')
end

# Template name suffix
Expand Down
45 changes: 43 additions & 2 deletions lib/lotus/view/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Configuration
#
# @since 0.5.0
# @api private
DEFAULT_ENCODING = 'utf-8'.freeze
DEFAULT_ENCODING = Encoding::UTF_8

attr_reader :load_paths
attr_reader :views
Expand Down Expand Up @@ -243,11 +243,52 @@ def layout(value = nil)
end
end

# Default encoding for templates
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload default_encoding(value)
# Sets the given value
# @param value [String,Encoding] a string representation of the encoding,
# or an Encoding constant
#
# @raise [ArgumentError] if the given value isn't a supported encoding
#
# @overload default_encoding
# Gets the value
# @return [Encoding]
#
# @since 0.5.0
#
# @example Set UTF-8 As A String
# require 'lotus/view'
#
# Lotus::View.configure do
# default_encoding 'utf-8'
# end
#
# @example Set UTF-8 As An Encoding Constant
# require 'lotus/view'
#
# Lotus::View.configure do
# default_encoding Encoding::UTF_8
# end
#
# @example Raise An Error For Unknown Encoding
# require 'lotus/view'
#
# Lotus::View.configure do
# default_encoding 'foo'
# end
#
# # => ArgumentError
def default_encoding(value = nil)
if value.nil?
@default_encoding
else
@default_encoding = value.to_s
@default_encoding = Encoding.find(value)
end
end

Expand Down
20 changes: 13 additions & 7 deletions test/view/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,19 @@ class LazyLayout
end

describe "#default_encoding" do
it 'defaults to "utf-8"' do
@configuration.default_encoding.must_equal "utf-8"
it 'defaults to Encoding::UTF_8' do
@configuration.default_encoding.must_equal Encoding::UTF_8
end

it 'allows to set different value' do
@configuration.default_encoding "koi-8"
@configuration.default_encoding.must_equal "koi-8"
encoding = Encoding.list.sample
@configuration.default_encoding encoding.to_s
@configuration.default_encoding.must_equal encoding
end

it 'raises error in case of unknown encoding' do
exception = -> { @configuration.default_encoding 'abc' }.must_raise ArgumentError
exception.message.must_equal 'unknown encoding name - abc'
end
end

Expand Down Expand Up @@ -193,7 +199,7 @@ class PrepareView
@configuration.root 'test'
@configuration.load_paths << '..'
@configuration.layout :application
@configuration.default_encoding 'latin-1'
@configuration.default_encoding 'UTF-7'
@configuration.add_view(HelloWorldView)
@configuration.add_layout(ApplicationLayout)
@configuration.prepare { include Kernel }
Expand All @@ -215,7 +221,7 @@ class PrepareView
@config.root '.'
@config.load_paths << '../..'
@config.layout :global
@config.default_encoding 'iso-8859'
@config.default_encoding 'iso-8859-1'
@config.add_view(RenderView)
@config.add_layout(GlobalLayout)
@config.prepare { include Comparable }
Expand All @@ -237,7 +243,7 @@ class PrepareView
@configuration.load_paths.must_include '..'
@configuration.load_paths.wont_include '../..'

@configuration.default_encoding.must_equal 'latin-1'
@configuration.default_encoding.must_equal Encoding::UTF_7

@configuration.layout.must_equal ApplicationLayout

Expand Down

0 comments on commit 18443dd

Please sign in to comment.