Class: Ezframe::IntType

Inherits:
TextType show all
Defined in:
lib/ezframe/column_type.rb

Direct Known Subclasses

ForeignType, IdType

Instance Attribute Summary

Attributes inherited from TypeBase

#attribute, #error, #parent

Instance Method Summary collapse

Methods inherited from TypeBase

#db_value, get_class, #initialize, #key, #label, #make_error_box, #multi_inputs?, #no_edit?, #no_view?, #type, type_name, #use_view_format, #value

Constructor Details

This class inherits a constructor from Ezframe::TypeBase

Instance Method Details

#db_typeObject



228
229
230
# File 'lib/ezframe/column_type.rb', line 228

def db_type
  return "int"
end

#form(opts = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
# File 'lib/ezframe/column_type.rb', line 210

def form(opts = {})
  return nil if no_edit? && !opts[:force]
  key = self.key
  key ="#{key}#{opts[:key_suffix]}" if opts[:key_suffix]
  h = Ht.input(type: "number", name: key, label: @attribute[:label], value: @value || "")
  h[:class] = @attribute[:class] if @attribute[:class]
  h[:after] = make_error_box(key)
  return h
end

#normalize(val) ⇒ Object



180
181
182
183
184
185
# File 'lib/ezframe/column_type.rb', line 180

def normalize(val)
  if val.is_a?(String)
    val = val.tr("0-9", "0-9").strip
  end
  return val
end

#validate(val) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/ezframe/column_type.rb', line 220

def validate(val)
  return nil if !val || val.to_s.strip.empty?
  unless /^\d+$/ =~ val.to_s
    return :invalid_value
  end
  return nil
end

#value=(v) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/ezframe/column_type.rb', line 187

def value=(v)
  if v.nil?
    default = @attribute[:default]
    if default
      @value = default
    else
      @value = nil
    end
    return
  end

  if v.nil?
    @value = nil
    return
  end
  unless v.is_a?(Integer)  || v.is_a?(String)
    EzLog.debug("value must integer or string: key=#{self.key}, #{v}: class=#{v.class}")
    return
  end
  v = normalize(v)
  @value = v.to_i
end

#view(opts = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ezframe/column_type.rb', line 165

def view(opts = {})
  return nil if no_view? && !opts[:force]
  return nil unless @value
  return nil if @attribute[:no_view_if_zero] && @value.to_i == 0
  if @attribute[:view_format]
    return use_view_format(@attribute[:view_format], @value)
  else
    if @attribute[:add_comma]
      return @value.to_i.add_comma
    else
      return @value.to_s
    end
  end
end