Class: Csv2hash::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/csv2hash/definition.rb

Constant Summary collapse

MAPPING =
'mapping'.freeze
COLLECTION =
'collection'.freeze
TYPES =
[ MAPPING, COLLECTION ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &blk) ⇒ Definition

Returns a new instance of Definition.



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

def initialize name, &blk
  @name = name
  self.cells = []
  self.header_size = 0
  self.structure_rules = {}
  instance_eval(&blk) if block_given?
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



9
10
11
# File 'lib/csv2hash/definition.rb', line 9

def cells
  @cells
end

#header_sizeObject

Returns the value of attribute header_size.



9
10
11
# File 'lib/csv2hash/definition.rb', line 9

def header_size
  @header_size
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/csv2hash/definition.rb', line 10

def name
  @name
end

#structure_rulesObject

Returns the value of attribute structure_rules.



9
10
11
# File 'lib/csv2hash/definition.rb', line 9

def structure_rules
  @structure_rules
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/csv2hash/definition.rb', line 10

def type
  @type
end

Instance Method Details

#cell(*args) ⇒ Object



24
25
26
# File 'lib/csv2hash/definition.rb', line 24

def cell *args
  self.cells << Cell.new(*args)
end

#default!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/csv2hash/definition.rb', line 49

def default!
  # binding.pry
  cells.each do |cell|
    cell.rules.fetch(:position)

    default_position cell
    unless cell.rules.has_key? :message
      if cell.rules.has_key? :values
        cell.rules.merge! message: ':key not supported, please use one of :values'
      else
        cell.rules.merge! message: 'undefined :key on :position'
      end
    end
    cell.rules.merge! mappable:    true    unless cell.rules.has_key? :mappable
    cell.rules.merge! type:       'string' unless cell.rules.has_key? :type
    cell.rules.merge! values:      nil     unless cell.rules.has_key? :values
    cell.rules.merge! nested:      nil     unless cell.rules.has_key? :nested
    cell.rules.merge! allow_blank: false   unless cell.rules.has_key? :allow_blank
    cell.rules.merge! extra_validator: nil unless cell.rules.has_key? :extra_validator
  end
  # binding.pry
end

#mapping(&blk) ⇒ Object



20
21
22
# File 'lib/csv2hash/definition.rb', line 20

def mapping &blk
  instance_eval(&blk) if block_given?
end

#set_header_size(&blk) ⇒ Object



28
29
30
# File 'lib/csv2hash/definition.rb', line 28

def set_header_size &blk
  self.header_size = yield if block_given?
end

#set_structure_rules(&blk) ⇒ Object



36
37
38
# File 'lib/csv2hash/definition.rb', line 36

def set_structure_rules &blk
  self.structure_rules = yield if block_given?
end

#set_type(&blk) ⇒ Object



32
33
34
# File 'lib/csv2hash/definition.rb', line 32

def set_type &blk
  @type = yield if block_given?
end

#validate!Object



40
41
42
43
44
45
46
47
# File 'lib/csv2hash/definition.rb', line 40

def validate!
  # binding.pry
  unless TYPES.include?(@type)
    raise "not suitable type, please use '#{MAPPING}' or '#{COLLECTION}'"
  end
  raise 'cells must be an Array of cell' unless self.cells.class == Array
  raise 'structure rules must be a Hash of rules' unless self.structure_rules.class == Hash
end