Class: SpeakyCsv::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/speaky_csv/base.rb

Overview

An instance of this class is yielded to the block passed to define_csv_fields. Used to configure speaky csv.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



12
13
14
15
16
17
18
# File 'lib/speaky_csv/base.rb', line 12

def initialize
  @export_only_fields = []
  @fields = []
  @has_manys = {}
  @has_ones = {}
  @primary_key = :id
end

Instance Attribute Details

#export_only_fieldsObject (readonly)

Returns the value of attribute export_only_fields.



5
6
7
# File 'lib/speaky_csv/base.rb', line 5

def export_only_fields
  @export_only_fields
end

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/speaky_csv/base.rb', line 5

def fields
  @fields
end

#has_manysObject (readonly)

Returns the value of attribute has_manys.



5
6
7
# File 'lib/speaky_csv/base.rb', line 5

def has_manys
  @has_manys
end

#has_onesObject (readonly)

Returns the value of attribute has_ones.



5
6
7
# File 'lib/speaky_csv/base.rb', line 5

def has_ones
  @has_ones
end

#primary_keyObject

Returns the value of attribute primary_key.



5
6
7
# File 'lib/speaky_csv/base.rb', line 5

def primary_key
  @primary_key
end

Instance Method Details

#dupObject



57
58
59
60
61
62
63
# File 'lib/speaky_csv/base.rb', line 57

def dup
  other = super
  other.instance_variable_set '@has_manys', @has_manys.deep_dup
  other.instance_variable_set '@has_ones', @has_ones.deep_dup

  other
end

#field(*fields, export_only: false) ⇒ Object

Add one or many fields to the csv format.

If options are passed, they apply to all given fields.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/speaky_csv/base.rb', line 23

def field(*fields, export_only: false)
  @fields += fields.map(&:to_sym)
  @fields.uniq!

  if export_only
    @export_only_fields += fields.map(&:to_sym)
    @export_only_fields.uniq!
  end

  nil
end

#has_many(name) {|| ... } ⇒ Object

Yields:

  • ()


50
51
52
53
54
55
# File 'lib/speaky_csv/base.rb', line 50

def has_many(name)
  @has_manys[name.to_sym] ||= self.class.new
  yield @has_manys[name.to_sym]

  nil
end

#has_one(name) {|| ... } ⇒ Object

Yields:

  • ()


43
44
45
46
47
48
# File 'lib/speaky_csv/base.rb', line 43

def has_one(name)
  @has_ones[name.to_sym] ||= self.class.new
  yield @has_ones[name.to_sym]

  nil
end