Skip to content

Instantly share code, notes, and snippets.

@sshkarupa
Forked from odigity/sequel_scopes.rb
Created October 19, 2016 09:37
Show Gist options
  • Save sshkarupa/afb47185eb1775b32844cc64a091bfdf to your computer and use it in GitHub Desktop.
Save sshkarupa/afb47185eb1775b32844cc64a091bfdf to your computer and use it in GitHub Desktop.

Revisions

  1. @odigity odigity revised this gist Oct 7, 2015. 2 changed files with 77 additions and 77 deletions.
    77 changes: 0 additions & 77 deletions The Sequel Gem: Everything About Scopes
    Original file line number Diff line number Diff line change
    @@ -1,77 +0,0 @@
    (I recommend understanding the basics of this first: http://sequel.jeremyevans.net/rdoc/files/doc/object_model_rdoc.html)

    Extending the underlying dataset (http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Extending+the+underlying+dataset)

    The recommended way to implement table-wide logic by defining methods on the dataset using dataset_module:

    class Post < Sequel::Model
    dataset_module do
    def posts_with_few_comments
    where{num_comments < 30}
    end

    def clean_posts_with_few_comments
    posts_with_few_comments.delete
    end
    end
    end

    This allows you to have access to your model API from filtered datasets as well:

    Post.where(:category => 'ruby').clean_posts_with_few_comments

    Sequel models also provide a subset class method that creates a dataset method with a simple filter:

    class Post < Sequel::Model
    subset(:posts_with_few_comments){num_comments < 30}
    subset :invisible, Sequel.~(:visible)
    end

    Model ((http://www.rubydoc.info/gems/sequel/Sequel/Model))

    DATASET_METHODS : Class methods added to model that call the method of the same name on the dataset
    Dataset::ACTION_METHODS + Dataset::QUERY_METHODS

    Model::ClassMethods (http://www.rubydoc.info/gems/sequel/Sequel/Model/ClassMethods)

    #subset(name, *args, &block) ⇒ Object

    Sets up a dataset method that returns a filtered dataset. Sometimes thought of as a scope, and like most dataset methods, they can be chained. For example:

    Topic.subset(:joes, :username.like('%joe%'))
    Topic.subset(:popular){num_posts > 100}
    Topic.subset(:recent){created_on > Date.today - 7}

    Allows you to do `Topic.joes.recent.popular` to get topics with a username that includes joe that have more than 100 posts and were created less than 7 days ago.
    Both the args given and the block are passed to Dataset#filter.
    This method creates dataset methods that do not accept arguments.
    To create dataset methods that accept arguments, you should use define a method directly inside a #dataset_module block.

    Model::DatasetModule (http://www.rubydoc.info/gems/sequel/Sequel/Model/DatasetModule)

    This Module subclass is used by Model.dataset_module to add dataset methods to classes.
    It adds a couple of features standard Modules, allowing you to use the same subset method you can call on Model,
    as well as making sure that public methods added to the module automatically have class methods created for them.

    #subset(name, *args, &block) ⇒ Object

    Define a named filter for this dataset, see Model.subset for details.

    ActiveRecord -- named_scope (http://sequel.jeremyevans.net/rdoc/files/doc/active_record_rdoc.html#label-named_scope)

    For a pure filter, you can use subset:

    Album.subset(:debut, :position => 1)
    Album.subset(:gold){copies_sold > 500000}

    For anything more complex, you can use dataset_module:

    Album.dataset_module do
    def by_artist(artist_id)
    where(:artist_id=>artist_id)
    end

    def by_release_date
    order(:release_date)
    end
    end
    77 changes: 77 additions & 0 deletions sequel_scopes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    # (I recommend understanding the basics of this first: http://sequel.jeremyevans.net/rdoc/files/doc/object_model_rdoc.html)

    # Extending the underlying dataset (http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Extending+the+underlying+dataset)

    # The recommended way to implement table-wide logic by defining methods on the dataset using dataset_module:

    class Post < Sequel::Model
    dataset_module do
    def posts_with_few_comments
    where{num_comments < 30}
    end

    def clean_posts_with_few_comments
    posts_with_few_comments.delete
    end
    end
    end

    # This allows you to have access to your model API from filtered datasets as well:

    Post.where(:category => 'ruby').clean_posts_with_few_comments

    # Sequel models also provide a subset class method that creates a dataset method with a simple filter:

    class Post < Sequel::Model
    subset(:posts_with_few_comments){num_comments < 30}
    subset :invisible, Sequel.~(:visible)
    end

    # Model ((http://www.rubydoc.info/gems/sequel/Sequel/Model))

    DATASET_METHODS : Class methods added to model that call the method of the same name on the dataset
    Dataset::ACTION_METHODS + Dataset::QUERY_METHODS

    # Model::ClassMethods (http://www.rubydoc.info/gems/sequel/Sequel/Model/ClassMethods)

    #subset(name, *args, &block) ⇒ Object

    # Sets up a dataset method that returns a filtered dataset. Sometimes thought of as a scope, and like most dataset methods, they can be chained. For example:

    Topic.subset(:joes, :username.like('%joe%'))
    Topic.subset(:popular){num_posts > 100}
    Topic.subset(:recent){created_on > Date.today - 7}

    # Allows you to do `Topic.joes.recent.popular` to get topics with a username that includes joe that have more than 100 posts and were created less than 7 days ago.
    # Both the args given and the block are passed to Dataset#filter.
    # This method creates dataset methods that do not accept arguments.
    # To create dataset methods that accept arguments, you should use define a method directly inside a #dataset_module block.

    # Model::DatasetModule (http://www.rubydoc.info/gems/sequel/Sequel/Model/DatasetModule)

    # This Module subclass is used by Model.dataset_module to add dataset methods to classes.
    # It adds a couple of features standard Modules, allowing you to use the same subset method you can call on Model,
    # as well as making sure that public methods added to the module automatically have class methods created for them.

    #subset(name, *args, &block) ⇒ Object

    # Define a named filter for this dataset, see Model.subset for details.

    # ActiveRecord -- named_scope (http://sequel.jeremyevans.net/rdoc/files/doc/active_record_rdoc.html#label-named_scope)

    # For a pure filter, you can use subset:

    Album.subset(:debut, :position => 1)
    Album.subset(:gold){copies_sold > 500000}

    # For anything more complex, you can use dataset_module:

    Album.dataset_module do
    def by_artist(artist_id)
    where(:artist_id=>artist_id)
    end

    def by_release_date
    order(:release_date)
    end
    end
  2. @odigity odigity created this gist Oct 7, 2015.
    77 changes: 77 additions & 0 deletions The Sequel Gem: Everything About Scopes
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    (I recommend understanding the basics of this first: http://sequel.jeremyevans.net/rdoc/files/doc/object_model_rdoc.html)

    Extending the underlying dataset (http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Extending+the+underlying+dataset)

    The recommended way to implement table-wide logic by defining methods on the dataset using dataset_module:

    class Post < Sequel::Model
    dataset_module do
    def posts_with_few_comments
    where{num_comments < 30}
    end

    def clean_posts_with_few_comments
    posts_with_few_comments.delete
    end
    end
    end

    This allows you to have access to your model API from filtered datasets as well:

    Post.where(:category => 'ruby').clean_posts_with_few_comments

    Sequel models also provide a subset class method that creates a dataset method with a simple filter:

    class Post < Sequel::Model
    subset(:posts_with_few_comments){num_comments < 30}
    subset :invisible, Sequel.~(:visible)
    end

    Model ((http://www.rubydoc.info/gems/sequel/Sequel/Model))

    DATASET_METHODS : Class methods added to model that call the method of the same name on the dataset
    Dataset::ACTION_METHODS + Dataset::QUERY_METHODS

    Model::ClassMethods (http://www.rubydoc.info/gems/sequel/Sequel/Model/ClassMethods)

    #subset(name, *args, &block) ⇒ Object

    Sets up a dataset method that returns a filtered dataset. Sometimes thought of as a scope, and like most dataset methods, they can be chained. For example:

    Topic.subset(:joes, :username.like('%joe%'))
    Topic.subset(:popular){num_posts > 100}
    Topic.subset(:recent){created_on > Date.today - 7}

    Allows you to do `Topic.joes.recent.popular` to get topics with a username that includes joe that have more than 100 posts and were created less than 7 days ago.
    Both the args given and the block are passed to Dataset#filter.
    This method creates dataset methods that do not accept arguments.
    To create dataset methods that accept arguments, you should use define a method directly inside a #dataset_module block.

    Model::DatasetModule (http://www.rubydoc.info/gems/sequel/Sequel/Model/DatasetModule)

    This Module subclass is used by Model.dataset_module to add dataset methods to classes.
    It adds a couple of features standard Modules, allowing you to use the same subset method you can call on Model,
    as well as making sure that public methods added to the module automatically have class methods created for them.

    #subset(name, *args, &block) ⇒ Object

    Define a named filter for this dataset, see Model.subset for details.

    ActiveRecord -- named_scope (http://sequel.jeremyevans.net/rdoc/files/doc/active_record_rdoc.html#label-named_scope)

    For a pure filter, you can use subset:

    Album.subset(:debut, :position => 1)
    Album.subset(:gold){copies_sold > 500000}

    For anything more complex, you can use dataset_module:

    Album.dataset_module do
    def by_artist(artist_id)
    where(:artist_id=>artist_id)
    end

    def by_release_date
    order(:release_date)
    end
    end