Class: Caracal::Core::Models::ListModel
- Defined in:
- lib/caracal/core/models/list_model.rb
Overview
This class encapsulates the logic needed to store and manipulate list data.
Instance Attribute Summary collapse
-
#list_level ⇒ Object
readonly
Returns the value of attribute list_level.
-
#list_type ⇒ Object
readonly
accessors.
Instance Method Summary collapse
-
#initialize(options = {}, &block) ⇒ ListModel
constructor
initialization.
-
#items ⇒ Object
This method returns only those items owned directly by this list.
-
#level_map ⇒ Object
This method returns a hash, where the keys are levels and the values are the list type at that level.
-
#li(*args, &block) ⇒ Object
.li.
-
#recursive_items ⇒ Object
This method returns a flattened array containing every item within this list’s tree.
-
#valid? ⇒ Boolean
VALIDATION ===========================.
Constructor Details
#initialize(options = {}, &block) ⇒ ListModel
initialization
29 30 31 32 33 34 |
# File 'lib/caracal/core/models/list_model.rb', line 29 def initialize(={}, &block) @list_type = DEFAULT_LIST_TYPE @list_level = DEFAULT_LIST_LEVEL super , &block end |
Instance Attribute Details
#list_level ⇒ Object (readonly)
Returns the value of attribute list_level.
25 26 27 |
# File 'lib/caracal/core/models/list_model.rb', line 25 def list_level @list_level end |
#list_type ⇒ Object (readonly)
accessors
24 25 26 |
# File 'lib/caracal/core/models/list_model.rb', line 24 def list_type @list_type end |
Instance Method Details
#items ⇒ Object
This method returns only those items owned directly by this list.
46 47 48 |
# File 'lib/caracal/core/models/list_model.rb', line 46 def items @items ||= [] end |
#level_map ⇒ Object
This method returns a hash, where the keys are levels and the values are the list type at that level.
53 54 55 56 57 58 |
# File 'lib/caracal/core/models/list_model.rb', line 53 def level_map recursive_items.reduce({}) do |hash, item| hash[item.list_item_level] = item.list_item_type hash end end |
#li(*args, &block) ⇒ Object
.li
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/caracal/core/models/list_model.rb', line 94 def li(*args, &block) = Caracal::Utilities.(args) .merge!({ content: args.first }) if args.first .merge!({ type: list_type }) .merge!({ level: list_level }) model = Caracal::Core::Models::ListItemModel.new(, &block) if model.valid? items << model else raise Caracal::Errors::InvalidModelError, 'List item must have at least one run.' end model end |
#recursive_items ⇒ Object
This method returns a flattened array containing every item within this list’s tree.
63 64 65 66 67 68 69 70 71 |
# File 'lib/caracal/core/models/list_model.rb', line 63 def recursive_items items.map do |model| if model.nested_list.nil? model else [model, model.nested_list.recursive_items] end end.flatten end |
#valid? ⇒ Boolean
VALIDATION ===========================
112 113 114 115 116 |
# File 'lib/caracal/core/models/list_model.rb', line 112 def valid? a = [:type, :level] required = a.map { |m| send("list_#{ m }") }.compact.size == a.size required && !items.empty? end |