Class: Hashematics::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/hashematics/category.rb

Overview

A Category is an index of objects. It holds two things:

  1. list of top-level objects

  2. list of top-level objects cross-referenced by a parent.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_key:, include_blank: false, parent_key: nil) ⇒ Category

Returns a new instance of Category.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hashematics/category.rb', line 17

def initialize(id_key:, include_blank: false, parent_key: nil)
  raise ArgumentError, 'id_key is required' unless id_key

  @default_parent_id  = Id.default
  @id_key             = Key.get(id_key)
  @include_blank      = include_blank || false
  @lookup             = {}
  @parent_key         = Key.get(parent_key)

  freeze
end

Instance Attribute Details

#id_keyObject (readonly)

Returns the value of attribute id_key.



15
16
17
# File 'lib/hashematics/category.rb', line 15

def id_key
  @id_key
end

#include_blankObject (readonly)

Returns the value of attribute include_blank.



15
16
17
# File 'lib/hashematics/category.rb', line 15

def include_blank
  @include_blank
end

#parent_keyObject (readonly)

Returns the value of attribute parent_key.



15
16
17
# File 'lib/hashematics/category.rb', line 15

def parent_key
  @parent_key
end

Instance Method Details

#add(record) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/hashematics/category.rb', line 35

def add(record)
  return self if skip_record?(record)

  set(
    record.id(parent_key),
    record.id(id_key),
    record
  )
end

#records(parent_record = nil) ⇒ Object



29
30
31
32
33
# File 'lib/hashematics/category.rb', line 29

def records(parent_record = nil)
  parent_id = parent_record&.id(parent_key) || default_parent_id

  get(parent_id).values
end