Module: Mongoid::Extensions::String::Inflections

Included in:
String
Defined in:
lib/mongoid/extensions/string/inflections.rb

Overview

:nodoc:

Constant Summary collapse

CHAR_CONV =

Represents how special characters will get converted when creating a composite key that should be unique and part of a url.

{
  " " => "-",
  "!" => "-excl-",
  "\"" => "-bckslsh-",
  "#" => "-hash-",
  "$" => "-dol-",
  "%" => "-perc-",
  "&" => "-and-",
  "'" => "-quo-",
  "(" => "-oparen-",
  ")" => "-cparen-",
  "*" => "-astx-",
  "+" => "-plus-",
  "," => "-comma-",
  "-" => "-dash-",
  "." => "-period-",
  "/" => "-fwdslsh-",
  ":" => "-colon-",
  ";" => "-semicol-",
  "<" => "-lt-",
  "=" => "-eq-",
  ">" => "-gt-",
  "?" => "-ques-",
  "@" => "-at-",
  "[" => "-obrck-",
  "\\" => "-bckslsh-",
  "]" => "-clbrck-",
  "^" => "-carat-",
  "_" => "-undscr-",
  "`" => "-bcktick-",
  "{" => "-ocurly-",
  "|" => "-pipe-",
  "}" => "-clcurly-",
  "~" => "-tilda-"
}
REVERSALS =
{
  "asc" => "desc",
  "ascending" => "descending",
  "desc" => "asc",
  "descending" => "ascending"
}

Instance Method Summary collapse

Instance Method Details

#collectionizeObject



58
59
60
# File 'lib/mongoid/extensions/string/inflections.rb', line 58

def collectionize
  tableize.gsub("/", "_")
end

#identifyObject



62
63
64
65
66
67
68
69
# File 'lib/mongoid/extensions/string/inflections.rb', line 62

def identify
  if Mongoid.parameterize_keys
    key = ""
    each_char { |c| key += (CHAR_CONV[c] || c.downcase) }; key
  else
    self
  end
end

#invertObject



75
76
77
# File 'lib/mongoid/extensions/string/inflections.rb', line 75

def invert
  REVERSALS[self]
end

#labelizeObject



71
72
73
# File 'lib/mongoid/extensions/string/inflections.rb', line 71

def labelize
  underscore.humanize
end

#plural?Boolean

Returns:



83
84
85
# File 'lib/mongoid/extensions/string/inflections.rb', line 83

def plural?
  pluralize == self
end

#readerObject



87
88
89
# File 'lib/mongoid/extensions/string/inflections.rb', line 87

def reader
  writer? ? gsub("=", "") : self
end

#singular?Boolean

Returns:



79
80
81
# File 'lib/mongoid/extensions/string/inflections.rb', line 79

def singular?
  singularize == self
end

#writer?Boolean

Returns:



91
92
93
# File 'lib/mongoid/extensions/string/inflections.rb', line 91

def writer?
  include?("=")
end