Class: JSONORM::ORM

Inherits:
Object
  • Object
show all
Defined in:
lib/json-orm/orm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, log_file = 'orm.log') ⇒ ORM

Returns a new instance of ORM.



7
8
9
10
11
12
# File 'lib/json-orm/orm.rb', line 7

def initialize(database, log_file = 'orm.log')
  @database = database
  @transaction_data = nil
  @attributes = {}
  @logger = Logger.new(log_file)
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/json-orm/orm.rb', line 5

def database
  @database
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/json-orm/orm.rb', line 5

def logger
  @logger
end

#transaction_dataObject (readonly)

Returns the value of attribute transaction_data.



5
6
7
# File 'lib/json-orm/orm.rb', line 5

def transaction_data
  @transaction_data
end

Class Method Details

.custom_validatorsObject



29
30
31
# File 'lib/json-orm/orm.rb', line 29

def self.custom_validators
  @custom_validators || {}
end

.register_validator(attribute, &block) ⇒ Object

# Define another custom validator, for example, for ‘age’ ORM.register_validator(‘age’) do |value|

raise "Invalid age" unless value.is_a?(Integer) && value >= 0

end



24
25
26
27
# File 'lib/json-orm/orm.rb', line 24

def self.register_validator(attribute, &block)
  @custom_validators ||= {}
  @custom_validators[attribute] = block
end

Instance Method Details

#allObject



33
34
35
# File 'lib/json-orm/orm.rb', line 33

def all
  read_data
end

#begin_transactionObject



69
70
71
# File 'lib/json-orm/orm.rb', line 69

def begin_transaction
  @transaction_data = read_data.dup
end

#commit_transactionObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/json-orm/orm.rb', line 73

def commit_transaction
  logger.info('Starting transaction commit')
  database.write(transaction_data)
  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

#create(attributes) ⇒ Object



45
46
47
48
49
50
# File 'lib/json-orm/orm.rb', line 45

def create(attributes)
  attributes[:id] = next_id unless attributes.key?(:id) && attributes[:id]
  validate_attributes!(attributes)
  transaction_data.push(attributes)
  attributes
end

#delete(id) ⇒ Object



65
66
67
# File 'lib/json-orm/orm.rb', line 65

def delete(id)
  transaction_data.reject! { |record| record[:id] == id }
end

#find(id) ⇒ Object



37
38
39
# File 'lib/json-orm/orm.rb', line 37

def find(id)
  read_data.detect { |record| record[:id] == id }
end

#rollback_transactionObject



84
85
86
# File 'lib/json-orm/orm.rb', line 84

def rollback_transaction
  @transaction_data = nil
end

#update(id, new_attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/json-orm/orm.rb', line 52

def update(id, new_attributes)
  validate_attributes!(new_attributes, false)
  transaction_data.map! do |record|
    if record[:id] == id
      updated_record = record.merge(new_attributes)
      validate_attributes!(updated_record, false) # Validate after merge
      updated_record
    else
      record
    end
  end
end

#where(attribute, value) ⇒ Object



41
42
43
# File 'lib/json-orm/orm.rb', line 41

def where(attribute, value)
  ChainableQuery.new(self, read_data).where(attribute, value)
end