Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
Forked from ernie/repo.rb
Last active August 29, 2015 14:01
Show Gist options
  • Save rentalcustard/4499d38dfa488c08974a to your computer and use it in GitHub Desktop.
Save rentalcustard/4499d38dfa488c08974a to your computer and use it in GitHub Desktop.
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