Class: Attributor::CSV

Inherits:
Collection show all
Defined in:
lib/attributor/types/csv.rb

Direct Known Subclasses

Ids

Class Method Summary collapse

Methods inherited from Collection

check_option!, construct, constructable?, #dump, inherited, load, member_attribute, member_type, native_type, of, valid_type?, validate, #validate, validate_options

Methods included from Dumpable

#dump

Methods included from Container

included

Class Method Details

.decode_string(value, _context) ⇒ Object



3
4
5
# File 'lib/attributor/types/csv.rb', line 3

def self.decode_string(value, _context)
  value.split(',')
end

.describe(shallow = false, example: nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/attributor/types/csv.rb', line 30

def self.describe(shallow = false, example: nil)
  hash = super(shallow)
  hash.delete(:member_attribute)
  hash[:example] = example if example
  hash
end

.dump(values, **opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/attributor/types/csv.rb', line 7

def self.dump(values, **opts)
  case values
  when ::String
    values
  when ::Array
    values.collect { |value| member_attribute.dump(value, opts).to_s }.join(',')
  when nil
    nil
  else
    context = opts[:context] || DEFAULT_ROOT_CONTEXT
    name =  context.last.to_s
    type = values.class.name
    reason = 'Attributor::CSV only supports dumping values of type ' \
             "Array or String, not #{values.class.name}."
    raise DumpError, context: context, name: name, type: type, original_exception: reason
  end
end

.example(context = nil, options: {}) ⇒ Object



25
26
27
28
# File 'lib/attributor/types/csv.rb', line 25

def self.example(context = nil, options: {})
  collection = super(context, options: options.merge(size: (2..4)))
  collection.join(',')
end

.familyObject



37
38
39
# File 'lib/attributor/types/csv.rb', line 37

def self.family
  Collection.family
end