Class: KDoc::CsvDoc

Inherits:
Container show all
Defined in:
lib/k_doc/csv_doc.rb

Overview

CsvDoc is a DSL for modeling CSV data objects

Instance Attribute Summary collapse

Attributes inherited from Container

#context, #opts, #owner

Attributes included from BlockProcessor

#action_block, #block, #block_state, #children, #depend_on_tags, #dependents, #init_block

Attributes included from Datum

#data

Attributes included from Taggable

#tag_options

Instance Method Summary collapse

Methods inherited from Container

#debug, #os, #owned?

Methods included from BlockProcessor

#action, #actioned?, #add_child, #block_execute, #children_evaluated?, #debug_block_processor, #depend_on, #dependencies_met?, #evaluated?, #execute_block, #fire_action, #fire_children, #fire_eval, #fire_init, #import, #import_data, #init, #initialize_block_processor, #initialized?, #new?, #resolve_dependency

Methods included from Datum

#clear_data, #initialize_data, #set_data

Methods included from Guarded

#clear_errors, #error_hash, #error_messages, #errors, #guard, #log_any_messages, #valid?, #warn

Methods included from Taggable

#initialize_tag, #key, #namespace, #project, #tag, #type

Constructor Details

#initialize(key = nil, **opts, &block) ⇒ CsvDoc

Create CSV document

Parameters:

  • name (String|Symbol)

    Name of the document

  • args (0)
    Type of the document, defaults to KDoc

    FakeOpinion.new.default_csv_type if not set

  • default:

    Default value (using named params), as above



15
16
17
18
19
20
21
# File 'lib/k_doc/csv_doc.rb', line 15

def initialize(key = nil, **opts, &block)
  super(**{ key: key }.merge(opts))

  initialize_file

  @block = block if block_given?
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/k_doc/csv_doc.rb', line 8

def file
  @file
end

Instance Method Details

#load(load_action: :once, data_action: :replace) ⇒ Object

Load data from file

Parameters:

  • load_action (Symbol) (defaults to: :once)

    The load_action to take if data has already been loaded

  • load_action (:once) (defaults to: :once)

    :once will load the data from content source on first try only

  • load_action (:reload) (defaults to: :once)

    :reload will reload from content source each time

  • data_action (Symbol) (defaults to: :replace)

    The data_action to take when setting data, defaults to :replace

  • data_action (:replace) (defaults to: :replace)

    :replace will replace the existing data instance with the incoming data value

  • data_action (:append) (defaults to: :replace)

    :append will keep existing data and then new value data over the top



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/k_doc/csv_doc.rb', line 31

def load(load_action: :once, data_action: :replace)
  return if load_action == :once && loaded?

  rows = []

  CSV.foreach(file, headers: true, header_converters: :symbol) do |row|
    rows << row.to_h
  end

  set_data(rows, data_action: data_action)

  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/k_doc/csv_doc.rb', line 45

def loaded?
  @loaded
end