Class: Caracal::Core::Models::ListModel

Inherits:
BaseModel
  • Object
show all
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

Instance Method Summary collapse

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(options={}, &block)
  @list_type  = DEFAULT_LIST_TYPE
  @list_level = DEFAULT_LIST_LEVEL
  
  super options, &block
end

Instance Attribute Details

#list_levelObject (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_typeObject (readonly)

accessors



24
25
26
# File 'lib/caracal/core/models/list_model.rb', line 24

def list_type
  @list_type
end

Instance Method Details

#itemsObject

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_mapObject

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)
  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_itemsObject

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 ===========================

Returns:

  • (Boolean)


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