lib/json-orm/orm.rb in json-orm-0.1.0 vs lib/json-orm/orm.rb in json-orm-0.2.1

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module JSONORM class ORM attr_reader :database, :transaction_data, :logger def initialize(database, log_file = 'orm.log') @@ -39,11 +41,11 @@ def where(attribute, value) ChainableQuery.new(self, read_data).where(attribute, value) end def create(attributes) - attributes[:id] = next_id unless attributes.key?(:id) + attributes[:id] = next_id unless attributes.key?(:id) && attributes[:id] validate_attributes!(attributes) transaction_data.push(attributes) attributes end @@ -67,14 +69,14 @@ def begin_transaction @transaction_data = read_data.dup end def commit_transaction - logger.info("Starting transaction commit") + logger.info('Starting transaction commit') database.write(transaction_data) - logger.info("Transaction committed successfully") - rescue => e + logger.info('Transaction committed successfully') + rescue StandardError => e logger.error("Failed to commit transaction: #{e.message}") raise "Failed to commit transaction: #{e.message}" ensure @transaction_data = nil end @@ -93,22 +95,19 @@ max_id = read_data.map { |record| record[:id] }.max || 0 max_id + 1 end def validate_attributes!(attributes, check_id = true) - raise "Record must have an id" if check_id && !attributes[:id] + raise 'Record must have an id' if check_id && !attributes[:id] attributes.each do |key, value| validate_attribute(key, value) end end - # Update the validation method def validate_attribute(key, value) - if self.class.custom_validators[key] - self.class.custom_validators[key].call(value) - else - # Default validations (if any) - end + return unless self.class.custom_validators.key?(key) + + self.class.custom_validators[key].call(value) end end -end \ No newline at end of file +end