<commit_before># frozen_string_literal: true

class Order
  attr_reader :price, :unit

  def initialize(price, unit)
    @price = price
    @unit = unit
  end
end<commit_msg>:+1: implement Order class<commit_after># frozen_string_literal: true

require 'json'

class Order
  attr_reader :price, :unit

  def initialize(price, unit)
    @price = price
    @unit = unit
  end

  def self.create_from_json(json_string)
    j = JSON.parse(json_string, symbolize_names: true)
    klass = get_buy_or_sell_class_by_name(j[:buy_or_sell])
    klass.new(j[:price], j[:unit])
  end

  def to_h
    {
      price: @price,
      unit: @unit,
      buy_or_sell: buy_or_sell
    }
  end

  def to_json
    to_h.to_json
  end

  private

  def get_buy_or_sell_class_by_name(buy_or_sell)
    case buy_or_sell&.to_sym
    when :buy
      Order::Buy
    when :sell
      Order::Sell
    else
      raise ArgumentError
    end
  end
end
{'fraction_non_alphanumeric': 0.08733624454148471, 'fraction_numerical': 0.001091703056768559, 'mean_word_length': 3.039647577092511, 'pattern_counts': {'":': 0, '<': 3, '<?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': '604705', 'n_tokens_mistral': 356, 'n_tokens_neox': 336, 'n_words': 78}