lib/measured/unit.rb in measured-1.1.0 vs lib/measured/unit.rb in measured-1.2.0
- old
+ new
@@ -8,10 +8,26 @@
@conversion_amount, @conversion_unit = parse_value(value) if value
end
attr_reader :name, :names, :conversion_amount, :conversion_unit
+ def name_eql?(name_to_compare, case_sensitive: false)
+ return false unless name_to_compare.present?
+ name_to_compare = name_to_compare.to_s
+ case_sensitive ? @name.eql?(name_to_compare) : case_insensitive(@name).include?(name_to_compare.downcase)
+ end
+
+ def names_include?(name_to_compare, case_sensitive: false)
+ return false unless name_to_compare.present?
+ name_to_compare = name_to_compare.to_s
+ case_sensitive ? @names.include?(name_to_compare) : case_insensitive(@names).include?(name_to_compare.downcase)
+ end
+
+ def add_alias(aliases)
+ @names = (@names << aliases).flatten.sort unless aliases.nil? || aliases.empty?
+ end
+
def to_s
if conversion_string
"#{ @name } (#{ conversion_string })"
else
@name
@@ -37,9 +53,13 @@
def inverse_conversion_amount
1 / conversion_amount.to_r
end
private
+
+ def case_insensitive(comparison)
+ [comparison].flatten.map(&:downcase)
+ end
def conversion_string
"#{ conversion_amount } #{ conversion_unit }" if @conversion_amount || @conversion_unit
end