class Caracal::Core::Models::ListModel

This class encapsulates the logic needed to store and manipulate list data.

Attributes

list_level[R]
list_type[R]

accessors

Public Class Methods

new(options={}, &block) click to toggle source

initialization

Calls superclass method Caracal::Core::Models::BaseModel::new
# File lib/caracal/core/models/list_model.rb, line 29
def initialize(options={}, &block)
  @list_type  = DEFAULT_LIST_TYPE
  @list_level = DEFAULT_LIST_LEVEL
  
  super options, &block
end

Public Instance Methods

items() click to toggle source

This method returns only those items owned directly by this list.

# File lib/caracal/core/models/list_model.rb, line 46
def items
  @items ||= []
end
level_map() click to toggle source

This method returns a hash, where the keys are levels and the values are the list type at that level.

# 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) click to toggle source

.li

# File lib/caracal/core/models/list_model.rb, line 94
def li(*args, &block)
  options = Caracal::Utilities.extract_options!(args)
  options.merge!({ content: args.first }) if args.first
  options.merge!({ type:    list_type  })
  options.merge!({ level:   list_level })
  
  model = Caracal::Core::Models::ListItemModel.new(options, &block)
  if model.valid?
    items << model
  else
    raise Caracal::Errors::InvalidModelError, 'List item must have at least one run.'
  end
  model
end
recursive_items() click to toggle source

This method returns a flattened array containing every item within this list's tree.

# 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?() click to toggle source
VALIDATION ===========================
# 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

Private Instance Methods

option_keys() click to toggle source
# File lib/caracal/core/models/list_model.rb, line 124
def option_keys
  [:type, :level]
end