Rsubhak¶ ↑
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), doing manipulations.
Rsubhak.rsubhak¶ ↑
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), searching for all occurrences of hash key k, and substituting (within their values) all occurrences of pattern p for replacement r.
Parameters¶ ↑
- ha
-
hash or array
- k
-
hash key
- p
-
pattern
- r
-
replacement
The point of inspiration for this gem was to sanitize the params hash, so that no matter how deeply nested a price or quantity might be, one can among other things, remove the currency sign and commas from a form input (i.e. convert ‘$1,000.00’ to ‘1000.00’); however there is a better way of handling this (see below).
You may still use this gem to do any regex substitution to any hash key’s value. As the method is recursive, it can handle deeply nested hashes and arrays, starting from either (see ‘test/`).
Pronunciation¶ ↑
r sub hak
Examples¶ ↑
irb> require 'rsubhak' => true irb> params={"price"=>"$1,000.00", "nested"=>[{"id"=>"1", "price"=>"$150.00"}], "commit"=>"Save"} => {"price"=>"$1,000.00", "nested"=>[{"id"=>"1", "price"=>"$150.00"}], "commit"=>"Save"} irb> Rsubhak.rsubhak(params, 'price', /[\$,]/, '') => {"price"=>"1000.00", "nested"=>[{"id"=>"1", "price"=>"150.00"}], "commit"=>"Save"}
To remove or strip any dollar sign or commas:
Rsubhak.rsubhak(params, 'price', /[\$,]/, '')
To remove the Euro sign and periods, and then convert commas to periods, thereby going from ‘€1.000,00’ to ‘1000.00’:
Rsubhak.rsubhak(params, 'price', /[€\.]/, '') Rsubhak.rsubhak(params, 'price', /,/, '.')
To use it in your Rails project, first add this to your Gemfile:
gem 'rsubhak', :git => 'git://github.com/abatko/rsubhak.git'
And then in your code, do something like the following. Note that in the Rails app context the hash key may be a symbol.
class YourModel < ActiveRecord::Base validates :price, numericality: {greater_than_or_equal_to: 0.01} end class YourController < ApplicationController before_filter :sanitize_params # ... protected def sanitize_params Rsubhak.rsubhak(params, :price, /[\$,]/, '') # remove $ and , (commas) from any :price in params end end
Note on handling price, quantity, etc.¶ ↑
Though modifying price and quantity was the initial motivation for this gem, a better way to handle user inputs such as these is through virtual attributes.
Price may be a decimal attribute (with :precision => 8, :scale => 2) and it must also pass validation:
validates :price, numericality: {greater_than_or_equal_to: 0.01}
Yet user inputs such as $1,000.00 won’t pass validation (because of the currency sign), and inputs such as 1,000 will end up as 1 in the database.
One way to handle user input is through a virtual attribute:
def price_with_currency_and_commas=(new_price) self.price = new_price.gsub(/[\$,]/,'') # note: this will bite you with non-US and non-CA users! end def price_with_currency_and_commas price # handle format with number_to_currency in view end <%= f.text_field :price_with_currency_and_commas, value: number_to_currency(@product.price) %>
Rsubhak.rstrip¶ ↑
Recursively traverse a hash or array ha (potentially of other nested hashes and/or arrays), searching for all values that respond to strip, and calling strip! on them to remove leading and trailing whitespace.
Parameters¶ ↑
- ha
-
hash or array
Pronunciation¶ ↑
r strip
Examples¶ ↑
rstrip(params)
Contributing¶ ↑
-
Fork it
-
Create your feature branch (‘git checkout -b my_new_feature`)
-
Commit your changes (‘git commit -am ’Add some feature’‘)
-
Push to the branch (‘git push origin my_new_feature`)
-
Create new Pull Request