entry.rb 827 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 name=(value)
  20. @name = value.try :strip
  21. end
  22. def type=(value)
  23. @type = value.try :strip
  24. end
  25. def root?
  26. path == 'index'
  27. end
  28. def as_json
  29. { name: name, path: path, type: type }
  30. end
  31. end
  32. end