lib/csv_decision/columns.rb in csv_decision-0.1.0 vs lib/csv_decision/columns.rb in csv_decision-0.2.0

- old
+ new

@@ -6,45 +6,59 @@ # See LICENSE and README.md for details. module CSVDecision # Dictionary of all this table's columns - inputs, outputs etc. # @api private class Columns - # TODO: Value object used for any columns with defaults - # Default = Struct.new(:name, :function, :default_if) - - # Dictionary of all data columns. + # Dictionary of all table data columns. # The key of each hash is the header cell's array column index. - # Note that input and output columns can be interspersed and need not have unique names. + # Note that input and output columns may be interspersed, and multiple input columns + # may refer to the same input hash key symbol. + # However, output columns must have unique symbols, which cannot overlap with input + # column symbols. class Dictionary # @return [Hash{Integer=>Entry}] All column names. attr_accessor :columns # @return [Hash{Integer=>Entry}] All input column dictionary entries. attr_accessor :ins + # @return [Hash{Integer=>Entry}] All defaulted input column dictionary + # entries. This is actually just a subset of :ins. + attr_accessor :defaults + # @return [Hash{Integer=>Entry}] All output column dictionary entries. attr_accessor :outs # @return [Hash{Integer=>Entry}] All if: column dictionary entries. + # This is actually just a subset of :outs. attr_accessor :ifs - # TODO: Input hash path - optional (planned feature) - # attr_accessor :path - - # TODO: Input columns with a default value (planned feature) - # attr_accessor :defaults - def initialize @columns = {} + @defaults = {} @ifs = {} @ins = {} @outs = {} - # TODO: @path = {} - # TODO: @defaults = {} end end + # Input columns with defaults specified + def defaults + @dictionary&.defaults + end + + # Set defaults for columns with defaults specified + def defaults=(value) + @dictionary.defaults = value + end + + # @return [Hash{Symbol=>[false, Integer]}] Dictionary of all + # input and output column names. + def dictionary + @dictionary.columns + end + # Input columns hash keyed by column index. # @return [Hash{Index=>Entry}] def ins @dictionary.ins end @@ -59,26 +73,13 @@ # @return [Hash{Index=>Entry}] def ifs @dictionary.ifs end - def dictionary - @dictionary.columns - end - + # @return [Array<Symbol>] All input column symbols. def input_keys @dictionary.columns.select { |_k, v| v == :in }.keys end - - # Input columns with defaults specified (planned feature) - # def defaults - # @dictionary.defaults - # end - - # Input hash path (planned feature) - # def path - # @dictionary.path - # end # @param table [Table] Decision table being constructed. def initialize(table) # If a column does not have a valid header cell, then it's empty of data. # Return the stripped header row, and remove it from the data array. \ No newline at end of file