class CSL::Locale::Term

Attributes

form_fallbacks[R]
text[RW]

Public Class Methods

specialize(options) click to toggle source
# File lib/csl/locale/term.rb, line 239
def specialize(options)
  specialized = {}

  options.each do |key, value|
    key = key.to_sym

    if !value.nil? && Term::Attributes.keys.include?(key)
      specialized[key] = value
    end
  end

  specialized.delete :'gender-form' unless
    specialized[:'gender-form'].to_s =~ /^masculine|feminine$/

  specialized
end

Public Instance Methods

default_ordinal?() click to toggle source
# File lib/csl/locale/term.rb, line 299
def default_ordinal?
  attributes.name == 'ordinal'
end
gendered?() click to toggle source
# File lib/csl/locale/term.rb, line 310
def gendered?
  !attributes.gender.blank?
end
long?() click to toggle source
# File lib/csl/locale/term.rb, line 334
def long?
  return true unless attribute?(:form)
  attributes.form.to_s =~ /^long$/i
end
long_ordinal?() click to toggle source

@return [Boolean] whether or not this term is a long-ordinal term

# File lib/csl/locale/term.rb, line 290
def long_ordinal?
  /^long-ordinal(-\d\d+)?$/ === attributes.name
end
match_modulo?(number) click to toggle source

This method returns whether or not the ordinal term matchs the passed-in modulus. This is determined by the ordinal term’s match attribute: a value of ‘last-two-digits’ matches a modulus of 100, ‘last-digit’ matches a modulus of 10 and ‘whole-number’ matches only if the number is identical to the ordinal value.

If the term is no ordinal term, this methods always returns false.

@return [Boolean] whether or not the ordinal term matches the

passed-in number.
# File lib/csl/locale/term.rb, line 268
def match_modulo?(number)
  return false unless ordinal?

  case attributes.match
  when 'last-two-digits'
    ordinal == number % 100
  when 'last-digit'
    ordinal == number % 10
  when 'whole-number'
    ordinal == number
  else
    true
  end
end
Also aliased as: matches_modulo?
matches_modulo?(number)
Alias for: match_modulo?
neutral?() click to toggle source
# File lib/csl/locale/term.rb, line 314
def neutral?
  !gendered?
end
ordinal() click to toggle source

@return [Fixnum, :default, nil]

# File lib/csl/locale/term.rb, line 304
def ordinal
  return unless ordinal?
  return :default if attributes.name == 'ordinal'
  attributes.name[/\d+/].to_i
end
ordinal?() click to toggle source

@return [Boolean] whether or not this term is an ordinal term

# File lib/csl/locale/term.rb, line 285
def ordinal?
  /^(long-)?ordinal(-\d\d+)?$/ === attributes.name
end
plural()
Alias for: pluralize
pluralize() click to toggle source
# File lib/csl/locale/term.rb, line 350
def pluralize
  return text if textnode?
  children.multiple.to_s
end
Also aliased as: plural
set(singular, plural = nil) click to toggle source
# File lib/csl/locale/term.rb, line 360
def set(singular, plural = nil)
  if plural.nil?
    self.text = singular
  else
    self.single = singular
    self.multiple = plural
  end

  self
end
short?() click to toggle source
# File lib/csl/locale/term.rb, line 318
def short?
  attribute?(:form) && attributes.form.to_s =~ /^short$/i
end
short_ordinal?() click to toggle source

@return [Boolean] whether or not this term is a regular ordinal term

# File lib/csl/locale/term.rb, line 295
def short_ordinal?
  /^ordinal(-\d\d+)?$/ === attributes.name
end
singular()
Alias for: singularize
singularize() click to toggle source
# File lib/csl/locale/term.rb, line 343
def singularize
  return text if textnode?
  children.single.to_s
end
Also aliased as: singular
symbol?() click to toggle source
# File lib/csl/locale/term.rb, line 330
def symbol?
  attribute?(:form) && attributes.form.to_s =~ /^symbol$/i
end
tags() click to toggle source
Calls superclass method
# File lib/csl/locale/term.rb, line 397
def tags
  if textnode?
    [
      "<#{[nodename, *attribute_assignments].join(' ')}>",
      CSL.encode_xml_text(text),
      "</#{nodename}>"
    ]
  else
    super
  end
end
textnode?() click to toggle source
# File lib/csl/locale/term.rb, line 339
def textnode?
  !text.blank?
end
to_s(options = nil) click to toggle source

@param options [Hash,nil] an optional configuration hash

@option options [:singular,:plural] :number (:singular) whether to

return the term's singular or plural variant.

@option options [Boolean] :plural (false) whether or not to return

the term's plural variant (this option, if set, takes precedence
over :number).

@return [String] the term as a string

# File lib/csl/locale/term.rb, line 418
def to_s(options = nil)
  if textnode?
    text
  else
    if pluralize?(options)
      pluralize
    else
      singularize
    end
  end
end
verb?() click to toggle source
# File lib/csl/locale/term.rb, line 322
def verb?
  attribute?(:form) && attributes.form.to_s =~ /^verb$/i
end
verb_short?() click to toggle source
# File lib/csl/locale/term.rb, line 326
def verb_short?
  attribute?(:form) && attributes.form.to_s =~ /^verb-short$/i
end

Private Instance Methods

pluralize?(options) click to toggle source
# File lib/csl/locale/term.rb, line 435
def pluralize?(options)
  return false if options.nil?

  case
  when options.key?(:plural) || options.key?('plural')
    options[:plural] || options['plural']
  when options.key?(:number) || options.key?('number')
    key = options[:number] || options['number']

    if key.is_a?(Integer) || key.to_s =~ /^[+-]?\d+$/
      key.to_i > 1
    else
      !key.blank? && key.to_s =~ /^plural/i
    end
  else
    false
  end
end