Class: StrongCSV::Let
- Inherits:
-
Object
- Object
- StrongCSV::Let
- Defined in:
- lib/strong_csv/let.rb
Overview
Let is a class that is used to define types for columns.
Instance Attribute Summary collapse
- #headers ⇒ Boolean readonly
- #pickers ⇒ Hash readonly
- #types ⇒ Hash{Symbol => [Types::Base, Proc]} readonly
Instance Method Summary collapse
- #boolean ⇒ Object
- #boolean? ⇒ Boolean
- #float(**options) ⇒ Object
- #float?(**options) ⇒ Boolean
-
#initialize ⇒ Let
constructor
A new instance of Let.
- #integer(**options) ⇒ Object
- #integer?(**options) ⇒ Boolean
- #let(name, type, *types, error_message: nil, &block) ⇒ Object
- #optional(*args) ⇒ Object
-
#pick(column, as:) {|values| ... } ⇒ Object
#pick is intended for defining a singleton method with ‘:as`.
- #string(**options) ⇒ Object
- #string?(**options) ⇒ Boolean
- #time(**options) ⇒ Object
- #time?(**options) ⇒ Boolean
Constructor Details
#initialize ⇒ Let
Returns a new instance of Let.
15 16 17 18 19 20 |
# File 'lib/strong_csv/let.rb', line 15 def initialize @types = [] @headers = false @pickers = {} @picked = {} end |
Instance Attribute Details
#headers ⇒ Boolean (readonly)
10 11 12 |
# File 'lib/strong_csv/let.rb', line 10 def headers @headers end |
#pickers ⇒ Hash (readonly)
13 14 15 |
# File 'lib/strong_csv/let.rb', line 13 def pickers @pickers end |
#types ⇒ Hash{Symbol => [Types::Base, Proc]} (readonly)
7 8 9 |
# File 'lib/strong_csv/let.rb', line 7 def types @types end |
Instance Method Details
#boolean ⇒ Object
69 70 71 |
# File 'lib/strong_csv/let.rb', line 69 def boolean Types::Boolean.new end |
#boolean? ⇒ Boolean
73 74 75 |
# File 'lib/strong_csv/let.rb', line 73 def boolean? optional(boolean) end |
#float(**options) ⇒ Object
78 79 80 |
# File 'lib/strong_csv/let.rb', line 78 def float(**) Types::Float.new(**) end |
#float?(**options) ⇒ Boolean
83 84 85 |
# File 'lib/strong_csv/let.rb', line 83 def float?(**) optional(float(**)) end |
#integer(**options) ⇒ Object
60 61 62 |
# File 'lib/strong_csv/let.rb', line 60 def integer(**) Types::Integer.new(**) end |
#integer?(**options) ⇒ Boolean
65 66 67 |
# File 'lib/strong_csv/let.rb', line 65 def integer?(**) optional(integer(**)) end |
#let(name, type, *types, error_message: nil, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/strong_csv/let.rb', line 25 def let(name, type, *types, error_message: nil, &block) type = Types::Union.new(type, *types) unless types.empty? case name when ::Integer @types << TypeWrapper.new(name: name, type: type, block: block, error_message: ) when ::String, ::Symbol @types << TypeWrapper.new(name: name.to_sym, type: type, block: block, error_message: ) else raise TypeError, "Invalid type specified for `name`. `name` must be String, Symbol, or Integer: #{name.inspect}" end validate_columns end |
#optional(*args) ⇒ Object
108 109 110 |
# File 'lib/strong_csv/let.rb', line 108 def optional(*args) Types::Optional.new(*args) end |
#pick(column, as:) {|values| ... } ⇒ Object
#pick is intended for defining a singleton method with ‘:as`. This might be useful for the case where you want to receive IDs that are stored in a database, and you want to make sure the IDs actually exist.
50 51 52 53 54 55 56 57 |
# File 'lib/strong_csv/let.rb', line 50 def pick(column, as:, &block) define_singleton_method(as) do @picked[as] end @pickers[as] = lambda do |csv| @picked[as] = block.call(csv.map { |row| row[column] }) end end |
#string(**options) ⇒ Object
88 89 90 |
# File 'lib/strong_csv/let.rb', line 88 def string(**) Types::String.new(**) end |
#string?(**options) ⇒ Boolean
93 94 95 |
# File 'lib/strong_csv/let.rb', line 93 def string?(**) optional(string(**)) end |
#time(**options) ⇒ Object
98 99 100 |
# File 'lib/strong_csv/let.rb', line 98 def time(**) Types::Time.new(**) end |
#time?(**options) ⇒ Boolean
103 104 105 |
# File 'lib/strong_csv/let.rb', line 103 def time?(**) optional(time(**)) end |