entry.rb 896 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. module Docs
  3. class Entry
  4. class Invalid < StandardError; end
  5. attr_accessor :name, :type, :path
  6. def initialize(name = nil, path = nil, type = nil)
  7. self.name = name
  8. self.path = path
  9. self.type = type
  10. unless root?
  11. raise Invalid, 'missing name' if !name || name.empty?
  12. raise Invalid, 'missing path' if !path || path.empty?
  13. raise Invalid, 'missing type' if !type || type.empty?
  14. end
  15. end
  16. def ==(other)
  17. other.name == name && other.path == path && other.type == type
  18. end
  19. def <=>(other)
  20. name.to_s.casecmp(other.name.to_s)
  21. end
  22. def name=(value)
  23. @name = value.try :strip
  24. end
  25. def type=(value)
  26. @type = value.try :strip
  27. end
  28. def root?
  29. path == 'index'
  30. end
  31. def as_json
  32. { name: name, path: path, type: type }
  33. end
  34. end
  35. end