无持久性的纯 Ruby 关系代数
PassiveRecord is an extremely lightweight in-memory pseudo-relational algebra.
We implement a simplified subset of AR's interface in pure Ruby.
Do you need to track objects by ID and look them up again, or look them up based on attributes, or even utilize some relational semantics, but have no real need for persistence?
PassiveRecord may be right for you!
include PassiveRecord to get started…`
A class including PassiveRecord will gain the following new instance and class methods.
A class Role which is declared to include PassiveRecord will gain the following instance methods:
role.update(attrs_hash)role.destroyrole.to_hrole.inspect to show ID and visible attributes A class User which is declared to include PassiveRecord will gain the following class methods:
User.all and User.eachUser.create(name: 'Aloysius')User.descendantsUser.destroy(id)User.destroy_allUser.each enumerates over User.all, giving User.count, User.first, etc.User.find(id_or_ids)User.find_by(conditions)User.find_all_by(conditions)User.where(conditions) (returns a PassiveRecord::Query object) A model Child which is declared belongs_to :parent will gain:
child.parentchild.parent_idchild.parent=child.parent_id= A model Parent which declares has_one :child will gain:
parent.childparent.child_idparent.child=parent.child_id=parent.create_child(attrs) A model Parent which declares has_many :children or has_and_belongs_to_many :children will gain:
parent.children (returns a Relation, documented below)parent.children=parent.children_idsparent.children_ids=parent.children<<parent.create_child(attrs)parent.children.all?(&predicate)parent.children.empty?parent.children.where(conditions) (returns a Core::Query)parent.children.pluck(attribute)parent.children.sum(attribute)parent.children.average(attribute)parent.children.mode(attribute) Parent models which declare has_many :children gain a parent.children instance that returns an explicit PassiveRecord relation object, which has the following public methods:
parent.children.allparent.children.each enumerates over parent.children.all, giving parent.children.count, parent.children.first, etc.parent.children.all?(&predicate)parent.children.empty?parent.children.where(conditions) (returns a Core::Query)parent.children<< (insert a new child into the relation) You can acquire Core::Query objects through the class method where. These are chainable, accept nested conditions that traverse relationships, and understand scopes defined as class methods. The query object will have the following public methods:
Post.where(conditions).allPost.where(conditions).each enumerates over where(conditions).all, so we have where(conditions).count, where(conditions).first, etc.Post.where(conditions).create(attrs)Post.where(conditions).first_or_create(attrs)Post.where(conditions).pluck(attr)Post.where(conditions).sum(attr)Post.where(conditions).average(attr)Post.where(conditions).mode(attr)Post.where(conditions).where(further_conditions) (chaining)Post.where.not(conditions) (negation)Post.where(conditions).or(Post.where(conditions)) (disjunction)Post.active.recent (scoping with class methods that return queries; supports chaining) conditions here is expected to be a hash of attribute values. Note that there is special behavior for certain kinds of values.
BETWEEN). For instance you might query for users with birthdays between yesterday and today with User.where(birthday: 1.day.ago...1.day.from_now)IN), so for instance you may query for users whose job title is included in a list of job titles like: User.where(job_title: ['manager', 'developer', 'qa'])Doctor.where(appointments: { patient: patient }) would lookup doctors whose appointments include an appointment with patient.before_create :call_a_methodafter_create :call_another_method, :and_then_call_another_onebefore_update do manually_invoke(a_method) endafter_update { or_use_a_block }before_destroy :somethingafter_destroy { something_else }Copyright (c) 2016 Joseph Weissman
暂无开放 Issues,或尚未同步最近议题。