Class: Csv2hash::Definition
- Inherits:
-
Object
- Object
- Csv2hash::Definition
- Defined in:
- lib/csv2hash/definition.rb
Constant Summary collapse
- MAPPING =
'mapping'.freeze
- COLLECTION =
'collection'.freeze
- TYPES =
[ MAPPING, COLLECTION ]
Instance Attribute Summary collapse
-
#cells ⇒ Object
Returns the value of attribute cells.
-
#header_size ⇒ Object
Returns the value of attribute header_size.
-
#name ⇒ Object
Returns the value of attribute name.
-
#structure_rules ⇒ Object
Returns the value of attribute structure_rules.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cell(*args) ⇒ Object
- #default! ⇒ Object
-
#initialize(name, &blk) ⇒ Definition
constructor
A new instance of Definition.
- #mapping(&blk) ⇒ Object
- #set_header_size(&blk) ⇒ Object
- #set_structure_rules(&blk) ⇒ Object
- #set_type(&blk) ⇒ Object
- #validate! ⇒ Object
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 self.name = name self.cells = [] self.header_size = 0 self.structure_rules = {} instance_eval(&blk) if block_given? end |
Instance Attribute Details
#cells ⇒ Object
Returns the value of attribute cells.
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def cells @cells end |
#header_size ⇒ Object
Returns the value of attribute header_size.
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def header_size @header_size end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def name @name end |
#structure_rules ⇒ Object
Returns the value of attribute structure_rules.
9 10 11 |
# File 'lib/csv2hash/definition.rb', line 9 def structure_rules @structure_rules end |
#type ⇒ Object (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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/csv2hash/definition.rb', line 48 def default! cells.each do |cell| cell.rules.fetch(:position) default_position cell cell 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! case_sensitive_values: true unless cell.rules.has_key? :case_sensitive_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 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 |
# File 'lib/csv2hash/definition.rb', line 40 def validate! 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 |