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

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(options, *classes)
  options = options.clone
  options[:class] ||= ""
  if options[:class].is_a?(Array)
    options[:class] += classes
  else
    # suppose that it is a String
    options[:class] += " " unless options[:class].blank?
    options[:class] += classes.join(" ")
  end
  options
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 extract_position_from_options(array, options)
  position = options.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

.string_like?(value) ⇒ Boolean

Returns:

  • (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(message, delay = 5)
  @warnings ||= {}
  timestamp = @warnings[message]
  return false if timestamp && timestamp >= Time.now - delay
  warn message
  @warnings[message] = Time.now
  true
end