<commit_before>require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'


module Caracal
  module Core
    module Models
      
      # This class handles block options passed to tables via their data 
      # collections.
      #
      class TableCellModel < BaseModel
        
        #-------------------------------------------------------------
        # Configuration
        #-------------------------------------------------------------
        
        # constants
        const_set(:DEFAULT_CELL_BACKGROUND,   'ffffff')
        const_set(:DEFAULT_CELL_MARGINS,      Caracal::Core::Models::MarginModel.new({ top: 100, bottom: 100, left: 100, right: 100 }))
        
        # accessors
        attr_reader :cell_background 
        attr_reader :cell_width
        attr_reader :cell_margins
        
        # initialization
        def initialize(**options, &block)
          @cell_background = DEFAULT_CELL_BACKGROUND
          @cell_margins    = DEFAULT_CELL_MARGINS
          
          if content = options.delete(:content)
            p content, options.dup
          end
          
          super options, &block
        end
        
        
        #-------------------------------------------------------------
        # Public Methods
        #-------------------------------------------------------------
        
        #=============== DATA ACCESSORS =======================
        
        def contents
          @contents ||= []
        end
        
        
        #=============== STYLES ===============================
        
        # This method allows styles to be applied to this cell 
        # from the table level.  It attempts to add the style
        # first to the instance, and then to any sub-models that 
        # respond to the method.
        #
        # In all cases, invalid options will simply be ignored.
        #
        def apply_styles(**options)
          # first, try apply to self
          options.each do |(k,v)|
            send(k, v)  if respond_to?(k)
          end
          
          # prevent top-level attrs from trickling down
          options.delete_if { |(k,v)| option_keys.include?(k) }    
          
          # then, try apply to contents
          contents.each do |model|
            options.each do |k,v|
              model.send(k, v) if model.respond_to?(k)
            end
            
            # finally, apply to runs. options do trickle down
            # because paragraph-level styles don't seem to
            # affect runs within tables. weirdsies.
            if model.respond_to?(:runs)
              runs.each do |run|
                options.each do |k,v|
                  run.send(k, v) if run.respond_to?(k)
                end
              end
            end
          end
        end
        
        def calculate_width(default_width)
          width(default_width) unless cell_width.to_i > 0
          
          container_width = cell_width - cell_margin_left - cell_margin_right
          
          contents.each do |model|
            if model.respond_to?(:calculate_width)
              model.calculate_width(container_width)    # will always be a TableModel
            end
          end
        end
        
        
        #=============== GETTERS ==============================
        
        # margin attrs
        [:top, :bottom, :left, :right].each do |m|<commit_msg>Corrected stupid typo in apply_styles method.<commit_after>require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'


module Caracal
  module Core
    module Models
      
      # This class handles block options passed to tables via their data 
      # collections.
      #
      class TableCellModel < BaseModel
        
        #-------------------------------------------------------------
        # Configuration
        #-------------------------------------------------------------
        
        # constants
        const_set(:DEFAULT_CELL_BACKGROUND,   'ffffff')
        const_set(:DEFAULT_CELL_MARGINS,      Caracal::Core::Models::MarginModel.new({ top: 100, bottom: 100, left: 100, right: 100 }))
        
        # accessors
        attr_reader :cell_background 
        attr_reader :cell_width
        attr_reader :cell_margins
        
        # initialization
        def initialize(**options, &block)
          @cell_background = DEFAULT_CELL_BACKGROUND
          @cell_margins    = DEFAULT_CELL_MARGINS
          
          if content = options.delete(:content)
            p content, options.dup
          end
          
          super options, &block
        end
        
        
        #-------------------------------------------------------------
        # Public Methods
        #-------------------------------------------------------------
        
        #=============== DATA ACCESSORS =======================
        
        def contents
          @contents ||= []
        end
        
        
        #=============== STYLES ===============================
        
        # This method allows styles to be applied to this cell 
        # from the table level.  It attempts to add the style
        # first to the instance, and then to any sub-models that 
        # respond to the method.
        #
        # In all cases, invalid options will simply be ignored.
        #
        def apply_styles(**options)
          # first, try apply to self
          options.each do |(k,v)|
            send(k, v)  if respond_to?(k)
          end
          
          # prevent top-level attrs from trickling down
          options.delete_if { |(k,v)| option_keys.include?(k) }    
          
          # then, try apply to contents
          contents.each do |model|
            options.each do |k,v|
              model.send(k, v) if model.respond_to?(k)
            end
            
            # finally, apply to runs. options do trickle down
            # because paragraph-level styles don't seem to
            # affect runs within tables. weirdsies.
            if model.respond_to?(:runs)
              model.runs.each do |run|
                options.each do |k,v|
                  run.send(k, v) if run.respond_to?(k)
                end
              end
            end
          end
        end
        
        def calculate_width(default_width)
          width(default_width) unless cell_width.to_i > 0
          
          container_width = cell_width - cell_margin_left - cell_margin_right
          
          contents.each do |model|
            if model.respond_to?(:calculate_width)
              model.calculate_width(container_width)    # will always be a TableModel
            end
          end
        end
        
        
        #=============== GETTERS ==============================
        
        # margin attrs
        [:top, :bottom, :left, :right].each do |m|
{'fraction_non_alphanumeric': 0.1703056768558952, 'fraction_numerical': 0.003784570596797671, 'mean_word_length': 1.2557452396585687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3822165', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1660, 'n_words': 456}