Module: Datagrid::Utils
- Defined in:
- lib/datagrid/utils.rb
Overview
:nodoc:
Constant Summary collapse
- TRUTH =
[true, 1, "1", "true", "yes", "on"]
Class Method Summary collapse
- .add_html_classes(options, *classes) ⇒ Object
- .apply_args(*args, &block) ⇒ Object
- .booleanize(value) ⇒ Object
- .extract_position_from_options(array, options) ⇒ Object
- .format_date_as_timestamp(value) ⇒ Object
- .parse_date(value) ⇒ Object
- .string_like?(value) ⇒ Boolean
- .warn_once(message, delay = 5) ⇒ Object
Class Method Details
.add_html_classes(options, *classes) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/datagrid/utils.rb', line 21 def add_html_classes(, *classes) = .clone [:class] ||= "" if [:class].is_a?(Array) [:class] += classes else # suppose that it is a String [:class] += " " unless [:class].blank? [:class] += classes.join(" ") end end |
.apply_args(*args, &block) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/datagrid/utils.rb', line 49 def apply_args(*args, &block) return block.call(*args) if block.arity < 0 args = args.clone (args.size - block.arity).times do args.pop end block.call(*args) end |
.booleanize(value) ⇒ Object
8 9 10 |
# File 'lib/datagrid/utils.rb', line 8 def booleanize(value) TRUTH.include?(value) end |
.extract_position_from_options(array, options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/datagrid/utils.rb', line 38 def (array, ) position = .extract!(:before, :after) if position[:before] array.index {|c| c.name.to_sym == position[:before].to_sym } elsif position[:after] array.index {|c| c.name.to_sym == position[:after].to_sym } + 1 else -1 end end |
.format_date_as_timestamp(value) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/datagrid/utils.rb', line 76 def (value) if !value value elsif value.is_a?(Array) [value.first.try(:beginning_of_day), value.last.try(:end_of_day)] elsif value.is_a?(Range) (value.first.beginning_of_day..value.last.end_of_day) else value.beginning_of_day..value.end_of_day end end |
.parse_date(value) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/datagrid/utils.rb', line 58 def parse_date(value) return nil if value.blank? return value if value.is_a?(Range) if value.is_a?(String) Array(Datagrid.configuration.date_formats).each do |format| begin return Date.strptime(value, format) rescue ::ArgumentError end end end return Date.parse(value) if value.is_a?(String) return value.to_date if value.respond_to?(:to_date) value rescue ::ArgumentError nil end |
.string_like?(value) ⇒ Boolean
34 35 36 |
# File 'lib/datagrid/utils.rb', line 34 def string_like?(value) value.is_a?(Symbol) || value.is_a?(String) end |
.warn_once(message, delay = 5) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/datagrid/utils.rb', line 12 def warn_once(, delay = 5) @warnings ||= {} = @warnings[] return false if && >= Time.now - delay warn @warnings[] = Time.now true end |