-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from marshall-lee/coerce_key_options
Introduce Hashie::Extensions::Dash::Coercion.
- Loading branch information
Showing
5 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |