Permalink
Browse files

add custom formatting method

  • Loading branch information...
1 parent 7fb3c64 commit 9aa7127e267eb1759ce556646a1afaf690ea8851 @schorsch schorsch committed Jul 29, 2012
Showing with 67 additions and 36 deletions.
  1. +27 −0 README.rdoc
  2. +0 −3 king_placeholder.gemspec
  3. +1 −0 lib/king_placeholder.rb
  4. +7 −6 lib/king_placeholder/parser.rb
  5. +32 −27 spec/king_placeholder_spec.rb
View
@@ -44,6 +44,33 @@ Handle relations
@user.expand_placeholders("[company.name]")
=> MyCompany
+Set a custom formatting method(in format_placeholder) throught which all fields
+are run, if you have special money or date formatters.
+
+ class User
+ include KingPlaceholder
+ has_placeholders :created_at
+
+ def format_placeholder(field)
+ I18n.l( self.send(field) )
+ end
+ end
+
+Use callbacks to setup / teardown env variables
+
+ class User
+ include KingPlaceholder
+ has_placeholders :name, :email
+
+ def before_expand_placeholders
+ I18n.locale = self.language
+ end
+ def after_expand_placeholders
+ I18n.locale = nil
+ end
+ end
+
+
Also see specs
== TODO
View
@@ -18,9 +18,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'statemachine'
gem.add_runtime_dependency 'i18n'
gem.add_runtime_dependency 'activesupport'
- gem.add_runtime_dependency 'actionpack'
- #gem.add_runtime_dependency 'actionview'
- #gem.add_runtime_dependency 'king_views'
gem.add_development_dependency 'activerecord'
gem.add_development_dependency 'rdoc'
View
@@ -117,6 +117,7 @@ def expand_placeholders(content, opts={})
return content unless content.is_a?(String)
end
run_callbacks :expand_placeholders do
+ opts[:formatter] = :format_placeholder if self.respond_to?(:format_placeholder)
parser = KingPlaceholder::Parser.new(self, content, opts)
parser.sm.match
parser.result if parser.sm.state == :finished
@@ -1,8 +1,5 @@
require 'statemachine'
-require 'action_view' # king_views related suxs
-require 'action_controller' # king_views
-require 'king_views'
-
+require 'active_support/core_ext/string/inflections'
module KingPlaceholder
# Statemachine for placeholder substitution
# The statemachine is created and its state updated from within the Context
@@ -31,7 +28,6 @@ def self.create_with(machine_context)
# machine.sm.match
# machine.result
class Parser
- include ::KingFormat::FormattingHelper
# reference to statemachine
attr_accessor :sm
# incoming string
@@ -93,7 +89,12 @@ def cleanup
# namespace e.g. [price_to_pay]
def sub_string
return unless obj.is_placeholder?(@field)
- value = strfval(obj, @field)
+ value = if @opts[:formatter]
+ obj.send(@opts[:formatter], @field)
+ else
+ obj.send @field
+ end
+ #value = strfval(obj, @field)
@result.gsub!(@placeholder, value.to_s) if value
end
@@ -18,33 +18,41 @@ class Side
class Detail
include KingPlaceholder
- include KingFormat::MoneyFields
attr_accessor :int_field, :money_field, :secret_field, :currency
attr_accessor :master
- has_money_fields :money_field
has_placeholders :int_field, :money_field
end
describe 'Class with placeholders' do
before :each do
- I18n.locale = :en_master
-# Thread.current[:default_currency_format] = I18n.t(:'number.currency.format')
@record = Detail.new
- @record.int_field = 1000
- @record.money_field = 12.34
+ @record.int_field = 1002
+ @record.money_field = 12.333
end
it 'should have native values' do
- @record.int_field.should == 1000
- @record.money_field.should == 12.34
+ @record.int_field.should == 1002
+ @record.money_field.should == 12.333
end
- it 'should have placeholder values' do
- @record.expand_placeholders('[int_field]').should == '1000'
- @record.expand_placeholders('[money_field]').should == '$12.34'
+ it 'should have placeholder value' do
+ @record.expand_placeholders('[int_field]').should == '1002'
+ @record.expand_placeholders('[money_field]').should == '12.333'
end
-
+ it 'should expand placeholders in an array' do
+ @record.expand_placeholders(['[int_field]', '[money_field]', 'static']).should == ['1002', '12.333', 'static']
+ end
+
+ it 'should expand placeholders in a hash' do
+ @record.expand_placeholders( :key1 => '[int_field]',
+ :key2 => '[money_field]',
+ :key3 => 'static'
+ ).should ==
+ { :key1 => '1002',
+ :key2 => '12.333',
+ :key3 => 'static' }
+ end
end
describe 'Placeholder substitution' do
@@ -85,8 +93,7 @@ class Detail
it 'should expand with simple fieldname' do
@detail1.expand_placeholders('[int_field]').should == '1001'
@detail1.expand_placeholders("[int_field]\n").should == "1001\n"
- @detail1.expand_placeholders('[int_field]---[int_field]').should == '1001---1001'
- @detail1.expand_placeholders('[int_field]---[money_field]').should == '1001---$12.34'
+ @detail1.expand_placeholders('[int_field]---[int_field]--[money_field]').should == '1001---1001--12.34'
end
it 'should not parse unknown field' do
@@ -117,7 +124,7 @@ class Detail
@detail1.expand_placeholders('[detail.master.string_field] [detail.int_field]').should == 'foo 1001'
end
- it 'should parse with multiple steps' do
+ it 'should parse multiple steps' do
@detail1.expand_placeholders('[master.side.field]').should == '123'
end
end
@@ -133,7 +140,7 @@ class Detail
end
end
- context 'with groups' do
+ context 'with collection' do
it 'should expand' do
@master.expand_placeholders('[details][int_field]\n[/details]').should == '1001\n1002\n'
@master.expand_placeholders('[details]Test:[int_field][/details]').should == 'Test:1001Test:1002'
@@ -151,18 +158,16 @@ class Detail
end
end
+ context 'with custom formatter' do
+ before :each do
+ I18n.locale = :en_master
+ # Thread.current[:default_currency_format] = I18n.t(:'number.currency.format')
+ end
- it 'should expand placeholders in an array' do
- @detail1.expand_placeholders(['[int_field]', '[money_field]', 'static']).should == ['1001', '$12.34', 'static']
+ xit 'should format money' do
+ @record.expand_placeholders('[money_field]').should == '$12.34'
+ end
end
- it 'should expand placeholders in a hash' do
- @detail1.expand_placeholders( :key1 => '[int_field]',
- :key2 => '[money_field]',
- :key3 => 'static'
- ).should ==
- { :key1 => '1001',
- :key2 => '$12.34',
- :key3 => 'static' }
- end
+
end

0 comments on commit 9aa7127

Please sign in to comment.