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.
5 6 7 8 9 10 |
# File 'lib/json-orm/orm.rb', line 5 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.
3 4 5 |
# File 'lib/json-orm/orm.rb', line 3 def database @database end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
3 4 5 |
# File 'lib/json-orm/orm.rb', line 3 def logger @logger end |
#transaction_data ⇒ Object (readonly)
Returns the value of attribute transaction_data.
3 4 5 |
# File 'lib/json-orm/orm.rb', line 3 def transaction_data @transaction_data end |
Class Method Details
.custom_validators ⇒ Object
27 28 29 |
# File 'lib/json-orm/orm.rb', line 27 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
22 23 24 25 |
# File 'lib/json-orm/orm.rb', line 22 def self.register_validator(attribute, &block) @custom_validators ||= {} @custom_validators[attribute] = block end |
Instance Method Details
#all ⇒ Object
31 32 33 |
# File 'lib/json-orm/orm.rb', line 31 def all read_data end |
#begin_transaction ⇒ Object
67 68 69 |
# File 'lib/json-orm/orm.rb', line 67 def begin_transaction @transaction_data = read_data.dup end |
#commit_transaction ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/json-orm/orm.rb', line 71 def commit_transaction logger.info("Starting transaction commit") database.write(transaction_data) logger.info("Transaction committed successfully") rescue => e logger.error("Failed to commit transaction: #{e.}") raise "Failed to commit transaction: #{e.}" ensure @transaction_data = nil end |
#create(attributes) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/json-orm/orm.rb', line 43 def create(attributes) attributes[:id] = next_id unless attributes.key?(:id) validate_attributes!(attributes) transaction_data.push(attributes) attributes end |
#delete(id) ⇒ Object
63 64 65 |
# File 'lib/json-orm/orm.rb', line 63 def delete(id) transaction_data.reject! { |record| record[:id] == id } end |
#find(id) ⇒ Object
35 36 37 |
# File 'lib/json-orm/orm.rb', line 35 def find(id) read_data.detect { |record| record[:id] == id } end |
#rollback_transaction ⇒ Object
82 83 84 |
# File 'lib/json-orm/orm.rb', line 82 def rollback_transaction @transaction_data = nil end |
#update(id, new_attributes) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/json-orm/orm.rb', line 50 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
39 40 41 |
# File 'lib/json-orm/orm.rb', line 39 def where(attribute, value) ChainableQuery.new(self, read_data).where(attribute, value) end |