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?, #formatting_options, #has_attributes?, #has_default_attributes?, #has_language?, hide_default_attributes!, hide_default_attributes?, #initialize, #initialize_copy, #inspect, #match?, match?, matches?, parse, parse!, #save_to, show_default_attributes!, types

Methods included from PrettyPrinter

#pretty_print, #to_xml

Methods included from Treelike

#<<, #add_child, #add_children, #ancestors, #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.



213
214
215
# File 'lib/csl/locale/term.rb', line 213

def form_fallbacks
  @form_fallbacks
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



199
200
201
# File 'lib/csl/locale/term.rb', line 199

def text
  @text
end

Class Method Details

.specialize(options) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/csl/locale/term.rb', line 215

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



275
276
277
# File 'lib/csl/locale/term.rb', line 275

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

#feminine!self?



346
347
348
349
350
351
352
353
354
355
356
# File 'lib/csl/locale/term.rb', line 346

%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



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


#gendered?Boolean



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

def gendered?
  !attributes.gender.blank?
end

#long?Boolean



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

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

#long_ordinal?Boolean



266
267
268
# File 'lib/csl/locale/term.rb', line 266

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

#masculine!self?



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


#masculine?Boolean



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


#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.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/csl/locale/term.rb', line 244

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



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

def neutral?
  !gendered?
end

#ordinalFixnum, ...



280
281
282
283
284
# File 'lib/csl/locale/term.rb', line 280

def ordinal
  return unless ordinal?
  return :default if attributes.name == 'ordinal'
  attributes.name[/\d+/].to_i
end

#ordinal?Boolean



261
262
263
# File 'lib/csl/locale/term.rb', line 261

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

#pluralizeObject Also known as: plural



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

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

#short?Boolean



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

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

#short_ordinal?Boolean



271
272
273
# File 'lib/csl/locale/term.rb', line 271

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

#singularizeObject Also known as: singular



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

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

#symbol?Boolean



306
307
308
# File 'lib/csl/locale/term.rb', line 306

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

#tagsObject



358
359
360
361
362
363
364
365
366
367
368
# File 'lib/csl/locale/term.rb', line 358

def tags
  if textnode?
    [
      "<#{[nodename, *attribute_assignments].join(' ')}>",
      CSL.encode_xml_text(text),
      "</#{nodename}>"
    ]
  else
    super
  end
end

#textnode?Boolean



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

def textnode?
  !text.blank?
end

#to_s(options = nil) ⇒ String

Returns the term as a string.

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).



379
380
381
382
383
384
385
386
387
388
389
# File 'lib/csl/locale/term.rb', line 379

def to_s(options = nil)
  if textnode?
    text
  else
    if pluralize?(options)
      pluralize
    else
      singularize
    end
  end
end

#verb?Boolean



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

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

#verb_short?Boolean



302
303
304
# File 'lib/csl/locale/term.rb', line 302

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