Class: Wedge::Plugins::Form::Atts

Inherits:
Object
  • Object
show all
Defined in:
lib/wedge/plugins/form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options
  @_atts      = atts.kind_of?(Hash) ? HashObject.new(atts) : atts
  @_accessors = accessors
  @_aliases   = aliases
  @_options   = options

  set_atts
  set_accessors

  self
end

Instance Attribute Details

#_accessorsObject (readonly)

Returns the value of attribute _accessors.



33
34
35
# File 'lib/wedge/plugins/form.rb', line 33

def _accessors
  @_accessors
end

#_aliasesObject (readonly)

Returns the value of attribute _aliases.



33
34
35
# File 'lib/wedge/plugins/form.rb', line 33

def _aliases
  @_aliases
end

#_attsObject

Returns the value of attribute _atts.



32
33
34
# File 'lib/wedge/plugins/form.rb', line 32

def _atts
  @_atts
end

#_formObject

Returns the value of attribute _form.



32
33
34
# File 'lib/wedge/plugins/form.rb', line 32

def _form
  @_form
end

#_optionsObject (readonly)

Returns the value of attribute _options.



33
34
35
# File 'lib/wedge/plugins/form.rb', line 33

def _options
  @_options
end

Instance Method Details

#can_read?(att) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
# File 'lib/wedge/plugins/form.rb', line 79

def can_read? att
  att_options = _options[att]

  return true if !att_options[:if] && !att_options[:unless]
  return true if att_options[:if] && _form.instance_exec(&att_options[:if])
  return true if att_options[:unless] && !_form.instance_exec(&att_options[:unless])

  false
end

#can_write?(att, override = false) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/wedge/plugins/form.rb', line 89

def can_write? att, override = false
  att_options = _options[att]

  override || (can_read?(att) && (!att_options[: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_accessorsObject



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_options = _options[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, att_options))
      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_attsObject



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_options = _options[att].deep_dup
    default     = att_options[:default]
    default     = _form.instance_exec(&default) if default.kind_of? Proc
    default     = _form.send("default_#{att}") if _form.respond_to? "default_#{att}"

    if form = att_options.delete(:form)
      send("#{att}=", Wedge[
        # name
        "#{form}_form",
        # attributes
        (_atts.respond_to?(att) ? (_atts.send(att) || {}) : {}),
        # options
        { _nested: true }.merge(att_options)
      ])
    elsif att_options.key?(:default) || _form.respond_to?("default_#{att}")
      send("#{att}=", default, true)
    end
  end

  self
end