Module: Dima::Utils

Defined in:
lib/dima/utils.rb

Class Method Summary collapse

Class Method Details

.empty?(value) ⇒ Boolean

Check existence.

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/dima/utils.rb', line 33

def Utils.empty?(value)
  return true if value.nil?
  return true if value.is_a?(String) and value.strip == ''
  return true if value.respond_to?(:empty?) and value.empty?
  false
end

.get(model, key) ⇒ Object

Extract value from a model.



11
12
13
14
15
# File 'lib/dima/utils.rb', line 11

def Utils.get(model, key)
  val = model
  key.to_s.split('.').each { |k| val = val.send k }
  val
end

.number_format(number, opts = {}) ⇒ Object

Number format.



25
26
27
28
29
30
# File 'lib/dima/utils.rb', line 25

def Utils.number_format(number, opts = {})
  prec = opts[:precision] || 2
  dlmt = opts[:delimiter] || Dima.config.num.delimiter
  sprt = opts[:separator] || Dima.config.num.separator
  number_with_precision(number, precision: prec, delimiter: dlmt, separator: sprt)
end

.set(model, key, val) ⇒ Object

Setting valut to the model.



18
19
20
21
22
# File 'lib/dima/utils.rb', line 18

def Utils.set(model, key, val)
  keys = key.to_s.split('.')
  model = Dima::Utils.get(model, keys[0..-2].join('.')) if keys.size > 1
  model.send keys[-1] + '=', val
end

.to_sym_hash(hash) ⇒ Object

Hash.



41
42
43
# File 'lib/dima/utils.rb', line 41

def Utils.to_sym_hash(hash)
  hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end