summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladimir Kochnev <hashtable@yandex.ru>2015-08-28 19:12:56 +0300
committerVladimir Kochnev <hashtable@yandex.ru>2015-08-31 12:21:36 +0300
commit488c423c8978a2dbaa69d45338e8660e45c5c169 (patch)
tree1a060eb611c89bdcd04f02c06aaaffc839cc30f4
parentedeef5633c9b5efb288bc5ad4530b286470a9196 (diff)
downloadhashie-488c423c8978a2dbaa69d45338e8660e45c5c169.tar.gz
Introduce Hashie::Extensions::Dash::Coercion.
-rw-r--r--CHANGELOG.md1
-rw-r--r--README.md30
-rw-r--r--lib/hashie.rb1
-rw-r--r--lib/hashie/extensions/dash/coercion.rb25
-rw-r--r--spec/hashie/extensions/dash/coercion_spec.rb13
5 files changed, 70 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 81d2289..6e5b7bb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
* Your contribution here.
* [#304](https://github.com/intridea/hashie/pull/304): Ensured compatibility of `Hash` extensions with singleton objects - [@regexident](https://github.com/regexident).
+* [#306](https://github.com/intridea/hashie/pull/306): Added Hashie::Extensions::Dash::Coercion - [@marshall-lee](https://github.com/marshall-lee).
## 3.4.2 (6/2/2015)
diff --git a/README.md b/README.md
index 940d510..a2f381c 100644
--- a/README.md
+++ b/README.md
@@ -607,6 +607,36 @@ model.created_at.class #=> Time
To enable compatibility with Rails 4 use the [hashie-forbidden_attributes](https://github.com/Maxim-Filimonov/hashie-forbidden_attributes) gem.
+### Dash Extension: Coercion.
+
+If you want to use `Hashie::Extensions::Coercion` together with `Dash` then
+you may probably want to use `Hashie::Extensions::Dash::Coercion` instead.
+This extension automatically includes `Hashie::Extensions::Coercion`
+and also adds a convenient `:coerce` option to `property` so you can define coercion in one line
+instead of using `property` and `coerce_key` separate:
+
+```ruby
+class UserHash < Hashie::Dash
+ include Hashie::Extensions::Coercion
+
+ property :id
+ property :posts
+
+ coerce_key :posts, Array[PostHash]
+end
+```
+
+This is the same as:
+
+```ruby
+class UserHash < Hashie::Dash
+ include Hashie::Extensions::Dash::Coercion
+
+ property :id
+ property :posts, coerce: Array[PostHash]
+end
+```
+
## Trash
A Trash is a Dash that allows you to translate keys on initialization. It mixes
diff --git a/lib/hashie.rb b/lib/hashie.rb
index 7f0433f..b0f771c 100644
--- a/lib/hashie.rb
+++ b/lib/hashie.rb
@@ -34,6 +34,7 @@ module Hashie
module Dash
autoload :IndifferentAccess, 'hashie/extensions/dash/indifferent_access'
autoload :PropertyTranslation, 'hashie/extensions/dash/property_translation'
+ autoload :Coercion, 'hashie/extensions/dash/coercion'
end
module Mash
diff --git a/lib/hashie/extensions/dash/coercion.rb b/lib/hashie/extensions/dash/coercion.rb
new file mode 100644
index 0000000..316174c
--- /dev/null
+++ b/lib/hashie/extensions/dash/coercion.rb
@@ -0,0 +1,25 @@
+module Hashie
+ module Extensions
+ module Dash
+ module Coercion
+ # Extends a Dash with the ability to define coercion for properties.
+
+ def self.included(base)
+ base.send :include, Hashie::Extensions::Coercion
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ # Defines a property on the Dash. Options are the standard
+ # <tt>Hashie::Dash#property</tt> options plus:
+ #
+ # * <tt>:coerce</tt> - The class into which you want the property coerced.
+ def property(property_name, options = {})
+ super
+ coerce_key property_name, options[:coerce] if options[:coerce]
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/hashie/extensions/dash/coercion_spec.rb b/spec/hashie/extensions/dash/coercion_spec.rb
new file mode 100644
index 0000000..ce1f14a
--- /dev/null
+++ b/spec/hashie/extensions/dash/coercion_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe Hashie::Extensions::Dash::Coercion do
+ class DashWithCoercion < Hashie::Dash
+ include Hashie::Extensions::Dash::Coercion
+
+ property :type, coerce: Symbol
+ end
+
+ it 'does the coercion of properties' do
+ expect(DashWithCoercion.new(type: 'something')).to eq(type: :something)
+ end
+end