Class: Charty::TableAdapters::HashAdapter
- Inherits:
-
BaseAdapter
- Object
- BaseAdapter
- Charty::TableAdapters::HashAdapter
- Defined in:
- lib/charty/table_adapters/hash_adapter.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Attributes inherited from BaseAdapter
Class Method Summary collapse
- .array?(data) ⇒ Boolean
- .daru_vector?(x) ⇒ Boolean
- .narray_vector?(x) ⇒ Boolean
- .nmatrix_vector?(x) ⇒ Boolean
- .numpy_vector?(x) ⇒ Boolean
- .pandas_series?(x) ⇒ Boolean
- .supported?(data) ⇒ Boolean
Instance Method Summary collapse
- #[](row, column) ⇒ Object
- #column_length ⇒ Object
- #compare_data_equality(other) ⇒ Object
- #each ⇒ Object
-
#initialize(data, columns: nil, index: nil) ⇒ HashAdapter
constructor
A new instance of HashAdapter.
- #length ⇒ Object
Methods inherited from BaseAdapter
Constructor Details
#initialize(data, columns: nil, index: nil) ⇒ HashAdapter
Returns a new instance of HashAdapter.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 58 def initialize(data, columns: nil, index: nil) case data when Hash arrays = data.values columns ||= data.keys when Array case data[0] when Numeric, String, Time, Date arrays = [data] when Hash raise NotImplementedError, "an array of records is not supported" when self.class.method(:array?) unsupported_data_format unless data.all?(&self.class.method(:array?)) arrays = data.map(&:to_a).transpose else unsupported_data_format end else unsupported_data_format end unless arrays.empty? arrays, columns, index = check_data(arrays, columns, index) end @data = arrays.map.with_index {|a, i| [columns[i], a] }.to_h self.columns = columns unless columns.nil? self.index = index unless index.nil? end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
142 143 144 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 142 def data @data end |
Class Method Details
.array?(data) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 26 def self.array?(data) case data when Charty::Vector true when Array, method(:daru_vector?), method(:narray_vector?), method(:nmatrix_vector?), method(:numpy_vector?), method(:pandas_series?) true else false end end |
.daru_vector?(x) ⇒ Boolean
38 39 40 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 38 def self.daru_vector?(x) defined?(Daru::Vector) && x.is_a?(Daru::Vector) end |
.narray_vector?(x) ⇒ Boolean
42 43 44 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 42 def self.narray_vector?(x) defined?(Numo::NArray) && x.is_a?(Numo::NArray) && x.ndim == 1 end |
.nmatrix_vector?(x) ⇒ Boolean
46 47 48 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 46 def self.nmatrix_vector?(x) defined?(NMatrix) && x.is_a?(NMatrix) && x.dim == 1 end |
.numpy_vector?(x) ⇒ Boolean
50 51 52 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 50 def self.numpy_vector?(x) defined?(Numpy::NDArray) && x.is_a?(Numpy::NDArray) && x.ndim == 1 end |
.pandas_series?(x) ⇒ Boolean
54 55 56 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 54 def self.pandas_series?(x) defined?(Pandas::Series) && x.is_a?(Pandas::Series) end |
.supported?(data) ⇒ Boolean
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 8 def self.supported?(data) case data when [] true when Array case data[0] when Numeric, String, Time, Date true when Hash data.all? {|el| el.is_a? Hash } when method(:array?) data.all?(&method(:array?)) end when Hash true end end |
Instance Method Details
#[](row, column) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 168 def [](row, column) if row @data[column][row] else case column when Symbol sym_key = column str_key = column.to_s else str_key = String.try_convert(column) sym_key = str_key.to_sym end column_data = if @data.key?(sym_key) @data[sym_key] else @data[str_key] end Vector.new(column_data, index: index, name: column) end end |
#column_length ⇒ Object
155 156 157 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 155 def column_length data.length end |
#compare_data_equality(other) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 159 def compare_data_equality(other) case other when DaruAdapter, PandasDataFrameAdapter other.compare_data_equality(self) else super end end |
#each ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 190 def each i, n = 0, shape[0] while i < n record = @data.map {|k, v| v[i] } yield record i += 1 end end |
#length ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/charty/table_adapters/hash_adapter.rb', line 146 def length case when column_names.empty? 0 else data[column_names[0]].size end end |