Module: NForm::Attributes
- Included in:
- Form
- Defined in:
- lib/nform/attributes.rb
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
Instance Method Summary collapse
- #__hash_rep ⇒ Object
- #__undef_attr ⇒ Object
- #attribute(name, coerce: nil, required: false, default: nil) ⇒ Object
- #attribute_set ⇒ Object
- #define_attributes ⇒ Object
- #get_coercion(coerce_option) ⇒ Object
- #hash_representation(option) ⇒ Object
- #undefined_attributes(option) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
70 71 72 |
# File 'lib/nform/attributes.rb', line 70 def self.extended(base) base.include(InstanceMethods) end |
Instance Method Details
#__hash_rep ⇒ Object
33 34 35 |
# File 'lib/nform/attributes.rb', line 33 def __hash_rep @hash_rep ||= :complete end |
#__undef_attr ⇒ Object
29 30 31 |
# File 'lib/nform/attributes.rb', line 29 def __undef_attr @undef_attr ||= :raise end |
#attribute(name, coerce: nil, required: false, default: nil) ⇒ Object
7 8 9 |
# File 'lib/nform/attributes.rb', line 7 def attribute(name,coerce:nil,required:false,default:nil) attribute_set[name.to_sym] = {coerce: coerce, required: required, default: default} end |
#attribute_set ⇒ Object
11 12 13 |
# File 'lib/nform/attributes.rb', line 11 def attribute_set @attribute_set ||= {} end |
#define_attributes ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/nform/attributes.rb', line 37 def define_attributes attribute_set.each do |name,| define_method(name) do instance_variable_get("@#{name}") end # TODO: must use coercion set c = get_coercion([:coerce]) define_method("#{name}=") do |input| @__touched_keys << name instance_variable_set("@#{name}", c.call(input,self)) end end end |
#get_coercion(coerce_option) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/nform/attributes.rb', line 52 def get_coercion(coerce_option) case when coerce_option.nil? proc{|n| n } when coerce_option.is_a?(Symbol) NForm::Coercions.fetch(coerce_option) when coerce_option.respond_to?(:call) coerce_option when coerce_option.is_a?(Enumerable) chain = coerce_option.map{|o| get_coercion(o) } proc do |input,scope| chain.reduce(input){|i,c| c.call(i,scope) } end else raise Error, "Invalid coerce option given" end end |
#hash_representation(option) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/nform/attributes.rb', line 22 def hash_representation(option) unless i|partial complete|.include?(option) raise ArgumentError, "Unknown option `#{option}` for hash representation. Options are :partial or :complete" end @hash_rep = option end |
#undefined_attributes(option) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/nform/attributes.rb', line 15 def undefined_attributes(option) unless i|raise ignore|.include?(option) raise ArgumentError, "Unknown option `#{option}` for undefined attributes. Options are :raise or :ignore" end @undef_attr = option end |