Class: Rails::Crud::Tools::CrudData

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rails/crud/tools/crud_data.rb

Overview

The CrudData class is responsible for loading and managing CRUD data from a file. It includes methods to load data, reload if needed, and retrieve specific information from the data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCrudData

Returns a new instance of CrudData.



17
18
19
20
21
22
# File 'lib/rails/crud/tools/crud_data.rb', line 17

def initialize
  @process_id = nil
  @crud_rows = {}
  @crud_cols = {}
  @last_loaded_time = nil
end

Instance Attribute Details

#crud_colsObject

Returns the value of attribute crud_cols.



15
16
17
# File 'lib/rails/crud/tools/crud_data.rb', line 15

def crud_cols
  @crud_cols
end

#crud_rowsObject

Returns the value of attribute crud_rows.



15
16
17
# File 'lib/rails/crud/tools/crud_data.rb', line 15

def crud_rows
  @crud_rows
end

#last_loaded_timeObject

Returns the value of attribute last_loaded_time.



15
16
17
# File 'lib/rails/crud/tools/crud_data.rb', line 15

def last_loaded_time
  @last_loaded_time
end

#process_idObject

Returns the value of attribute process_id.



15
16
17
# File 'lib/rails/crud/tools/crud_data.rb', line 15

def process_id
  @process_id
end

#workbookObject

Returns the value of attribute workbook.



15
16
17
# File 'lib/rails/crud/tools/crud_data.rb', line 15

def workbook
  @workbook
end

Instance Method Details

#crud_sheetObject

CRUDシートを取得する



107
108
109
110
111
112
113
# File 'lib/rails/crud/tools/crud_data.rb', line 107

def crud_sheet
  sheet_name = CrudConfig.instance.config.crud_file.sheet_name
  sheet = @workbook[sheet_name]
  raise "CRUD sheet '#{sheet_name}' not found" if sheet.nil?

  sheet
end

#get_last_modified_by(file_path) ⇒ Object

xlsxファイルの最終更新者を取得する



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rails/crud/tools/crud_data.rb', line 81

def get_last_modified_by(file_path)
  CrudLogger.logger.debug "Starting to read ZIP file: #{file_path}"
  CrudLogger.logger.debug "File size: #{File.size(file_path)}"

  last_modified_by = nil
  File.open(file_path, "r") do |f|
    f.flock(File::LOCK_SH)
    begin
      Zip::File.open(file_path) do |zipfile|
        doc_props = zipfile.find_entry("docProps/core.xml")
        if doc_props
          content = doc_props.get_input_stream.read
          last_modified_by = content[%r{<cp:lastModifiedBy>(.*?)</cp:lastModifiedBy>}, 1]
        else
          CrudLogger.logger.warn "docProps/core.xml が見つかりませんでした。"
        end
      end
    ensure
      f.flock(File::LOCK_UN)
    end
  end

  last_modified_by
end

#load_crud_dataObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails/crud/tools/crud_data.rb', line 24

def load_crud_data
  config = CrudConfig.instance.config
  return unless config.enabled

  unless File.exist?(config.crud_file_path)
    CrudLogger.logger.warn "CRUD file not found: #{config.crud_file_path}"
    return false
  end

  @workbook = RubyXL::Parser.parse(config.crud_file_path)
  @last_loaded_time = File.mtime(config.crud_file_path)
  sheet = crud_sheet
  headers = sheet[0].cells.map(&:value)

  method_col_index = headers.index(config.method_col)
  action_col_index = headers.index(config.action_col)
  table_start_col_index = headers.index(config.table_start_col)

  raise "Method column not found" unless method_col_index
  raise "Action column not found" unless action_col_index
  raise "Table start column not found" unless table_start_col_index

  headers[table_start_col_index..].each_with_index do |table_name, index|
    @crud_cols[table_name] = table_start_col_index + index
  end

  sheet.each_with_index do |row, index|
    next if index.zero?

    method = row[method_col_index]&.value.to_s.strip
    method = Constants::DEFAULT_METHOD if method.empty?
    value = row[action_col_index]&.value
    split_value = value&.split
    action = split_value&.first
    next if action.nil?

    @crud_rows[method] ||= {}
    @crud_rows[method][action] = index
  end
end

#reload_if_neededObject

CRUDデータが更新された場合に再読み込みする



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rails/crud/tools/crud_data.rb', line 66

def reload_if_needed
  config = CrudConfig.instance.config
  return unless config.enabled

  return unless @last_loaded_time.nil? || File.mtime(config.crud_file_path) > @last_loaded_time

  last_modified_by = get_last_modified_by(config.crud_file_path)
  CrudLogger.logger.info "last modified by: #{last_modified_by}. process_id: #{process_id}"
  return if process_id == last_modified_by

  CrudLogger.logger.info "Reloading CRUD data due to file modification. last_loaded_time = #{@last_loaded_time}"
  load_crud_data
end