Class: Wedge::Plugins::Form::Atts
Instance Attribute Summary collapse
-
#_accessors ⇒ Object
readonly
Returns the value of attribute _accessors.
-
#_aliases ⇒ Object
readonly
Returns the value of attribute _aliases.
-
#_atts ⇒ Object
Returns the value of attribute _atts.
-
#_form ⇒ Object
Returns the value of attribute _form.
-
#_options ⇒ Object
readonly
Returns the value of attribute _options.
Instance Method Summary collapse
- #can_read?(att) ⇒ Boolean
- #can_write?(att, override = false) ⇒ Boolean
-
#initialize(atts, accessors, aliases, options) ⇒ Atts
constructor
A new instance of Atts.
- #process_value(val, opts) ⇒ Object
- #set_accessors ⇒ Object
- #set_atts ⇒ Object
- #set_defaults(_form = self) ⇒ Object
Constructor Details
#initialize(atts, accessors, aliases, options) ⇒ Atts
Returns a new instance of Atts.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wedge/plugins/form.rb', line 35 def initialize atts, accessors, aliases, @_atts = atts.kind_of?(Hash) ? HashObject.new(atts) : atts @_accessors = accessors @_aliases = aliases @_options = set_atts set_accessors self end |
Instance Attribute Details
#_accessors ⇒ Object (readonly)
Returns the value of attribute _accessors.
33 34 35 |
# File 'lib/wedge/plugins/form.rb', line 33 def _accessors @_accessors end |
#_aliases ⇒ Object (readonly)
Returns the value of attribute _aliases.
33 34 35 |
# File 'lib/wedge/plugins/form.rb', line 33 def _aliases @_aliases end |
#_atts ⇒ Object
Returns the value of attribute _atts.
32 33 34 |
# File 'lib/wedge/plugins/form.rb', line 32 def _atts @_atts end |
#_form ⇒ Object
Returns the value of attribute _form.
32 33 34 |
# File 'lib/wedge/plugins/form.rb', line 32 def _form @_form end |
#_options ⇒ Object (readonly)
Returns the value of attribute _options.
33 34 35 |
# File 'lib/wedge/plugins/form.rb', line 33 def @_options end |
Instance Method Details
#can_read?(att) ⇒ Boolean
79 80 81 82 83 84 85 86 87 |
# File 'lib/wedge/plugins/form.rb', line 79 def can_read? att = [att] return true if ![:if] && ![:unless] return true if [:if] && _form.instance_exec(&[:if]) return true if [:unless] && !_form.instance_exec(&[:unless]) false end |
#can_write?(att, override = false) ⇒ Boolean
89 90 91 92 93 |
# File 'lib/wedge/plugins/form.rb', line 89 def can_write? att, override = false = [att] override || (can_read?(att) && (![:read_only])) end |
#process_value(val, opts) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/wedge/plugins/form.rb', line 121 def process_value val, opts # Make sure the value is the correct type if !val.nil? && type = opts[:type] val = case type when 'Integer' val.to_i when 'Float' val.to_f when 'String' val.to_s # issue: opal: https://github.com/opal/opal/issues/982 # when 'Numeric' # # if we had support for bigdecimal in opal # # num = BigDecimal.new(val.to_s) # # # # if num.frac == 0 # # num.to_i # # else # # num.to_f # # end # if val.to_s == val.to_s.to_i.to_s # val.to_s.to_i # elsif val.to_s == val.to_s.to_f.to_s # val.to_s.to_f # else # val # end when 'Symbol' val.to_sym end end val end |
#set_accessors ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/wedge/plugins/form.rb', line 57 def set_accessors _accessors.each do |att| = [att] alias_att = _aliases[att] define_singleton_method att do _atts.send(att) if can_read?(att) end define_singleton_method "#{att}=" do |val, override = false| if can_write?(att, override) _atts.send("#{att}=", process_value(val, )) end end if alias_att define_singleton_method(alias_att) { send(att) } define_singleton_method("#{alias_att}=") { |val, override = false| send("#{att}=", val, override) } end end end |
#set_atts ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/wedge/plugins/form.rb', line 47 def set_atts atts_hash = {} _accessors.each do |att| atts_hash[att] = _atts.respond_to?(att) ? _atts.send(att) : nil end @_atts = HashObject.new atts_hash end |
#set_defaults(_form = self) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/wedge/plugins/form.rb', line 95 def set_defaults _form = self @_form = _form _accessors.each do |att| = [att].deep_dup default = [:default] default = _form.instance_exec(&default) if default.kind_of? Proc default = _form.send("default_#{att}") if _form.respond_to? "default_#{att}" if form = .delete(:form) send("#{att}=", Wedge[ # name "#{form}_form", # attributes (_atts.respond_to?(att) ? (_atts.send(att) || {}) : {}), # options { _nested: true }.merge() ]) elsif .key?(:default) || _form.respond_to?("default_#{att}") send("#{att}=", default, true) end end self end |