class Wedge::Plugins::Form::Atts

Attributes

_accessors[R]
_aliases[R]
_atts[RW]
_form[RW]
_options[R]

Public Class Methods

new(atts, accessors, aliases, options) click to toggle source
# 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

Public Instance Methods

can_read?(att) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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_atts() click to toggle source
# 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) click to toggle source
# 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