lib/csl/locale/term.rb in csl-1.0.2 vs lib/csl/locale/term.rb in csl-1.1.0

- old
+ new

@@ -19,10 +19,32 @@ yield self if block_given? end alias each each_child + # @example + # terms.store(term) + # terms.store('book', ['book', 'books']) + # terms.store('book', 'bk', :form => 'short') + # + # Shorthand method to stores a new term translations. + # + # @param [Term, String] the term; or the the term's name + # @param [String] the term's translation + # @param [Hash] additional term attributes + # + # @return [self] + def store(term, translation = nil, options = nil) + unless term.is_a?(Term) + term = Term.new(:name => term) + term.attributes.merge(options) unless options.nil? + term.set(*translation) + end + + self << term + end + # If a style uses a term in a form that is undefined, there is a # fallback to other forms: "verb-short" first falls back to "verb", # "symbol" first falls back to "short", and "verb" and "short" both # fall back to "long". If no form fallback is available, nil is # returned instead. @@ -149,11 +171,11 @@ tmp = ordinals.values.flatten(1) ordinals.clear delete_children tmp end - private + protected # @!attribute [r] registry # @return [Hash] a private registry to map term names to the respective # term objects for quick term look-up attr_reader :registry @@ -161,10 +183,12 @@ # @!attribute [r] ordinals # @return [Hash] a private registry to map ordinals to the respective # term objects for quick ordinal look-up attr_reader :ordinals + private + def added_child(term) raise ValidationError, "failed to register term #{term.inspect}: name attribute missing" unless term.attribute?(:name) if term.ordinal? @@ -212,11 +236,11 @@ attr_reader :form_fallbacks def specialize(options) specialized = {} - + options.each do |key, value| key = key.to_sym if !value.nil? && Term::Attributes.keys.include?(key) specialized[key] = value @@ -327,9 +351,24 @@ return text if textnode? children.multiple.to_s end alias plural pluralize + + alias singular= single= + alias plural= multiple= + + def set(singular, plural = nil) + if plural.nil? + self.text = singular + else + self.single = singular + self.multiple = plural + end + + self + end + # @!method masculine? # @return [Boolean] whether or not the term is masculine # @!method masculine!