Horza
Horza is a library for decoupling your application from the ORM you have implemented.
ActiveRecord Example
horza_user = Horza.adapt(User)
user.get(id)
user.get!(id)
user.find_first(options)
user.find_first!(options)
user.find_all(options)
user.create(options)
user.create!(options)
user.update(options)
user.update!(options)
user.delete(options)
user.delete!(options)
user.association(target: :employer, via: [])
parent = { id: parent_id, klass: :employer }
user.create_as_child(parent, options)
user.create_as_child!(parent, options)
conditions = { last_name: 'Turner' }
user.find_all(conditions: conditions, order: { last_name: :desc })
user.find_all(conditions: conditions, limit: 20)
user.find_all(conditions: conditions, offset: 50)
employer.association(target: :users, eager_load: true)
join_params = {
with: :employers,
on: { employer_id: :id },
fields: {
users: [:first_name, :last_name, :email],
employers: [:company_name, :address, :phone],
},
conditions: {
users: { last_name: 'Turner' },
employers: { company_name: 'Corporation ltd.' }
},
limit: 20,
offset: 5,
}
horza_user.join(join_params)
join_params = {
with: :employers,
on: [
{ employer_id: :id },
{ email: :email },
]
}
horza_user.join(join_params)
join_params = {
with: :employers,
on: [
{ employer_id: :id },
{ email: :email },
],
fields: {
users: [:id, :last_name, :email],
employers: [{id: :employer_id}],
},
}
horza_user.join(join_params)
Outputs
Horza queries return very dumb vanilla entities instead of ORM response objects.
Singular entities are simply hashes that allow both hash and dot notation, and binary helpers.
Collection entities behave like arrays.
result = horza_user.find_first(first_name: 'Blake')
result
result.class.name
result['id']
result.id
result.id?
result = horza_user.find_all(last_name: 'Turner')
result.class.name
result.length
result.size
result.empty?
result.present?
result.first
result.last
result[0]
Custom Entities
You can define your own entities by making them subclasses of Horza entities. Just make sure they have the same class name as your ORM classes. Horza will automatically detect custom entities and use them for output.
module CustomEntities
class Users < Horza::Entities::Collection
end
class User < Horza::Entities::Single
end
end
Active Model Semantics
The returned entity for ActiveRecord adapter now include ActiveModel semantics. This type of
entity SingleWithActiveModel can work with rails form helper and the submitted params will be
grouped under params[:horza_entities_single_with_active_model].
Config
Horza uses ORM-specific adapters to decouple Ruby apps from their ORMS.
Currently only the ActiveRecord adapter is supported.
Configure Adapter
Horza.configure do |config|
config.adapter = :active_record
end
Constant paths
Some of the operations in Horza involve resolving constants from the given options.
Consider the following snippet from above:
user.create_as_child(id: parent_id, klass: :employer, options)
Horza by default will return the Employer class if it has already been loaded.
In environments where constants are lazy-loaded - such as in Rails - you would need to explicitly configure paths that Horza should look up when trying to resolve constants.
For instance in a Rails app your models are typically found in the app/models
directory,
so you would need to add it to the constant_paths.
Horza.configure do |config|
config.constant_paths += [Rails.root.join("app/models").to_s]
end
Note The file paths added to constant_paths would also need to be added to your app's autoload_paths for the constants to be loaded dynamically.
Model Namespaces
Horza will attempt to resolve namespaced constants where possibble provided the following caveats are met:
- The file names added to the constant_paths and the constants defined in them follow the Rails naming conventions.
- There aren't multiple files with the same base name in a given directory in the constant paths.
To illustrate the second point consider the following directory structure:
class Employer < ActiveRecord::Base
has_many :users
end
module MyNamespace
class Employer < ActiveRecord::Base
has_many :users
end
end
Resetting config (At runtime or on app boot)
Horza.reset