Method: Sequel::Model.column_view

Defined in:
lib/sequel/model_patch.rb

.column_view(column_name, type, custom_method_name = nil) ⇒ Object

define a method to get the value of set the column as a certain type Used like this in a Model class.

column_view :'column_name', :date
column_view :'column_name', :time
column_view :'column_name', :time,     :custom_method_name
column_view :'column_name', :currency
column_view :'column_name', :boolean

defines methods for getting new value in the type you requested from the column_name. The type should be :time, :date, :currency, or :boolean

For example: column_name :eta_local, :date, the methods created are:

eta_local_date               =>  returns a Date
eta_local_date_view          =>  returns a Date formatted   y-m-d
eta_local_date_view_mdy      =>  returns a Date formatted   m/d/y
eta_local_date_view_mdy_ipc  =>  returns a Date formatted   m/d/y with first 0 chopped
   ie.  instead of 08/07/2009  .. it will be 8/07/2009

column_name :eta_local, :time, the methods created are:

eta_local_time       =>  returns a Time
eta_local_time_view  =>  returns a Time formatted  H:M

column_name :price, :currency, the method created is:

price_currency       => which returns a currecy formatted value 12.15 ( no dollar sign )

column_name :item_total, :delimited, the method created is:

item_total_delimited => which returns a delimited formatted value 12.15 ( no dollar sign )

column_name :paid, :boolean, the method created is:

paid_boolean         => which returns a string ("False" / "True") for values ( 0 / 1 )

Raises:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/sequel/model_patch.rb', line 235

def column_view(column_name, type, custom_method_name=nil)
  raise(StandardError, "Expect the type to be either :date, :datetime, :time, :currency, :precision or :boolean") unless [:date, :time, :datetime, :currency, :precision, :boolean].include? type
  # will get value from old_column_name
  method_name = custom_method_name || "#{column_name}_#{type.to_s}"

  # the method that returns the column value as a Date or Time value
  inst_def method_name do
    column_value = send(column_name)
    case type
      when :date
        Date.from_fos_days(column_value) if column_value
      when :time
        DateTime.from_fos_time(column_value) if column_value
      when :currency
#            (column_value || 0).to_currency
        number_to_currency((column_value || 0).to_f/100, :unit=>'', :precision=>2)
      when :precision
#            (column_value || 0).to_currency
#            precision wont do delimiter .. so send to currency
        number_to_currency((column_value || 0).to_f/100, :unit=>'', :precision=>2)
      when :boolean
        return "True" if column_value and (column_value == 1 or column_value == true)
        "False"
    end
  end

  method_view_name = method_name.to_s + "_view"
  case type
    when :boolean, :currency, :precision
      inst_def method_view_name do
        value = send(method_name)
        value.to_s if value
      end

    when :time
      inst_def method_view_name do
        value = send(method_name)
        value.strftime('%H:%M') if value
      end

    when :date
      inst_def method_view_name do
        value = send(method_name)
        value.to_s if value
      end
      
      # extra view methods for date type
      method_view_name = method_name.to_s + "_view_mdy"
      # method that returns the column value as a Date in mdy format
      inst_def method_view_name do
        column_value = send(column_name)
        return nil unless column_value
        Date.from_fos_days(column_value).strftime('%m/%d/%Y')
      end
      method_view_name = method_name.to_s + "_view_mdy_ipc"
      # method that returns the column value as a Date in mdy format
      inst_def method_view_name do
        column_value = send(column_name)
        return nil unless column_value
        date_str = Date.from_fos_days(column_value).strftime('%m/%d/%Y')
        date_str.slice!(3) if date_str[3] == 48 # if the fourth char is 0 ( ascii value 48 ) remove it
        date_str.slice!(0) if date_str[0] == 48 # if the first char is 0 ( ascii value 48 ) remove it
        date_str
      end
    end
end