lib/csl/locale/term.rb in csl-1.0.0.pre11 vs lib/csl/locale/term.rb in csl-1.0.0.pre12
- old
+ new
@@ -19,43 +19,47 @@
yield self if block_given?
end
alias each each_child
+ # @return [Term, nil] the first term that matches the query
def lookup(name, options = {})
options[:name] = name = name.to_s
- term = registry[name].detect { |t| t.match?(options) }
- return term unless term.nil? && options.delete(:'gender-form')
-
- registry[name].detect { |t| t.match?(options) }
+ # specialize search conditions
+ conditions = options.select { |k,_| Term::Attributes.keys.include?(k.to_sym) }
+
+ term = registry[name].detect { |t| t.match?(conditions) }
+ return term unless term.nil? && conditions.delete(:'gender-form')
+
+ registry[name].detect { |t| t.match?(conditions) }
end
alias [] lookup
-
+
def ordinalize_modulo(number, divisor, options = {})
ordinal = ordinalize(number, options)
return unless ordinal && ordinal.match_modulo?(divisor)
ordinal
end
def ordinalize(number, options = {})
return unless has_ordinals?
-
+
if number == :default
options[:name] = 'ordinal'
else
options[:name] = 'ordinal-%02d' % number
end
-
+
if options[:form].to_s =~ /^long/i
options.delete :form
options[:name][0,0] = 'long-'
end
-
+
ordinal = ordinals[number].detect { |t| t.match?(options) }
return ordinal unless ordinal.nil? && options.delete(:'gender-form')
-
+
ordinals[number].detect { |t| t.match?(options) }
end
# @return [Boolean] whether or not regular terms are registered at this node
def has_terms?
@@ -68,11 +72,11 @@
end
def has_legacy_ordinals?
has_ordinals? && !ordinals.key?(:default)
end
-
+
def drop_ordinals
tmp = ordinals.values.flatten(1)
ordinals.clear
delete_children tmp
end
@@ -111,11 +115,11 @@
ordinals[term.ordinal].delete(term)
else
registry[term[:name]].delete(term)
end
end
-
+
end
class Term < Node
attr_struct :name, :form, :gender, :'gender-form', :match
attr_children :single, :multiple
@@ -160,18 +164,26 @@
return :default if attributes.name == 'ordinal'
attributes.name[/\d+/].to_i
end
def gendered?
- not attributes.gender.blank?
+ !attributes.gender.blank?
end
def neutral?
- not gendered?
+ !gendered?
end
+ def short?
+ attribute?(:form) && attributes.form.to_s =~ /^short$/i
+ end
+
+ def long?
+ !short?
+ end
+
def textnode?
- not text.blank?
+ !text.blank?
end
def singularize
return text if textnode?
children.single.to_s
\ No newline at end of file