Created
February 4, 2013 14:16
-
-
Save kirel/4706936 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module AttributeCasts | |
| extend ActiveSupport::Concern | |
| module ClassMethods | |
| # casts method => type | |
| # type needs to correspond to a TypeCast class that responds to call | |
| def casts casts = {} | |
| casts.each do |meth, cast| | |
| caster = const_get "#{cast}_cast".camelize | |
| define_method "#{meth}_with_cast" do | |
| caster.new.call send("#{meth}_without_cast") | |
| end | |
| alias_method_chain "#{meth}", :cast | |
| end | |
| end | |
| end | |
| class BooleanCast | |
| def call val | |
| case val | |
| when nil, false, 0, "0", "", "false" | |
| false | |
| else | |
| true | |
| end | |
| end | |
| end | |
| class ArrayCast | |
| def call val | |
| case val | |
| when Hash | |
| val.values | |
| else | |
| Array.wrap(val) | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or 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
| module AttributeDefaults | |
| extend ActiveSupport::Concern | |
| module ClassMethods | |
| # sets default values for accessors | |
| # defaults are returned whenever an accessor would return nil | |
| # defaults method => default_value | |
| def defaults defaults = {} | |
| defaults.each do |meth, default| | |
| default_proc = default.respond_to?(:to_proc) ? default.to_proc : proc { default } | |
| define_method "#{meth}_default" do | |
| default_proc.call | |
| end | |
| define_method "#{meth}_with_default" do | |
| res = send("#{meth}_without_default") | |
| !res.nil? ? res : send("#{meth}_default") | |
| end | |
| alias_method_chain meth, :default | |
| end | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Api enhancements: