module Forma::Utils
Public Class Methods
extract_value(val, name)
click to toggle source
# File lib/forma/utils.rb, line 14 def extract_value(val, name) def simple_value(model, name) if model.respond_to?(name); model.send(name) elsif model.respond_to?('[]'); model[name] || model[name.to_sym] end end name.to_s.split('.').each { |n| val = simple_value(val, n) if val } val end
number_format(num, h = {})
click to toggle source
# File lib/forma/utils.rb, line 24 def number_format(num, h = {}) max_digits = h[:max_digits] || 2 max_digits = 0 if max_digits < 0 min_digits = h[:min_digits] || 0 min_digits = max_digits if min_digits > max_digits separator = h[:separator] || '.' delimiter = h[:delimiter] || ',' formatted = number_with_precision(num, precision: max_digits, separator: separator, delimiter: delimiter, strip_insignificant_zeros: true) nums = formatted.split(separator) length_after_separator = (nums[1] || '').length if length_after_separator >= min_digits formatted else "#{nums[0]}#{separator}#{(nums[1] || '').ljust(min_digits, '0')}" end end
singular_name(model)
click to toggle source
# File lib/forma/utils.rb, line 7 def singular_name(model) if model.respond_to?(:model_name); model.model_name.singular_route_key # Mongoid elsif model.class.respond_to?(:table_name); model.class.table_name.singularize # ActiveModel else; '' # Others end end
Public Instance Methods
simple_value(model, name)
click to toggle source
# File lib/forma/utils.rb, line 15 def simple_value(model, name) if model.respond_to?(name); model.send(name) elsif model.respond_to?('[]'); model[name] || model[name.to_sym] end end
Private Instance Methods
extract_value(val, name)
click to toggle source
# File lib/forma/utils.rb, line 14 def extract_value(val, name) def simple_value(model, name) if model.respond_to?(name); model.send(name) elsif model.respond_to?('[]'); model[name] || model[name.to_sym] end end name.to_s.split('.').each { |n| val = simple_value(val, n) if val } val end
number_format(num, h = {})
click to toggle source
# File lib/forma/utils.rb, line 24 def number_format(num, h = {}) max_digits = h[:max_digits] || 2 max_digits = 0 if max_digits < 0 min_digits = h[:min_digits] || 0 min_digits = max_digits if min_digits > max_digits separator = h[:separator] || '.' delimiter = h[:delimiter] || ',' formatted = number_with_precision(num, precision: max_digits, separator: separator, delimiter: delimiter, strip_insignificant_zeros: true) nums = formatted.split(separator) length_after_separator = (nums[1] || '').length if length_after_separator >= min_digits formatted else "#{nums[0]}#{separator}#{(nums[1] || '').ljust(min_digits, '0')}" end end
singular_name(model)
click to toggle source
# File lib/forma/utils.rb, line 7 def singular_name(model) if model.respond_to?(:model_name); model.model_name.singular_route_key # Mongoid elsif model.class.respond_to?(:table_name); model.class.table_name.singularize # ActiveModel else; '' # Others end end