Class: JSONORM::ORM
- Inherits:
-
Object
- Object
- JSONORM::ORM
- Defined in:
- lib/json-orm/orm.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#transaction_data ⇒ Object
readonly
Returns the value of attribute transaction_data.
Class Method Summary collapse
- .custom_validators ⇒ Object
-
.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.
Instance Method Summary collapse
- #all ⇒ Object
- #begin_transaction ⇒ Object
- #commit_transaction ⇒ Object
- #create(attributes) ⇒ Object
- #delete(id) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(database, log_file = 'orm.log') ⇒ ORM
constructor
A new instance of ORM.
- #rollback_transaction ⇒ Object
- #update(id, new_attributes) ⇒ Object
- #where(attribute, value) ⇒ Object
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
#database ⇒ Object (readonly)
Returns the value of attribute database.
5 6 7 |
# File 'lib/json-orm/orm.rb', line 5 def database @database end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/json-orm/orm.rb', line 5 def logger @logger end |
#transaction_data ⇒ Object (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_validators ⇒ Object
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
#all ⇒ Object
33 34 35 |
# File 'lib/json-orm/orm.rb', line 33 def all read_data end |
#begin_transaction ⇒ Object
69 70 71 |
# File 'lib/json-orm/orm.rb', line 69 def begin_transaction @transaction_data = read_data.dup end |
#commit_transaction ⇒ Object
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_transaction ⇒ Object
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 |