Class: ActsAsTable::Headers::Hash
- Inherits:
-
Hash
- Object
- Hash
- ActsAsTable::Headers::Hash
- Defined in:
- lib/acts_as_table/headers.rb
Overview
ActsAsTable headers hash object.
Instance Attribute Summary collapse
-
#column_models_count ⇒ Integer
readonly
Returns the ActsAsTable column models count for this ActsAsTable headers hash object.
Class Method Summary collapse
-
.for(column_models, header_names_with_padding) ⇒ ActsAsTable::Headers::Hash
Returns a new ActsAsTable headers hash object for the given ActsAsTable column models and header names.
Instance Method Summary collapse
-
#initialize(column_models_count, *args) {|hash, key| ... } ⇒ ActsAsTable::Headers::Hash
constructor
Returns a new ActsAsTable headers hash object.
-
#to_array ⇒ Array<Array<Object>>
Returns this ActsAsTable headers hash object as an array.
Constructor Details
#initialize(column_models_count, *args) {|hash, key| ... } ⇒ ActsAsTable::Headers::Hash
Returns a new ActsAsTable headers hash object.
153 154 155 156 157 |
# File 'lib/acts_as_table/headers.rb', line 153 def initialize(column_models_count, *args, &block) super(*args, &block) @column_models_count = column_models_count end |
Instance Attribute Details
#column_models_count ⇒ Integer (readonly)
Returns the ActsAsTable column models count for this ActsAsTable headers hash object.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/acts_as_table/headers.rb', line 74 class Hash < ::Hash # Returns a new ActsAsTable headers hash object for the given ActsAsTable column models and header names. # # @param [Enumerable<ActsAsTable::ColumnModel>] column_models # @param [Array<Array<String, nil>>] header_names_with_padding # @return [ActsAsTable::Headers::Hash] def self.for(column_models, header_names_with_padding) # @!method _block(orig_hash, counter) # Returns the new ActsAsTable headers hash object with accumulated ActsAsTable column models count. # # @param [ActsAsTable::Headers::Hash] orig_hash # @param [Integer] counter # @return [ActsAsTable::Headers::Hash] _block = ::Proc.new { |orig_hash, counter| # @return [Hash<String, Object>] new_hash = orig_hash.each_pair.inject({}) { |acc, pair| key, value = *pair case value when ::Array counter += value.size # @return [Array<ActsAsTable::ColumnModel>] acc[key] = value when ::Hash # @return [ActsAsTable::Headers::Hash] new_hash_for_value = _block.call(value, 0) counter += new_hash_for_value.column_models_count # @return [ActsAsTable::Headers::Hash] acc[key] = new_hash_for_value end acc } self.new(counter).merge(new_hash).freeze } # @return [Hash<String, Object>] orig_hash = header_names_with_padding.each_with_index.inject({}) { |acc, pair| header_names, index = *pair # @return [Integer] max_header_names_index = header_names.size - 1 # @return [Hash<String, Object>] column_models_for_index = header_names.each_with_index.inject(acc) { |header_acc, pair| header_name, header_name_index, = *pair if header_name_index == max_header_names_index header_acc[header_name] ||= [] header_acc[header_name] << column_models[index] else header_acc[header_name] ||= {} end header_acc[header_name] } column_models_for_index.freeze acc } _block.call(orig_hash, 0) end attr_reader :column_models_count # Returns a new ActsAsTable headers hash object. # # @param [Integer] column_models_count # @param [Array<Object>] args # @yieldparam [ActsAsTable::Headers::Hash] hash # @yieldparam [String] key # @yieldreturn [ActsAsTable::Headers::Hash, ActsAsTable::ColumnModel] # @return [ActsAsTable::Headers::Hash] def initialize(column_models_count, *args, &block) super(*args, &block) @column_models_count = column_models_count end # Returns this ActsAsTable headers hash object as an array. # # @return [Array<Array<Object>>] def to_array # @!method _block(acc, hash_with_column_models_count, depth) # Returns the new ActsAsTable headers hash object with accumulated ActsAsTable column models count. # # @param [Array<Array<Object>>] acc # @param [ActsAsTable::Headers::Hash] hash_with_column_models_count # @param [Integer] depth # @return [ActsAsTable::Headers::Hash] _block = ::Proc.new { |acc, hash_with_column_models_count, depth| hash_with_column_models_count.each do |key, hash_with_column_models_count_or_column_models| case hash_with_column_models_count_or_column_models when ::Array acc[depth] ||= [] acc[depth] << [key, hash_with_column_models_count_or_column_models.size] hash_with_column_models_count_or_column_models.each do |column_model| acc[depth + 1] ||= [] acc[depth + 1] << [column_model] end when ::Hash acc[depth] ||= [] acc[depth] << [key, hash_with_column_models_count_or_column_models.column_models_count] _block.call(acc, hash_with_column_models_count_or_column_models, depth + 1) end end acc } _block.call([], self, 0) end end |
Class Method Details
.for(column_models, header_names_with_padding) ⇒ ActsAsTable::Headers::Hash
Returns a new ActsAsTable headers hash object for the given ActsAsTable column models and header names.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/acts_as_table/headers.rb', line 80 def self.for(column_models, header_names_with_padding) # @!method _block(orig_hash, counter) # Returns the new ActsAsTable headers hash object with accumulated ActsAsTable column models count. # # @param [ActsAsTable::Headers::Hash] orig_hash # @param [Integer] counter # @return [ActsAsTable::Headers::Hash] _block = ::Proc.new { |orig_hash, counter| # @return [Hash<String, Object>] new_hash = orig_hash.each_pair.inject({}) { |acc, pair| key, value = *pair case value when ::Array counter += value.size # @return [Array<ActsAsTable::ColumnModel>] acc[key] = value when ::Hash # @return [ActsAsTable::Headers::Hash] new_hash_for_value = _block.call(value, 0) counter += new_hash_for_value.column_models_count # @return [ActsAsTable::Headers::Hash] acc[key] = new_hash_for_value end acc } self.new(counter).merge(new_hash).freeze } # @return [Hash<String, Object>] orig_hash = header_names_with_padding.each_with_index.inject({}) { |acc, pair| header_names, index = *pair # @return [Integer] max_header_names_index = header_names.size - 1 # @return [Hash<String, Object>] column_models_for_index = header_names.each_with_index.inject(acc) { |header_acc, pair| header_name, header_name_index, = *pair if header_name_index == max_header_names_index header_acc[header_name] ||= [] header_acc[header_name] << column_models[index] else header_acc[header_name] ||= {} end header_acc[header_name] } column_models_for_index.freeze acc } _block.call(orig_hash, 0) end |
Instance Method Details
#to_array ⇒ Array<Array<Object>>
Returns this ActsAsTable headers hash object as an array.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/acts_as_table/headers.rb', line 162 def to_array # @!method _block(acc, hash_with_column_models_count, depth) # Returns the new ActsAsTable headers hash object with accumulated ActsAsTable column models count. # # @param [Array<Array<Object>>] acc # @param [ActsAsTable::Headers::Hash] hash_with_column_models_count # @param [Integer] depth # @return [ActsAsTable::Headers::Hash] _block = ::Proc.new { |acc, hash_with_column_models_count, depth| hash_with_column_models_count.each do |key, hash_with_column_models_count_or_column_models| case hash_with_column_models_count_or_column_models when ::Array acc[depth] ||= [] acc[depth] << [key, hash_with_column_models_count_or_column_models.size] hash_with_column_models_count_or_column_models.each do |column_model| acc[depth + 1] ||= [] acc[depth + 1] << [column_model] end when ::Hash acc[depth] ||= [] acc[depth] << [key, hash_with_column_models_count_or_column_models.column_models_count] _block.call(acc, hash_with_column_models_count_or_column_models, depth + 1) end end acc } _block.call([], self, 0) end |