Class: CSL::Locale::Term

Inherits:
Node show all
Defined in:
lib/csl/locale/term.rb

Defined Under Namespace

Classes: Multiple, Registry, Single

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Node

#attributes

Attributes included from Treelike

#children, #nodename, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#<=>, #attribute?, #attributes?, #attributes_for, constantize, create, create_attributes, #custom_attributes, #deep_copy, #default_attribute?, #default_attributes, default_attributes, #each, #exact_match?, #format_page_ranges?, #formatting_options, #has_attributes?, #has_default_attributes?, #has_language?, hide_default_attributes!, hide_default_attributes?, #initialize, #initialize_copy, #inspect, #match?, match?, matches?, #merge!, #page_range_format, parse, parse!, #quotes?, #reverse_merge!, #save_to, show_default_attributes!, #strip_periods?, types

Methods included from Extensions::Nesting

#nesting

Methods included from PrettyPrinter

#pretty_print, #to_xml

Methods included from Treelike

#<<, #add_child, #add_children, #ancestors, #closest, #delete_child, #delete_children, #depth, #descendants, #each_ancestor, #each_child, #each_descendant, #each_sibling, #empty?, #find_child, #find_children, #has_children?, #root, #root?, #siblings, #unlink

Constructor Details

This class inherits a constructor from CSL::Node

Class Attribute Details

.form_fallbacksObject (readonly)

Returns the value of attribute form_fallbacks.



237
238
239
# File 'lib/csl/locale/term.rb', line 237

def form_fallbacks
  @form_fallbacks
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



223
224
225
# File 'lib/csl/locale/term.rb', line 223

def text
  @text
end

Class Method Details

.specialize(options) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# 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

Instance Method Details

#default_ordinal?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/csl/locale/term.rb', line 299

def default_ordinal?
  attributes.name == 'ordinal'
end

#feminine!self?

Returns the term with the gender attribute set to ‘feminine’, or nil if the term was already feminine.

Returns:

  • (self, nil)

    the term with the gender attribute set to ‘feminine’, or nil if the term was already feminine



385
386
387
388
389
390
391
392
393
394
395
# File 'lib/csl/locale/term.rb', line 385

%w{ masculine feminine }.each do |name|
  define_method("#{name}?") do
    attributes.gender.to_s == name
  end

  define_method("#{name}!") do
    return nil if attributes.gender.to_s == name
    attributes.gender = name
    self
  end
end

#feminine?Boolean

Returns whether or not the term is feminie.

Returns:

  • (Boolean)

    whether or not the term is feminie



# File 'lib/csl/locale/term.rb', line 379

#gendered?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/csl/locale/term.rb', line 310

def gendered?
  !attributes.gender.blank?
end

#long?Boolean

Returns:

  • (Boolean)


334
335
336
337
# File 'lib/csl/locale/term.rb', line 334

def long?
  return true unless attribute?(:form)
  attributes.form.to_s =~ /^long$/i
end

#long_ordinal?Boolean

Returns whether or not this term is a long-ordinal term.

Returns:

  • (Boolean)

    whether or not this term is a long-ordinal term



290
291
292
# File 'lib/csl/locale/term.rb', line 290

def long_ordinal?
  /^long-ordinal(-\d\d+)?$/ === attributes.name
end

#masculine!self?

Returns the term with the gender attribute set to ‘masculine’, or nil if the term was already masculine.

Returns:

  • (self, nil)

    the term with the gender attribute set to ‘masculine’, or nil if the term was already masculine



# File 'lib/csl/locale/term.rb', line 375

#masculine?Boolean

Returns whether or not the term is masculine.

Returns:

  • (Boolean)

    whether or not the term is masculine



# File 'lib/csl/locale/term.rb', line 372

#match_modulo?(number) ⇒ Boolean Also known as: matches_modulo?

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.

Returns:

  • (Boolean)

    whether or not the ordinal term matches the passed-in number.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# 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

#neutral?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/csl/locale/term.rb', line 314

def neutral?
  !gendered?
end

#ordinalFixnum, ...

Returns:

  • (Fixnum, :default, nil)


304
305
306
307
308
# 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?Boolean

Returns whether or not this term is an ordinal term.

Returns:

  • (Boolean)

    whether or not this term is an ordinal term



285
286
287
# File 'lib/csl/locale/term.rb', line 285

def ordinal?
  /^(long-)?ordinal(-\d\d+)?$/ === attributes.name
end

#pluralizeObject Also known as: plural



350
351
352
353
# File 'lib/csl/locale/term.rb', line 350

def pluralize
  return text if textnode?
  children.multiple.to_s
end

#set(singular, plural = nil) ⇒ Object



360
361
362
363
364
365
366
367
368
369
# 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?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'lib/csl/locale/term.rb', line 318

def short?
  attribute?(:form) && attributes.form.to_s =~ /^short$/i
end

#short_ordinal?Boolean

Returns whether or not this term is a regular ordinal term.

Returns:

  • (Boolean)

    whether or not this term is a regular ordinal term



295
296
297
# File 'lib/csl/locale/term.rb', line 295

def short_ordinal?
  /^ordinal(-\d\d+)?$/ === attributes.name
end

#singularizeObject Also known as: singular



343
344
345
346
# File 'lib/csl/locale/term.rb', line 343

def singularize
  return text if textnode?
  children.single.to_s
end

#symbol?Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/csl/locale/term.rb', line 330

def symbol?
  attribute?(:form) && attributes.form.to_s =~ /^symbol$/i
end

#tagsObject



397
398
399
400
401
402
403
404
405
406
407
# 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?Boolean

Returns:

  • (Boolean)


339
340
341
# File 'lib/csl/locale/term.rb', line 339

def textnode?
  !text.blank?
end

#to_s(options = nil) ⇒ String

Returns the term as a string.

Parameters:

  • options (Hash, nil) (defaults to: nil)

    an optional configuration hash

Options Hash (options):

  • :number (:singular, :plural) — default: :singular

    whether to return the term’s singular or plural variant.

  • :plural (Boolean) — default: false

    whether or not to return the term’s plural variant (this option, if set, takes precedence over :number).

Returns:

  • (String)

    the term as a string



418
419
420
421
422
423
424
425
426
427
428
# 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?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/csl/locale/term.rb', line 322

def verb?
  attribute?(:form) && attributes.form.to_s =~ /^verb$/i
end

#verb_short?Boolean

Returns:

  • (Boolean)


326
327
328
# File 'lib/csl/locale/term.rb', line 326

def verb_short?
  attribute?(:form) && attributes.form.to_s =~ /^verb-short$/i
end