Class: KDomain::RawDbSchema::Transform
- Inherits:
-
Object
- Object
- KDomain::RawDbSchema::Transform
- Includes:
- KLog::Logging
- Defined in:
- lib/k_domain/raw_db_schema/transform.rb
Overview
class TransformFilter
attr_accessor :take
def initialize(take: :all)
@take = take
end
end
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#schema_loader ⇒ Object
readonly
Returns the value of attribute schema_loader.
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
-
#template_file ⇒ Object
Returns the value of attribute template_file.
Instance Method Summary collapse
-
#apply_filter(schema) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #call ⇒ Object
-
#initialize(source_file, filter) ⇒ Transform
constructor
examples filter = os(run: 1, tables: os(offset: 10, limit: 10)).
-
#schema ⇒ Object
rubocop:disable Security/Eval.
- #write_json(json_file) ⇒ Object
-
#write_schema_loader(target_file) ⇒ Object
rename to target_ruby This is an internal ruby structure that is evaluated writing is only needed for debugging purposes.
Constructor Details
#initialize(source_file, filter) ⇒ Transform
examples
filter = os(run: 1, tables: os(offset: 10, limit: 10))
29 30 31 32 33 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 29 def initialize(source_file, filter) @source_file = source_file @template_file = KDomain::Gem.resource('templates/load_schema.rb') @filter = filter end |
Instance Attribute Details
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
23 24 25 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 23 def filter @filter end |
#schema_loader ⇒ Object (readonly)
Returns the value of attribute schema_loader.
22 23 24 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 22 def schema_loader @schema_loader end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
20 21 22 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 20 def source_file @source_file end |
#template_file ⇒ Object
Returns the value of attribute template_file.
21 22 23 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 21 def template_file @template_file end |
Instance Method Details
#apply_filter(schema) ⇒ Object
rubocop:disable Metrics/AbcSize
97 98 99 100 101 102 103 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 97 def apply_filter(schema) return schema unless filter.active == 1 schema[:tables] = schema[:tables].slice(filter.table.offset, filter.table.limit) || [] if filter.table.offset.is_a?(Integer) && filter.table.limit.is_a?(Integer) schema end |
#call ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 35 def call # log.kv 'source_file', source_file # log.kv 'template_file', template_file # log.kv 'source_file?', File.exist?(source_file) # log.kv 'template_file?', File.exist?(template_file) log.error "Template not found: #{template_file}" unless File.exist?(template_file) content = File.read(source_file) content .gsub!(/ActiveRecord::Schema.define/, 'load') lines = content.lines.map { |line| " #{line}" }.join @schema_loader = File .read(template_file) .gsub('{{source_file}}', source_file) .gsub('{{rails_schema}}', lines) end |
#schema ⇒ Object
rubocop:disable Security/Eval
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 79 def schema if schema_loader.nil? puts '.call method has not been executed' return end eval(schema_loader) # , __FILE__, __LINE__) loader = LoadSchema.new loader.load_schema apply_filter(loader.schema) rescue StandardError => e log.exception(e) end |
#write_json(json_file) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 68 def write_json(json_file) if schema_loader.nil? puts '.call method has not been executed' return end FileUtils.mkdir_p(File.dirname(json_file)) File.write(json_file, JSON.pretty_generate(schema)) end |
#write_schema_loader(target_file) ⇒ Object
rename to target_ruby This is an internal ruby structure that is evaluated writing is only needed for debugging purposes
58 59 60 61 62 63 64 65 66 |
# File 'lib/k_domain/raw_db_schema/transform.rb', line 58 def write_schema_loader(target_file) if schema_loader.nil? puts '.call method has not been executed' return end FileUtils.mkdir_p(File.dirname(target_file)) File.write(target_file, schema_loader) end |