Class: Strings::Inflection::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/strings/inflection/term.rb

Direct Known Subclasses

Noun, Verb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Term

Create a new term

Parameters:

  • word (String)

    the word to turn into a term object



21
22
23
24
# File 'lib/strings/inflection/term.rb', line 21

def initialize(word)
  @word = word.dup
  freeze
end

Instance Attribute Details

#wordObject (readonly)

Returns the value of attribute word.



13
14
15
# File 'lib/strings/inflection/term.rb', line 13

def word
  @word
end

Class Method Details

.[](word) ⇒ Object

Create a term



9
10
11
# File 'lib/strings/inflection/term.rb', line 9

def self.[](word)
  self.new(word)
end

Instance Method Details

#find_match(rules) ⇒ nil, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find a matching rule and replace

Returns:

  • (nil, String)

    nil or replaced word



32
33
34
35
36
37
38
# File 'lib/strings/inflection/term.rb', line 32

def find_match(rules)
  regex, replacement = rules.find { |rule| !!(word =~ rule[0]) }

  return if regex.nil?

  word.sub(regex, replacement)
end

#plural?Boolean

Check if noun is in plural form

Examples:

Strings::Inflection::Noun.new("errors").plural?
# => true

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/strings/inflection/term.rb', line 64

def plural?
  return false if word.to_s.empty?

  word.downcase == plural
end

#singular?Boolean

Check if noun is in singular form

Examples:

Strings::Inflection::Noun.new("error").singular?
# => true

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/strings/inflection/term.rb', line 49

def singular?
  return false if word.to_s.empty?

  word.downcase == singular
end

#to_sObject

A string representation of this term



73
74
75
# File 'lib/strings/inflection/term.rb', line 73

def to_s
  word.to_s
end