Module: ActiveStripper::Helpers

Defined in:
lib/active_stripper/helpers.rb

Class Method Summary collapse

Class Method Details

.cast_to_float(val) ⇒ Float

Convert value to Float value if format match. nil and empty string returns 0.0 other value are converted with Float(val) command if value is not convertible raise an argument Error Only to use if val is NOT a BigDecimal or should results in one

Parameters:

  • val (Any)

    Value to cast into float

Returns:

  • (Float)

    Casted value



106
107
108
109
# File 'lib/active_stripper/helpers.rb', line 106

def cast_to_float(val)
  return 0.0 if !val || val == ""
  return Float( (val.is_a?(String)) ? val.tr(",", ".") : val )
end

.cast_to_int(val) ⇒ Integer

Convert value to Integer value if format match. nil and empty string returns 0 other value are converted with Integer(val) command if value is not convertible raise an argument Error Only to use if val is NOT a BigDecimal or should results in one

Parameters:

  • val (Any)

    Value to cast into integer

Returns:

  • (Integer)

    Casted value



91
92
93
94
# File 'lib/active_stripper/helpers.rb', line 91

def cast_to_int(val)
  return 0 if !val || val == ""
  return Integer(val)
end

.empty_string_to_nil(val) ⇒ String/Nil

Set value to nil if val is an empty string

Parameters:

  • val (String)

    String to evaluate

Returns:

  • (String/Nil)

    Return @val if not empty otherwise return nil



76
77
78
79
# File 'lib/active_stripper/helpers.rb', line 76

def empty_string_to_nil(val)
  return if !val || val == ""
  return val
end

.end_space_stripper(val) ⇒ String

Remove all trailing spaces at the end of the string

Parameters:

  • val (String)

    String to clean up

Returns:

  • (String)

    Cleaned up string



40
41
42
43
# File 'lib/active_stripper/helpers.rb', line 40

def end_space_stripper(val)
  return if !val
  return val.gsub(/\s+$/, "")
end

.multiple_space_stripper(val) ⇒ String

Replace double spaces with single space

Parameters:

  • val (String)

    String to clean up

Returns:

  • (String)

    Cleaned up string



28
29
30
31
# File 'lib/active_stripper/helpers.rb', line 28

def multiple_space_stripper(val)
  return if !val
  return val.gsub(/\s+/, " ")
end

.start_space_stripper(val) ⇒ String

Remove all trailong spaces at the beginning of the string

Parameters:

  • val (String)

    String to clean up

Returns:

  • (String)

    Cleaned up string



52
53
54
55
# File 'lib/active_stripper/helpers.rb', line 52

def start_space_stripper(val)
  return if !val
  return val.gsub(/^\s+/, "")
end

.to_lower_stripper(val) ⇒ String

Lower case the entire string through String#downcase

Parameters:

  • val (String)

    String to lower

Returns:

  • (String)

    Lowered case string



64
65
66
67
# File 'lib/active_stripper/helpers.rb', line 64

def to_lower_stripper(val)
  return if !val
  return val.downcase
end

.white_space_stripper(val) ⇒ String

Apply String#strip to value

Parameters:

  • val (String)

    String to strip

Returns:

  • (String)

    Stripped value



16
17
18
19
# File 'lib/active_stripper/helpers.rb', line 16

def white_space_stripper(val)
  return if !val
  return val.strip
end