lib/locabulary/items/base.rb in locabulary-0.3.1 vs lib/locabulary/items/base.rb in locabulary-0.5.0

- old
+ new

@@ -50,13 +50,14 @@ # @deprecated attr_reader :affiliation def initialize(attributes = {}) attribute_names.each do |key| - value = attributes.fetch(key) { attributes.fetch(key.to_s, nil) } + value = attributes[key] || attributes[key.to_s] send("#{key}=", value) end + @children = [] end def to_h attribute_names.each_with_object({}) do |key, mem| mem[key.to_s] = send(key) unless send(key).to_s.strip == '' @@ -87,19 +88,58 @@ include Comparable def <=>(other) predicate_name_sort = predicate_name <=> other.predicate_name - return predicate_name_sort unless predicate_name_sort == 0 + return predicate_name_sort unless predicate_name_sort.zero? presentation_sequence_sort = presentation_sequence <=> other.presentation_sequence - return presentation_sequence_sort unless presentation_sequence_sort == 0 + return presentation_sequence_sort unless presentation_sequence_sort.zero? term_label <=> other.term_label end SORT_SEQUENCE_FOR_NIL = 100_000_000 private_constant :SORT_SEQUENCE_FOR_NIL def presentation_sequence default_presentation_sequence || SORT_SEQUENCE_FOR_NIL end + + def children + @children.sort + end + + def add_child(*input) + @children += input + end + + HIERARCHY_DELIMITER = '::'.freeze + def slugs + term_label.split(HIERARCHY_DELIMITER) + end + + def self.hierarchy_delimiter + HIERARCHY_DELIMITER + end + + def parent_slugs + slugs[0..-2] + end + + def parent_term_label + parent_slugs.join(HIERARCHY_DELIMITER) + end + + def root_slug + slugs[0] + end + + def selectable? + children.count.zero? + end + + def selectable_label + slugs[-1] + end + + alias selectable_id id end end end