Class: Adalog::ActiveRecordRepo
- Inherits:
-
Object
- Object
- Adalog::ActiveRecordRepo
- Defined in:
- lib/adalog/active_record_repo.rb
Instance Attribute Summary collapse
-
#base_relation ⇒ Object
readonly
Returns the value of attribute base_relation.
-
#record_class ⇒ Object
readonly
Returns the value of attribute record_class.
Instance Method Summary collapse
- #all ⇒ Object
- #fetch(**options) ⇒ Object
-
#initialize(record_class, **repo_options) ⇒ ActiveRecordRepo
constructor
A new instance of ActiveRecordRepo.
- #insert(attr_hash = {}, **attr_args) ⇒ Object
Constructor Details
#initialize(record_class, **repo_options) ⇒ ActiveRecordRepo
Returns a new instance of ActiveRecordRepo.
6 7 8 9 |
# File 'lib/adalog/active_record_repo.rb', line 6 def initialize(record_class, **) @record_class = record_class @base_relation = determine_base_relation() end |
Instance Attribute Details
#base_relation ⇒ Object (readonly)
Returns the value of attribute base_relation.
4 5 6 |
# File 'lib/adalog/active_record_repo.rb', line 4 def base_relation @base_relation end |
#record_class ⇒ Object (readonly)
Returns the value of attribute record_class.
4 5 6 |
# File 'lib/adalog/active_record_repo.rb', line 4 def record_class @record_class end |
Instance Method Details
#all ⇒ Object
42 43 44 |
# File 'lib/adalog/active_record_repo.rb', line 42 def all record_class.all.to_a end |
#fetch(**options) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/adalog/active_record_repo.rb', line 12 def fetch(**) = .fetch(:where, {}) = .fetch(:order, :none) relation = (, ) if [:first] relation.first([:first]) elsif [:last] relation.last([:last]) else relation.to_a end end |
#insert(attr_hash = {}, **attr_args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/adalog/active_record_repo.rb', line 26 def insert(attr_hash = {}, **attr_args) attrs = attr_hash.merge(attr_args) record = record_class.new(**attrs) if record.valid? if record.save [:ok, record] else wtf = "Unknown Non-validation error in call to #{record_class}#save" [:error, [wtf]] end else [:error, record.errors.] end end |