<commit_before>require 'uber/options'

module Representable
  # Created at class compile time. Keeps configuration options for one property.
  class Definition < Hash
    attr_reader :name
    alias_method :getter, :name

    def initialize(sym, options={})
      # deprecations:
      raise "The :from option got replaced by :as in Representable 1.8!" if options[:from]

      super()
      @name     = sym.to_s
      # defaults:
      self[:as]       = options.delete(:as) || @name

      setup!(options)
    end

    # TODO: test merge!.
    # TODO: make clear that this is the only writer method after #initialize.
    def merge!(options)
      setup!(options)
      self
    end

    private :default, :[]=

    def as
      self[:as]
    end

    def options # TODO: remove in 1.9.
      warn "Representable::Definition#option is deprecated, use #[] directly."
      self
    end

    def setter
      :"#{name}="
    end

    def typed?
      self[:class] or self[:extend] or self[:instance]
    end

    def array?
      self[:collection]
    end

    def hash?
      self[:hash]
    end

    def deserialize_class
      self[:class]
    end

    def default_for(value)
      return self[:default] if skipable_nil_value?(value)
      value
    end

    def has_default?
      has_key?(:default)
    end

    def representer_module
      self[:extend]
    end

    def skipable_nil_value?(value)
      value.nil? and not self[:render_nil]
    end

    def create_binding(*args)
      self[:binding].call(self, *args)
    end

    def sync?
      self[:parse_strategy] == :sync
    end

  private
    def setup!(options)
      r = options.delete(:extend) || options.delete(:decorator)
      options[:extend]  = r if r

      # todo: aS:
      for name, value in options
        value = Uber::Options::Value.new(value) if dynamic_options.include?(name)
        self[name] = value
      end

      self[:as] = self[:as].to_s
    end

    def dynamic_options
      [:getter, :setter, :class, :instance, :reader, :writer, :extend]
    end
  end
end<commit_msg>clean up #setup!<commit_after>require 'uber/options'

module Representable
  # Created at class compile time. Keeps configuration options for one property.
  class Definition < Hash
    attr_reader :name
    alias_method :getter, :name

    def initialize(sym, options={})
      # deprecations:
      raise "The :from option got replaced by :as in Representable 1.8!" if options[:from]

      super()
      @name     = sym.to_s

      # defaults:
      self[:as]       = options.delete(:as) || @name

      setup!(options)
    end

    # TODO: test merge!.
    def merge!(options)
      setup!(options)
      self
    end

    private :default, :[]=

    def as
      self[:as]
    end

    def options # TODO: remove in 1.9.
      warn "Representable::Definition#option is deprecated, use #[] directly."
      self
    end

    def setter
      :"#{name}="
    end

    def typed?
      self[:class] or self[:extend] or self[:instance]
    end

    def array?
      self[:collection]
    end

    def hash?
      self[:hash]
    end

    def deserialize_class
      self[:class]
    end

    def default_for(value)
      return self[:default] if skipable_nil_value?(value)
      value
    end

    def has_default?
      has_key?(:default)
    end

    def representer_module
      self[:extend]
    end

    def skipable_nil_value?(value)
      value.nil? and not self[:render_nil]
    end

    def create_binding(*args)
      self[:binding].call(self, *args)
    end

    def sync?
      self[:parse_strategy] == :sync
    end

  private
    def setup!(options)
      handle_extend!(options)

      # todo: aS:
      for name, value in options
        value = Uber::Options::Value.new(value) if dynamic_options.include?(name)
        self[name] = value
      end

      self[:as] = self[:as].to_s
    end

    def dynamic_options
      [:getter, :setter, :class, :instance, :reader, :writer, :extend]
    end

    def handle_extend!(options)
      mod = options.delete(:extend) || options.delete(:decorator) and options[:extend] = mod
    end
  end
end
{'fraction_non_alphanumeric': 0.10735972795725042, 'fraction_numerical': 0.0019431624969638087, 'mean_word_length': 2.3182917002417405, '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': '5978492', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1250, 'n_words': 362}