Module: Daru::View::ParamHelpers
- Included in:
- JsHelpers
- Defined in:
- lib/daru/data_tables/generate_js/param_helpers.rb
Instance Method Summary collapse
- #js_parameters(options) ⇒ Object
-
#typecast(value, type = nil) ⇒ Object
If the column type is ‘string’ , the value should be a string.
Instance Method Details
#js_parameters(options) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/daru/data_tables/generate_js/param_helpers.rb', line 6 def js_parameters() return '' if .nil? attributes = .collect { |(key, value)| "#{key}: #{typecast(value)}" } '{' + attributes.join(', ') + '}' end |
#typecast(value, type = nil) ⇒ Object
If the column type is ‘string’ , the value should be a string. If the column type is ‘number’ , the value should be a number. If the column type is ‘boolean’ , the value should be a boolean. If the column type is ‘date’ , the value should be a Date object. If the column type is ‘datetime’ , the value should be a DateTime or Time object. If the column type is ‘timeofday’ , the value should be an array of three or four numbers: [hour, minute, second, optional milliseconds]. Returns an array of strings if given an array Returns ‘null’ when value is nil. Recursive typecasting when value is a hash.
rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/daru/data_tables/generate_js/param_helpers.rb', line 27 def typecast(value, type=nil) case when value.respond_to?(:js_code) && value.js_code? value when value.is_a?(String) value.to_json when value.is_a?(Integer) || value.is_a?(Float) value when value.is_a?(TrueClass) || value.is_a?(FalseClass) value.to_s when value.is_a?(DateTime) || value.is_a?(Time) if type == 'time' "new Date(0, 0, 0, #{value.hour}, #{value.min}, #{value.sec})" else "new Date(#{value.year}, #{value.month-1}, #{value.day}, #{value.hour}, #{value.min}, #{value.sec})" end when value.is_a?(Date) "new Date(#{value.year}, #{value.month-1}, #{value.day})" when value.nil? 'null' when value.is_a?(Array) '[' + value.map { |v| typecast(v) }.join(',') + ']' when value.is_a?(Hash) js_parameters(value) else value end end |