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, options, valid_type?, #validate, validate, validate_options

Methods included from Dumpable

#dump

Methods included from Container

included

Class Method Details

.decode_string(value, context) ⇒ Object



5
6
7
# File 'lib/attributor/types/csv.rb', line 5

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

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



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

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



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

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.new(context: context, name: name, type: type, original_exception: reason)
  end
end

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



27
28
29
30
# File 'lib/attributor/types/csv.rb', line 27

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

.familyObject



39
40
41
# File 'lib/attributor/types/csv.rb', line 39

def self.family
  Collection.family
end