Skip to content
Browse files

Change localize method API to allow grouping methods in the same parser

This way you call localize with a hash containing the :using key:

    localize :price, :total, :using => :numeric
  • Loading branch information...
1 parent be111e3 commit f8d88d1c35ee92de3fb197a468b4a13e49f7e470 @carlosantoniodasilva committed May 30, 2012
Showing with 13 additions and 8 deletions.
  1. +3 −3 README.md
  2. +6 −2 lib/i18n_alchemy.rb
  3. +3 −2 test/models/product.rb
  4. +1 −1 test/models/user.rb
View
6 README.md
@@ -94,7 +94,7 @@ Given a product model with a `total` method, that is a simple calculation of `qu
```ruby
class Product < ActiveRecord::Base
include I18n::Alchemy
- localize :total => :number
+ localize :total, :using => :number
def total
quantity * price
@@ -117,7 +117,7 @@ your class have both reader and writer methods available:
```ruby
class Product
include I18n::Alchemy
- localize :released_at => :date
+ localize :released_at, :using => :date
attr_accessor :released_at
end
@@ -160,7 +160,7 @@ If you are using `localize`, you can mix the custom parsers with your existing c
```ruby
class Product < ActiveRecord::Base
include I18n::Alchemy
- localize :total => MyCustomNumberParser
+ localize :total, :using => MyCustomNumberParser
end
```
View
8 lib/i18n_alchemy.rb
@@ -28,8 +28,12 @@ def localized(attributes=nil, *args)
end
module ClassMethods
- def localize(methods_hash)
- self.localized_methods = self.localized_methods.merge(methods_hash)
+ def localize(*methods, options)
+ parser = options[:using]
+ methods = methods.each_with_object(localized_methods) do |method_name, hash|
+ hash[method_name] = parser
+ end
+ self.localized_methods = methods
end
def custom_parsers(parsers_hash)
View
5 test/models/product.rb
@@ -1,7 +1,8 @@
class Product < ActiveRecord::Base
include I18n::Alchemy
- localize :total => :number, :estimated_delivery_at => MyCustomDateParser,
- :estimated_last_comission_payment_at => :timestamp
+ localize :total, :using => :number
+ localize :estimated_delivery_at, :using => MyCustomDateParser
+ localize :estimated_last_comission_payment_at, :using => :timestamp
custom_parsers :released_month => MyCustomDateParser
attr_protected :my_precious
View
2 test/models/user.rb
@@ -2,5 +2,5 @@ class User
include I18n::Alchemy
attr_accessor :created_at
- localize :created_at => I18n::Alchemy::DateParser
+ localize :created_at, :using => :date
end

0 comments on commit f8d88d1

Please sign in to comment.
Something went wrong with that request. Please try again.