<commit_before># frozen_string_literal: true

class Gtk::FormDSL::ListView < Gtk::TreeView
  def initialize(parent_dslobj, columns, config, object_initializer, &generate)
    raise 'no block given' unless generate
    @parent_dslobj = parent_dslobj
    @columns = columns
    @config = config
    @object_initializer = object_initializer
    @generate = generate
    super()
    store = Gtk::ListStore.new(Object, *([String] * columns.size))

    columns.each_with_index do |(label, _), index|
      col = Gtk::TreeViewColumn.new(label, Gtk::CellRendererText.new, text: index+1)
      #col.resizable = scheme[:resizable]
      append_column(col)
    end

    set_model(store)

    @parent_dslobj[@config].each do |obj|
      append(obj)
    end
  end

  def buttons(container_class)
    container = container_class.new

    # create = Gtk::Button.new(Gtk::Stock::ADD)
    # create.ssc(:clicked) do
    #   Plugin[:gui].dialog('hogefuga の作成', &generate).next do |values|
    #     result = @model_klass.new(values.to_h)
    #     append(result)
    #     rewind
    #   end
    #   true
    # end
    # container.add(create)

    if @generate
      edit = Gtk::Button.new(Gtk::Stock::EDIT)
      edit.ssc(:clicked) do
        _, _, iter = selection.to_enum(:selected_each).first
        target = iter[0]
        proc = @generate
        Plugin[:gui].dialog('hogefuga の編集') do
          set_value target.to_hash
          instance_exec(target, &proc)
        end.next do |values|
          iter[0] = @object_initializer.(values.to_h)
          notice "update object: #{values.to_h.inspect}"
          update(iter)
          rewind
        end.terminate('hoge')
        true
      end
      container.add(edit)
    end

    container
  end

  private def append(obj)
    iter = self.model.append
    iter[0] = obj
    update(iter)
  end

  private def update(iter)
    pp iter[0]
    @columns.each_with_index do |(_, converter), index|
      iter[index + 1] = converter.(iter[0]).to_s
    end
  end

  private def rewind
    @parent_dslobj[@config] = self.model.to_enum(:each).map do |_, _, iter|
      iter[0]
    end
  end
end
<commit_msg>作成できるようにした refs #1380<commit_after># frozen_string_literal: true

class Gtk::FormDSL::ListView < Gtk::TreeView
  def initialize(parent_dslobj, columns, config, object_initializer, &generate)
    raise 'no block given' unless generate
    @parent_dslobj = parent_dslobj
    @columns = columns
    @config = config
    @object_initializer = object_initializer
    @generate = generate
    super()
    store = Gtk::ListStore.new(Object, *([String] * columns.size))

    columns.each_with_index do |(label, _), index|
      col = Gtk::TreeViewColumn.new(label, Gtk::CellRendererText.new, text: index+1)
      #col.resizable = scheme[:resizable]
      append_column(col)
    end

    set_model(store)

    @parent_dslobj[@config].each do |obj|
      append(obj)
    end
  end

  def buttons(container_class)
    container = container_class.new

    if @generate
      create = Gtk::Button.new(Gtk::Stock::ADD)
      create.ssc(:clicked) do
        proc = @generate
        Plugin[:gui].dialog('hogefuga の作成') do
          instance_exec(nil, &proc)
        end.next do |values|
          append(@object_initializer.(values.to_h))
          notice "create object: #{values.to_h.inspect}"
          rewind
        end.terminate('hoge')
        true
      end
      container.add(create)
    end

    if @generate
      edit = Gtk::Button.new(Gtk::Stock::EDIT)
      edit.ssc(:clicked) do
        _, _, iter = selection.to_enum(:selected_each).first
        target = iter[0]
        proc = @generate
        Plugin[:gui].dialog('hogefuga の編集') do
          set_value target.to_hash
          instance_exec(target, &proc)
        end.next do |values|
          iter[0] = @object_initializer.(values.to_h)
          notice "update object: #{values.to_h.inspect}"
          update(iter)
          rewind
        end.terminate('hoge')
        true
      end
      container.add(edit)
    end

    container
  end

  private def append(obj)
    iter = self.model.append
    iter[0] = obj
    update(iter)
  end

  private def update(iter)
    pp iter[0]
    @columns.each_with_index do |(_, converter), index|
      iter[index + 1] = converter.(iter[0]).to_s
    end
  end

  private def rewind
    @parent_dslobj[@config] = self.model.to_enum(:each).map do |_, _, iter|
      iter[0]
    end
  end
end

{'fraction_non_alphanumeric': 0.11791280777049921, 'fraction_numerical': 0.00451773209848656, 'mean_word_length': 2.6, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3201908', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1457, 'n_words': 302}