-
-
Save rentalcustard/4499d38dfa488c08974a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PersonRepository < Norm::PostgreSQLRepository | |
def named(name) | |
select_records(select_statement.where(:name => name) | |
end | |
def named_ordered_by_created_at(name) | |
select_records(select_statement. | |
where(:name => name). | |
order(:created_at, :desc)) | |
end | |
def named_ordered_by_created_at_selecting_foo(:name => name) | |
select_records(select_statement(:foo). | |
where(:name => name). | |
order(:created_at, :desc)) | |
end | |
#and now we have duplication, so maybe we extract a private method: | |
private | |
def select(fields, conditions, order) | |
#stuff | |
end | |
#but quickly this method grows unwieldy and we'd like an object that, like us, | |
# understands SQL, but unlike us accepts arbitrary valid combinations of | |
# selections, projections, groupings etc. | |
def select_statement(*fields) | |
Norm::SQL.select(fields).from('people') | |
end | |
def insert_statement | |
column_list = record_class.attribute_names.join(', ') | |
Norm::SQL.insert("people (#{column_list})").returning('*') | |
end | |
def update_statement | |
Norm::SQL.update('people').returning('*') | |
end | |
def delete_statement | |
Norm::SQL.delete('people').returning('*') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment