Class: Charty::TableAdapters::BaseAdapter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/charty/table_adapters/base_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



9
10
11
# File 'lib/charty/table_adapters/base_adapter.rb', line 9

def columns
  @columns
end

#indexObject

Returns the value of attribute index.



32
33
34
# File 'lib/charty/table_adapters/base_adapter.rb', line 32

def index
  @index
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/charty/table_adapters/base_adapter.rb', line 38

def ==(other)
  case other
  when BaseAdapter
    return false if columns != other.columns
    return false if index != other.index
    compare_data_equality(other)
  else
    false
  end
end

#column?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/charty/table_adapters/base_adapter.rb', line 19

def column?(name)
  return true if column_names.include?(name)

  case name
  when String
    column_names.include?(name.to_sym)
  when Symbol
    column_names.include?(name.to_s)
  else
    false
  end
end

#column_namesObject



15
16
17
# File 'lib/charty/table_adapters/base_adapter.rb', line 15

def column_names
  columns.to_a
end

#compare_data_equality(other) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/charty/table_adapters/base_adapter.rb', line 53

def compare_data_equality(other)
  columns.each do |name|
    if self[nil, name] != other[nil, name]
      return false
    end
  end
  true
end

#drop_naObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/charty/table_adapters/base_adapter.rb', line 62

def drop_na
  # TODO: Must implement this method in each adapter
  missing_index = index.select do |i|
    column_names.any? do |key|
      Util.missing?(self[i, key])
    end
  end
  if missing_index.empty?
    nil
  else
    select_index = index.to_a - missing_index
    new_data = column_names.map { |key|
      vals = select_index.map {|i| self[i, key] }
      [key, vals]
    }.to_h
    Charty::Table.new(new_data, index: select_index)
  end
end

#group_by(table, grouper, sort, drop_na) ⇒ Object



49
50
51
# File 'lib/charty/table_adapters/base_adapter.rb', line 49

def group_by(table, grouper, sort, drop_na)
  Table::HashGroupBy.new(table, grouper, sort, drop_na)
end

#melt(id_vars: nil, value_vars: nil, var_name: nil, value_name: :value) ⇒ Object



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
# File 'lib/charty/table_adapters/base_adapter.rb', line 142

def melt(id_vars: nil, value_vars: nil, var_name: nil, value_name: :value)
  if column?(value_name)
    raise ArgumentError,
          "The value of `value_name` must not be matched to the existing column names."
  end

  case value_name
  when Symbol
    # do nothing
  else
    value_name = value.to_str.to_sym
  end

  id_vars = check_melt_vars(id_vars, :id_vars)
  value_vars = check_melt_vars(value_vars, :value_vars) { self.column_names }
  value_vars -= id_vars

  case var_name
  when nil
    var_name = self.columns.name
    var_name = :variable if var_name.nil?
  when Symbol
    # do nothing
  else
    var_name = var_name.to_str
  end
  var_name = var_name.to_sym

  n_batch_rows = self.length
  n_target_columns = value_vars.length
  melted_data = id_vars.map { |cn|
    id_values = self[nil, cn].to_a
    [cn.to_sym, id_values * n_target_columns]
  }.to_h

  melted_data[var_name] = value_vars.map { |cn| Array.new(n_batch_rows, cn) }.flatten

  melted_data[value_name] = value_vars.map { |cn| self[nil, cn].to_a }.flatten

  Charty::Table.new(melted_data)
end

#sort_values(by, na_position: :last) ⇒ Object



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
# File 'lib/charty/table_adapters/base_adapter.rb', line 81

def sort_values(by, na_position: :last)
  na_cmp_val = check_na_position(na_position)
  case by
  when String, Symbol
    order = (0 ... length).sort do |i, j|
      a = self[i, by]
      b = self[j, by]
      case
      when Util.missing?(a)  # missing > b
        na_cmp_val
      when Util.missing?(b)  # a < missing
        -na_cmp_val
      else
        cmp = a <=> b
        if cmp == 0
          i <=> j
        else
          cmp
        end
      end
    end
  when Array
    order = (0 ... length).sort do |i, j|
      cmp = 0
      by.each do |key|
        a = self[i, key]
        b = self[j, key]
        case
        when Util.missing?(a)  # missing > b
          cmp = na_cmp_val
          break
        when Util.missing?(b)  # a < missing
          cmp = -na_cmp_val
          break
        else
          cmp = a <=> b
          break if cmp != 0
        end
      end
      if cmp == 0
        i <=> j
      else
        cmp
      end
    end
  else
    raise ArgumentError,
          "%p is invalid value for `by`" % by
  end

  Charty::Table.new(
    column_names.map { |name|
      [
        name,
        self[nil, name].values_at(*order)
      ]
    }.to_h,
    index: index.to_a.values_at(*order)
  )
end