Skip to content

Commit

Permalink
Change localize method API to allow grouping methods in the same parser
Browse files Browse the repository at this point in the history
This way you call localize with a hash containing the :using key:

    localize :price, :total, :using => :numeric
  • Loading branch information
carlosantoniodasilva committed May 30, 2012
1 parent be111e3 commit f8d88d1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
```

Expand Down
8 changes: 6 additions & 2 deletions lib/i18n_alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions test/models/product.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.