<commit_before># frozen_string_literal: true

module RgGen
  module Core
    module InputBase
      class InputData
        def initialize(valid_value_lists)
          @valid_value_lists = valid_value_lists
          @values = Hash.new(NilValue)
          @children = []
          define_setter_methods
          block_given? && yield(self)
        end

        def value(value_name, value, position = nil)
          symbolized_name = value_name.to_sym
          return unless valid_value?(symbolized_name)
          @values[symbolized_name] =
            case value
            when InputValue
              value
            else
              InputValue.new(value, position)
            end
        end

        def []=(value_name, position = nil, value)
          value(value_name, value, position)
        end

        def [](value_name)
          @values[value_name]
        end

        def values(value_list = nil, position = nil)
          value_list && Hash(value_list).each { |n, v| value(n, v, position) }
          @values
        end

        attr_reader :children

        def child(value_list = nil, &block)
          create_child_data do |child_data|
            child_data.build_by_block(block)
            child_data.values(value_list)
            @children << child_data
          end
        end

        def load_file(file)
          contents = File.binread(file)
          build_by_block(binding.eval("proc { #{contents} }", file))
        end

        private

        def valid_value?(value_name)
          @valid_value_lists.first.include?(value_name)
        end

        def define_setter_methods<commit_msg>use #instance_eval instead of Binding#eval to fix #26<commit_after># frozen_string_literal: true

module RgGen
  module Core
    module InputBase
      class InputData
        def initialize(valid_value_lists)
          @valid_value_lists = valid_value_lists
          @values = Hash.new(NilValue)
          @children = []
          define_setter_methods
          block_given? && yield(self)
        end

        def value(value_name, value, position = nil)
          symbolized_name = value_name.to_sym
          return unless valid_value?(symbolized_name)
          @values[symbolized_name] =
            case value
            when InputValue
              value
            else
              InputValue.new(value, position)
            end
        end

        def []=(value_name, position = nil, value)
          value(value_name, value, position)
        end

        def [](value_name)
          @values[value_name]
        end

        def values(value_list = nil, position = nil)
          value_list && Hash(value_list).each { |n, v| value(n, v, position) }
          @values
        end

        attr_reader :children

        def child(value_list = nil, &block)
          create_child_data do |child_data|
            child_data.build_by_block(block)
            child_data.values(value_list)
            @children << child_data
          end
        end

        def load_file(file)
          build_by_block(
            instance_eval("-> { #{File.binread(file)} }", file, 1)
          )
        end

        private

        def valid_value?(value_name)
          @valid_value_lists.first.include?(value_name)
        end

        def define_setter_methods
{'fraction_non_alphanumeric': 0.07678355501813784, 'fraction_numerical': 0.0009068923821039903, 'mean_word_length': 1.780672268907563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, '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': '4138521', 'n_tokens_mistral': 936, 'n_tokens_neox': 916, 'n_words': 206}