<commit_before>require "attachment_magick/configuration/configuration"
require "attachment_magick/dragonfly/init"
require "attachment_magick/dsl"
require 'attachment_magick/railtie'

module AttachmentMagick
  attr_accessor :attachment_magick_default_options
  attr_accessor :attachment_default_view

  class << self
    attr_accessor :configuration
  end

  def self.setup
    self.configuration ||= Configuration.new
    yield(configuration)
  end

  def attachment_magick(&block)
    define_relationship(self)
    default_grids = generate_grids
    map           = DSL.new(self, default_grids)
    map.instance_eval(&block)

    @attachment_magick_default_options = {:styles => map.styles || default_grids}
    grid_methods
  end

  def generate_grids(column_amount=AttachmentMagick.configuration.columns_amount, column_width=AttachmentMagick.configuration.columns_width, gutter=AttachmentMagick.configuration.gutter, only=[])
    hash = {}
    grids_to_create = only.empty? ? 1.upto(column_amount) : only

    grids_to_create.each do |key|
      grid  = ("grid_#{key}").to_sym
      value = (key * column_width) + (gutter * (key - 1))
      hash.merge!({grid => {:width => value}})
    end

    unless AttachmentMagick.configuration.custom_styles.styles.empty?
      AttachmentMagick.configuration.custom_styles.styles.each do |key, value|
        option = value
        if value.is_a?(String)
          width, height = value.split("x")
          option        = {:width => width.to_i}
          option.merge!({:height => height.to_i}) if height
        end

        hash.merge!({key.to_sym => option})
      end
    end

    return hash
  end

  def grid_methods
    @attachment_magick_default_options[:styles].each do |key, value|
      self.send :define_singleton_method, "style_#{key.to_s}" do
        metric = "#{value[:width]}x#{value[:height]}"
        metric = "#{metric}#" if value[:height] && value[:crop] == true

        return metric
      end
    end
  end

  def define_relationship(klass)
    embeds_many :images,  :class_name => "AttachmentMagick::MongoidImage",      :polymorphic => true  if klass.include?(Mongoid::Document)
    has_many    :images,  :class_name => "AttachmentMagick::ActiveRecordImage", :as => :imageable, :dependent => :destroy if klass.include?(ActiveRecord::Persistence)

    accepts_nested_attributes_for :images
  end

  private :define_relationship
  private :generate_grids
  private :grid_methods
end<commit_msg>bugfix - error if mongoid not present<commit_after>require "attachment_magick/configuration/configuration"
require "attachment_magick/dragonfly/init"
require "attachment_magick/dsl"
require 'attachment_magick/railtie'

module AttachmentMagick
  attr_accessor :attachment_magick_default_options
  attr_accessor :attachment_default_view

  class << self
    attr_accessor :configuration
  end

  def self.setup
    self.configuration ||= Configuration.new
    yield(configuration)
  end

  def attachment_magick(&block)
    define_relationship(self)
    default_grids = generate_grids
    map           = DSL.new(self, default_grids)
    map.instance_eval(&block)

    @attachment_magick_default_options = {:styles => map.styles || default_grids}
    grid_methods
  end

  def generate_grids(column_amount=AttachmentMagick.configuration.columns_amount, column_width=AttachmentMagick.configuration.columns_width, gutter=AttachmentMagick.configuration.gutter, only=[])
    hash = {}
    grids_to_create = only.empty? ? 1.upto(column_amount) : only

    grids_to_create.each do |key|
      grid  = ("grid_#{key}").to_sym
      value = (key * column_width) + (gutter * (key - 1))
      hash.merge!({grid => {:width => value}})
    end

    unless AttachmentMagick.configuration.custom_styles.styles.empty?
      AttachmentMagick.configuration.custom_styles.styles.each do |key, value|
        option = value
        if value.is_a?(String)
          width, height = value.split("x")
          option        = {:width => width.to_i}
          option.merge!({:height => height.to_i}) if height
        end

        hash.merge!({key.to_sym => option})
      end
    end

    return hash
  end

  def grid_methods
    @attachment_magick_default_options[:styles].each do |key, value|
      self.send :define_singleton_method, "style_#{key.to_s}" do
        metric = "#{value[:width]}x#{value[:height]}"
        metric = "#{metric}#" if value[:height] && value[:crop] == true

        return metric
      end
    end
  end

  def define_relationship(klass)
    embeds_many :images,  :class_name => "AttachmentMagick::MongoidImage",      :polymorphic => true  if klass.include?(Mongoid::Document) if defined? Mongoid::Document
    has_many    :images,  :class_name => "AttachmentMagick::ActiveRecordImage", :as => :imageable, :dependent => :destroy if klass.include?(ActiveRecord::Persistence) if defined? ActiveRecord::Persistence

    accepts_nested_attributes_for :images
  end

  private :define_relationship
  private :generate_grids
  private :grid_methods
end
{'fraction_non_alphanumeric': 0.10833998403830807, 'fraction_numerical': 0.0007980845969672786, 'mean_word_length': 3.8109404990403073, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, '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': '3592067', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1531, 'n_words': 302}