<commit_before>require 'easy_modal_window/version'
require 'easy_modal_window/helpers'

module EasyModalWindow
  class Dialog

    attr_reader :modal_options

    ALLOVED_OPTIONS = [:window_selector, :title_selector, :render_template, :formats,
                       :resizable, :height, :width, :buttons, :before_close, :success_message,
                       :object, :container_class, :element_class, :errors_group_class].freeze

    def initialize(args)
      @modal_options = {}
      @modal_options[:window_selector] = '#ajax-modal'
      @modal_options[:title_selector] = 'h3.title'
      @modal_options[:render_template] = ''
      @modal_options[:formats] = [:html]
      @modal_options[:resizable] = true
      @modal_options[:height] = 'auto'
      @modal_options[:width] = 'auto'
      # hash with button descriptions: {first_button: {name: 'Name',
      #                                                condition: -> {...},
      #                                                action: 'location.reload();'},
      #                                 second_button: ...}
      @modal_options[:buttons] = {}
      # dialog callbacks
      # {condition: ->{...}, action: '...'}
      @modal_options[:before_close] = {}

      # options for result templates
      @modal_options[:success_message] = 'All right'
      @modal_options[:object] = nil
      @modal_options[:container_class] = ''
      @modal_options[:element_class] = ''
      @modal_options[:errors_group_class] = ''

      @modal_options.merge!(args.slice(*EasyModalWindow::Dialog::ALLOVED_OPTIONS))
    end

    (ALLOVED_OPTIONS - [:before_close, :buttons]).each do |method|
      class_eval <<-EOT, __FILE__, __LINE__ + 1
        def #{method}
          @modal_options[:#{method}]
        end
      EOT
    end

    def before_close
      result = {}
      if @modal_options[:before_close].has_key?(:action) && @modal_options[:before_close][:action].present?
        result[:action] = @modal_options[:before_close][:action]
      else
        result[:condition] = false
        result[:action] = ''
        return result
      end

      if @modal_options[:before_close].has_key?(:condition)
        result[:condition] = value_of(@modal_options[:before_close][:condition])
      else
        result[:condition] = true
      end

      result
    end

    def buttons
      @modal_options[:buttons].inject([]) do |h, v|
        option = {}
        option[:name] = v.last[:name] || 'unknown'
        option[:condition] = value_of(v.last[:condition])
        option[:action] = v.last[:action] || ''
        h << option
        h
      end
    end

  private

    def value_of(var)
      var.is_a?(Proc) ? var.call : var
    end

  end
end<commit_msg>Bugfix<commit_after>require 'easy_modal_window/version'
require 'easy_modal_window/helpers'

module EasyModalWindow
  class Dialog

    attr_reader :modal_options

    ALLOVED_OPTIONS = [:window_selector, :title_selector, :render_template, :formats,
                       :resizable, :height, :width, :buttons, :before_close, :success_message,
                       :object, :container_class, :element_class, :errors_group_class].freeze

    def initialize(args)
      @modal_options = {}
      @modal_options[:window_selector] = '#ajax-modal'
      @modal_options[:title_selector] = 'h3.title'
      @modal_options[:render_template] = ''
      @modal_options[:formats] = [:html]
      @modal_options[:resizable] = true
      @modal_options[:height] = 'auto'
      @modal_options[:width] = 'auto'
      # hash with button descriptions: {first_button: {name: 'Name',
      #                                                condition: -> {...},
      #                                                action: 'location.reload();'},
      #                                 second_button: ...}
      @modal_options[:buttons] = {}
      # dialog callbacks
      # {condition: ->{...}, action: '...'}
      @modal_options[:before_close] = {}

      # options for result templates
      @modal_options[:success_message] = 'All right'
      @modal_options[:object] = nil
      @modal_options[:container_class] = ''
      @modal_options[:element_class] = ''
      @modal_options[:errors_group_class] = ''

      @modal_options.merge!(args.slice(*EasyModalWindow::Dialog::ALLOVED_OPTIONS))
    end

    (ALLOVED_OPTIONS - [:before_close, :buttons]).each do |method|
      class_eval <<-EOT, __FILE__, __LINE__ + 1
        def #{method}
          @modal_options[:#{method}]
        end
      EOT
    end

    # TODO: simplify this method
    def before_close
      result = {}
      if @modal_options[:before_close].has_key?(:action) && @modal_options[:before_close][:action].present?
        result[:action] = @modal_options[:before_close][:action]
      else
        result[:condition] = false
        result[:action] = ''
        return result
      end

      result[:condition] = @modal_options[:before_close][:condition].nil? || value_of(@modal_options[:before_close][:condition])

      result
    end

    def buttons
      @modal_options[:buttons].inject([]) do |h, v|
        option = {}
        option[:name] = v.last[:name] || 'unknown'
        option[:condition] = value_of(v.last[:condition]).nil? || value_of(v.last[:condition])
        option[:action] = v.last[:action] || ''
        h << option
        h
      end
    end

  private

    def value_of(var)
      var.is_a?(Proc) ? var.call : var
    end

  end
end
{'fraction_non_alphanumeric': 0.13776634827332843, 'fraction_numerical': 0.0007347538574577516, 'mean_word_length': 2.4052532833020637, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6578670', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1582, 'n_words': 281}