Class: CiteProc::Names
- Includes:
- Enumerable
- Defined in:
- lib/citeproc/names.rb
Overview
Represents a Variable containing an ordered list of Name objects. The names can be formatted using CSL formatting options (see Names.defaults for details).
Class Attribute Summary collapse
-
.defaults ⇒ Hash
readonly
The Names’ default formatting options.
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The current formatting options.
Attributes inherited from Variable
Class Method Summary collapse
-
.parse(names) ⇒ Names?
Parses the passed-in string and returns a Names object.
-
.parse!(names) ⇒ Names
Parses the passed-in string and returns a Names object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum?
Compares two lists of Names.
-
#connector ⇒ String
The connector between the penultimate and the last name.
-
#delimiter ⇒ String
The delimiter between names.
-
#delimiter_always_precedes_last! ⇒ self
(also: #delimiter_precedes_last!)
Set the :‘delimiter-precedes-last’ option to :always.
-
#delimiter_always_precedes_last? ⇒ Boolean
Whether or not the should always be inserted between the penultimate and the last name.
-
#delimiter_contextually_precedes_last! ⇒ self
Set the :‘delimiter-precedes-last’ option to :contextual.
-
#delimiter_contextually_precedes_last? ⇒ Boolean
Whether or not the should be inserted between the penultimate and the last name depending on the number of names.
-
#delimiter_never_precedes_last! ⇒ self
Set the :‘delimiter-precedes-last’ option to :never.
-
#delimiter_never_precedes_last? ⇒ Boolean
Whether or not the should never be inserted between the penultimate and the last name.
-
#delimiter_precedes_last? ⇒ Boolean
Whether or not the delimiter will be inserted between the penultimate and the last name.
-
#each {|name| ... } ⇒ self, Enumerator
Calls a block once for each name.
-
#initialize(*arguments) ⇒ Names
constructor
A new instance of Names.
- #initialize_copy(other) ⇒ Object
-
#inspect ⇒ String
A human-readable representation of the Names object.
-
#last_delimiter ⇒ String
The delimiter between the penultimate and last name.
-
#max_names ⇒ Fixnum
The maximum number of names that should be printed.
-
#numeric? ⇒ false
Names are non-numeric Variables.
-
#plural? ⇒ Boolean
Whether or not the variable holds more than one Name.
-
#push(name) ⇒ self
Appends the given name to the list of names.
- #replace(values) ⇒ Object
-
#to_bibtex ⇒ String
The names in a BibTeX-compatible format.
-
#to_citeproc ⇒ Array<Hash>
The list of names converted to hash objects.
-
#to_s ⇒ String
Converts the list of names into a formatted string depending on the current formatting options.
-
#truncate? ⇒ Boolean
Whether or not the Names should be truncate.
-
#truncate_subsequent? ⇒ Boolean
Whether ot not the Names, if printed on subsequent cites, should be truncated.
-
#truncated_delimiter ⇒ String
The delimiter between the last name printed name and the ‘and others’ term.
-
#unshift(name) ⇒ self
Inserts the given name at the beginning of the list of names.
Methods inherited from Variable
create, create!, #date?, #romanize, #strip_markup, #strip_markup!, #to_f, #to_json, #tokenize, #type
Constructor Details
Class Attribute Details
.defaults ⇒ Hash (readonly)
Returns the Names’ default formatting options.
473 474 475 |
# File 'lib/citeproc/names.rb', line 473 def defaults @defaults end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns the current formatting options.
507 508 509 |
# File 'lib/citeproc/names.rb', line 507 def @options end |
Class Method Details
.parse(names) ⇒ Names?
Parses the passed-in string and returns a Names object. Behaves like parse but returns nil for bad input without raising an error.
482 483 484 485 486 |
# File 'lib/citeproc/names.rb', line 482 def parse(names) parse!(names) rescue ParseError nil end |
.parse!(names) ⇒ Names
Parses the passed-in string and returns a Names object.
494 495 496 497 498 |
# File 'lib/citeproc/names.rb', line 494 def parse!(names) new Namae.parse!(names) rescue raise ParseError, $!. end |
Instance Method Details
#<=>(other) ⇒ Fixnum?
Compares two lists of Names.
705 706 707 708 |
# File 'lib/citeproc/names.rb', line 705 def <=>(other) return nil unless other.respond_to?(:to_a) to_a <=> other.to_a end |
#connector ⇒ String
Returns the connector between the penultimate and the last name.
673 674 675 |
# File 'lib/citeproc/names.rb', line 673 def connector [:and] end |
#delimiter ⇒ String
Returns the delimiter between names.
596 597 598 |
# File 'lib/citeproc/names.rb', line 596 def delimiter [:delimiter] end |
#delimiter_always_precedes_last! ⇒ self Also known as: delimiter_precedes_last!
Set the :‘delimiter-precedes-last’ option to :always
638 639 640 641 |
# File 'lib/citeproc/names.rb', line 638 def delimiter_always_precedes_last! [:'delimiter-precedes-last'] = :always self end |
#delimiter_always_precedes_last? ⇒ Boolean
Returns whether or not the should always be inserted between the penultimate and the last name.
632 633 634 |
# File 'lib/citeproc/names.rb', line 632 def delimiter_always_precedes_last? !!([:'delimiter-precedes-last'].to_s =~ /^always$/i) end |
#delimiter_contextually_precedes_last! ⇒ self
Set the :‘delimiter-precedes-last’ option to :contextual
667 668 669 670 |
# File 'lib/citeproc/names.rb', line 667 def delimiter_contextually_precedes_last! [:'delimiter-precedes-last'] = :contextual self end |
#delimiter_contextually_precedes_last? ⇒ Boolean
Returns whether or not the should be inserted between the penultimate and the last name depending on the number of names.
661 662 663 |
# File 'lib/citeproc/names.rb', line 661 def delimiter_contextually_precedes_last? !!([:'delimiter-precedes-last'].to_s =~ /^contextual/i) end |
#delimiter_never_precedes_last! ⇒ self
Set the :‘delimiter-precedes-last’ option to :never
654 655 656 657 |
# File 'lib/citeproc/names.rb', line 654 def delimiter_never_precedes_last! [:'delimiter-precedes-last'] = :never self end |
#delimiter_never_precedes_last? ⇒ Boolean
Returns whether or not the should never be inserted between the penultimate and the last name.
648 649 650 |
# File 'lib/citeproc/names.rb', line 648 def delimiter_never_precedes_last? !!([:'delimiter-precedes-last'].to_s =~ /^never$/i) end |
#delimiter_precedes_last? ⇒ Boolean
Returns whether or not the delimiter will be inserted between the penultimate and the last name.
619 620 621 622 623 624 625 626 627 628 |
# File 'lib/citeproc/names.rb', line 619 def delimiter_precedes_last? case when delimiter_never_precedes_last? false when delimiter_always_precedes_last? true else length > 2 end end |
#each {|name| ... } ⇒ self, Enumerator
Calls a block once for each name. If no block is given, an enumerator is returned instead.
692 693 694 695 696 697 698 699 |
# File 'lib/citeproc/names.rb', line 692 def each(&block) if block_given? names.each(&block) self else to_enum end end |
#initialize_copy(other) ⇒ Object
552 553 554 |
# File 'lib/citeproc/names.rb', line 552 def initialize_copy(other) @options, @value = other..dup, other.value.map(&:dup) end |
#inspect ⇒ String
Returns a human-readable representation of the Names object.
737 738 739 |
# File 'lib/citeproc/names.rb', line 737 def inspect "#<CiteProc::Names #{to_s.inspect}>" end |
#last_delimiter ⇒ String
Returns the delimiter between the penultimate and last name.
603 604 605 606 607 608 609 |
# File 'lib/citeproc/names.rb', line 603 def last_delimiter if delimiter_precedes_last? [delimiter, connector].compact.join.squeeze(' ') else connector end end |
#max_names ⇒ Fixnum
Returns the maximum number of names that should be printed.
580 581 582 |
# File 'lib/citeproc/names.rb', line 580 def max_names [length, [:'et-al-use-first'].to_i.abs].min end |
#numeric? ⇒ false
Returns Names are non-numeric Variables.
678 679 680 |
# File 'lib/citeproc/names.rb', line 678 def numeric? false end |
#plural? ⇒ Boolean
Returns whether or not the variable holds more than one Name.
683 684 685 |
# File 'lib/citeproc/names.rb', line 683 def plural? length > 1 end |
#push(name) ⇒ self
Appends the given name to the list of names.
540 541 542 543 544 545 |
# File 'lib/citeproc/names.rb', line 540 [:<<, :push, :unshift].each do |m| define_method(m) do |*arguments, &block| names.send(m, *arguments, &block) self end end |
#replace(values) ⇒ Object
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/citeproc/names.rb', line 556 def replace(values) @value = [] [*values].each do |value| case when value.is_a?(Name) @value << value when value.respond_to?(:each_pair), value.respond_to?(:to_hash) @value << Name.new(value) when value.respond_to?(:to_s) begin @value.concat Namae.parse!(value.to_s).map { |n| Name.new n } rescue raise TypeError, $!. end else raise TypeError, "failed to create names from #{value.inspect}" end end self end |
#to_bibtex ⇒ String
Returns the names in a BibTeX-compatible format.
727 728 729 |
# File 'lib/citeproc/names.rb', line 727 def to_bibtex map { |n| n.dup.sort_order!.format }.join(' and ') end |
#to_citeproc ⇒ Array<Hash>
Returns the list of names converted to hash objects.
732 733 734 |
# File 'lib/citeproc/names.rb', line 732 def to_citeproc map(&:to_citeproc) end |
#to_s ⇒ String
Converts the list of names into a formatted string depending on the current formatting options.
715 716 717 718 719 720 721 722 723 724 |
# File 'lib/citeproc/names.rb', line 715 def to_s case when truncate? [names[0...max_names].join(delimiter), [:'et-al']].join(truncated_delimiter) when length < 3 names.join(last_delimiter) else [names[0...-1].join(delimiter), names[-1]].join(last_delimiter) end end |
#truncate? ⇒ Boolean
Returns whether or not the Names should be truncate.
585 586 587 |
# File 'lib/citeproc/names.rb', line 585 def truncate? length >= [:'et-al-min'].to_i.abs end |
#truncate_subsequent? ⇒ Boolean
Returns whether ot not the Names, if printed on subsequent cites, should be truncated.
591 592 593 |
# File 'lib/citeproc/names.rb', line 591 def truncate_subsequent? length >= [:'et-al-subsequent-min'].to_i end |
#truncated_delimiter ⇒ String
Returns the delimiter between the last name printed name and the ‘and others’ term.
613 614 615 |
# File 'lib/citeproc/names.rb', line 613 def truncated_delimiter max_names > 1 ? delimiter : ' ' end |
#unshift(name) ⇒ self
Inserts the given name at the beginning of the list of names.
540 541 542 543 544 545 |
# File 'lib/citeproc/names.rb', line 540 [:<<, :push, :unshift].each do |m| define_method(m) do |*arguments, &block| names.send(m, *arguments, &block) self end end |