Skip to content

Instantly share code, notes, and snippets.

@lcuevastodoit
Last active September 26, 2022 00:50
Show Gist options
  • Save lcuevastodoit/0ac249f4d27860f94aae8033fb780ebf to your computer and use it in GitHub Desktop.
Save lcuevastodoit/0ac249f4d27860f94aae8033fb780ebf to your computer and use it in GitHub Desktop.
Configure User Snippets: Ruby Design Patterns Snippets for VSCode
{
// Place your snippets for ruby here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"require": {
"prefix": "rb.require",
"body": [
"require '$1'",
"$2"
],
"description": "require"
},
"def initialize": {
"prefix": "rb.def.initialize",
"body": [
"def initialize(data, options = {$1})",
" @data = data",
" @options = options",
"end"
],
"description": "def initialize"
},
"def call method": {
"prefix": "rb.def.call",
"body": [
"def call",
" $1",
"end"
],
"description": "call method simple"
},
"def call method with params": {
"prefix": "rb.def.call.params",
"body": [
"def call($1)",
" $2",
"end"
],
"description": "call method with params"
},
"def call method with params and options": {
"prefix": "rb.def.call.params.options",
"body": [
"def call($1, options = {$2})",
" $3",
"end"
],
"description": "call method with params and options"
},
"def call method with options": {
"prefix": "rb.def.call.options",
"body": [
"def call(options = {$1})",
" $2",
"end"
]
},
"def call method with options and block": {
"prefix": "rb.def.call.options.block",
"body": [
"def call(options = {$1})",
" $2",
" yield",
"end"
]
},
"ternary operator": {
"prefix": "rb.ternary",
"body": [
"$1 ? $2 : $3"
]
},
"if statement": {
"prefix": "rb.if",
"body": [
"if $1",
" $2",
"end"
]
},
"if else statement": {
"prefix": "rb.if.else",
"body": [
"if $1",
" $2",
"else",
" $3",
"end"
]
},
"if elsif else statement": {
"prefix": "rb.if.elsif.else",
"body": [
"if $1",
" $2",
"elsif $3",
" $4",
"else",
" $5",
"end"
]
},
"unless statement": {
"prefix": "rb.unless",
"body": [
"unless $1",
" $2",
"end"
]
},
"unless else statement": {
"prefix": "rb.unless.else",
"body": [
"unless $1",
" $2",
"else",
" $3",
"end"
]
},
"case statement": {
"prefix": "rb.case",
"body": [
"case $1",
"when $2",
" $3",
"end"
]
},
"case else statement": {
"prefix": "rb.case.else",
"body": [
"case $1",
"when $2",
" $3",
"else",
" $4",
"end"
]
},
"case when statement": {
"prefix": "rb.case.when",
"body": [
"case $1",
"when $2",
" $3",
"when $4",
" $5",
"end"
]
},
"case when else statement": {
"prefix": "rb.case.when.else",
"body": [
"case $1",
"when $2",
" $3",
"when $4",
" $5",
"else",
" $6",
"end"
]
},
"while statement": {
"prefix": "rb.while",
"body": [
"while $1",
" $2",
"end"
]
},
"until statement": {
"prefix": "rb.until",
"body": [
"until $1",
" $2",
"end"
]
},
"for statement": {
"prefix": "rb.for",
"body": [
"for $1 in $2",
" $3",
"end"
]
},
"each statement": {
"prefix": "rb.each",
"body": [
"$1.each do |$2|",
" $3",
"end"
]
},
"each with index statement": {
"prefix": "rb.each.index",
"body": [
"$1.each_with_index do |$2, $3|",
" $4",
"end"
]
},
"each with object statement": {
"prefix": "rb.each.object",
"body": [
"$1.each_with_object($2) do |$3, $4|",
" $5",
"end"
]
},
"map statement": {
"prefix": "rb.map",
"body": [
"$1.map do |$2|",
" $3",
"end"
]
},
"map with index statement": {
"prefix": "rb.map.index",
"body": [
"$1.map_with_index do |$2, $3|",
" $4",
"end"
]
},
"map with object statement": {
"prefix": "rb.map.object",
"body": [
"$1.map_with_object($2) do |$3, $4|",
" $5",
"end"
]
},
"inject statement": {
"prefix": "rb.inject",
"body": [
"$1.inject($2) do |$3, $4|",
" $5",
"end"
]
},
"reduce statement": {
"prefix": "rb.reduce",
"body": [
"$1.reduce($2) do |$3, $4|",
" $5",
"end"
]
},
"select statement": {
"prefix": "rb.select",
"body": [
"$1.select do |$2|",
" $3",
"end"
]
},
"reject statement": {
"prefix": "rb.reject",
"body": [
"$1.reject do |$2|",
" $3",
"end"
]
},
"any statement": {
"prefix": "rb.any",
"body": [
"$1.any? do |$2|",
" $3",
"end"
]
},
"all statement": {
"prefix": "rb.all",
"body": [
"$1.all? do |$2|",
" $3",
"end"
]
},
"none statement": {
"prefix": "rb.none",
"body": [
"$1.none? do |$2|",
" $3",
"end"
]
},
"one statement": {
"prefix": "rb.one",
"body": [
"$1.one? do |$2|",
" $3",
"end"
]
},
"min statement": {
"prefix": "rb.min",
"body": [
"$1.min do |$2, $3|",
" $4",
"end"
]
},
"max statement": {
"prefix": "rb.max",
"body": [
"$1.max do |$2, $3|",
" $4",
"end"
]
},
"min by statement": {
"prefix": "rb.min.by",
"body": [
"$1.min_by do |$2|",
" $3",
"end"
]
},
"max by statement": {
"prefix": "rb.max.by",
"body": [
"$1.max_by do |$2|",
" $3",
"end"
]
},
"minmax statement": {
"prefix": "rb.minmax",
"body": [
"$1.minmax do |$2, $3|",
" $4",
"end"
]
},
"minmax by statement": {
"prefix": "rb.minmax.by",
"body": [
"$1.minmax_by do |$2|",
" $3",
"end"
]
},
"sort statement": {
"prefix": "rb.sort",
"body": [
"$1.sort do |$2, $3|",
" $4",
"end"
]
},
"sort by statement": {
"prefix": "rb.sort.by",
"body": [
"$1.sort_by do |$2|",
" $3",
"end"
]
},
"sort reverse statement": {
"prefix": "rb.sort.reverse",
"body": [
"$1.sort.reverse do |$2, $3|",
" $4",
"end"
]
},
"sort by reverse statement": {
"prefix": "rb.sort.by.reverse",
"body": [
"$1.sort_by.reverse do |$2|",
" $3",
"end"
]
},
"sort! statement": {
"prefix": "rb.sort!",
"body": [
"$1.sort! do |$2, $3|",
" $4",
"end"
]
},
"sort by! statement": {
"prefix": "rb.sort.by!",
"body": [
"$1.sort_by! do |$2|",
" $3",
"end"
]
},
"sort reverse! statement": {
"prefix": "rb.sort.reverse!",
"body": [
"$1.sort.reverse! do |$2, $3|",
" $4",
"end"
]
},
"sort by reverse! statement": {
"prefix": "rb.sort.by.reverse!",
"body": [
"$1.sort_by.reverse! do |$2|",
" $3",
"end"
]
},
"group by statement": {
"prefix": "rb.group.by",
"body": [
"$1.group_by do |$2|",
" $3",
"end"
]
},
"count statement": {
"prefix": "rb.count",
"body": [
"$1.count do |$2|",
" $3",
"end"
]
},
"count by statement": {
"prefix": "rb.count.by",
"body": [
"$1.count_by do |$2|",
" $3",
"end"
]
},
"each with index statement": {
"prefix": "rb.each.with.index",
"body": [
"$1.each_with_index do |$2, $3|",
" $4",
"end"
]
},
"each slice statement": {
"prefix": "rb.each.slice",
"body": [
"$1.each_slice($2) do |$3|",
" $4",
"end"
]
},
"range statement": {
"prefix": "rb.range",
"body": [
"$1..$2"
]
},
"range exclude end statement": {
"prefix": "rb.range.exclude.end",
"body": [
"$1...$2"
]
},
"range step statement": {
"prefix": "rb.range.step",
"body": [
"$1.step($2, $3) do |$4|",
" $5",
"end"
]
},
"builder pattern example": {
"prefix": "rb.builder.pattern.example",
"description": "Builder class example with initialize, build, and build! methods",
"body": [
"# When you need many permutations of the existing features in a class while creating new objects.",
"class ParserBuilder",
" def self.build($2)",
" case $3",
" when $4 then Thing1Parser.new($2).parse",
" when $6 then Thing2Parser.new($2).parse",
" else",
" raise($8)",
" end",
" end",
"end"
]
},
"builder pattern example with initialize": {
"prefix": "rb.builder.pattern.example.initialize",
"description": "When you need many permutations of the existing features in a class while creating new objects.",
"body": [
"# When you need many permutations of the existing features in a class while creating new objects.",
"class ParserBuilder",
" end",
" def self.build($5)",
" case $6",
" when $7 then $8",
" when $9 then $10",
" else",
" raise($11)",
" end",
" end",
"end"
]
},
"decorator pattern example": {
"prefix": "rb.decorator.pattern.example",
"description": "When you want to make modifications to existing objects of a class rather than a class itself.",
"body": [
"# When you want to make modifications to existing objects of a class rather than a class itself.",
"class ConvertibleDecorator < SimpleDelegator",
" def $2",
" $3",
" end",
" def self.decorate($4)",
" new($5)",
" end",
" private",
" def $6",
" __getobj__",
" end",
"end"
]
},
"form pattern example":{
"prefix": "rb.form.pattern.example",
"description": "You are looking to reuse form validation rules across components.",
"body": [
"# You are looking to reuse form validation rules across components.",
"class $1",
" include ActiveModel::Model",
" include Virtus.model",
"",
" attribute :author, String",
"",
" validates :$3, presence: true",
" validates :$4, presence: true, format: $5",
" validates :$6, presence: true",
" validates :$7, presence: true",
"",
" attr_reader :record",
"",
" def persist",
" @record = id ? $8.find(id) : $9.new",
" if valid?",
" @record.attributes = attributes.except(:id)",
" @record.save!",
" true",
" else",
" false",
" end",
" end",
"end"
]
},
"interactor pattern example": {
"prefix": "rb.interactor.pattern.example",
"description": "When you are trying to break down an extensive process into smaller steps.",
"body": [
"# When you are trying to break down an extensive process into smaller steps.",
"class $1",
" include Interactor",
"",
" def call",
" $2",
" end",
"end",
"class $3",
" include Interactor",
"",
" def call",
" $4",
" end",
"end",
"class $5",
" include Interactor",
"",
" def call",
" $6",
" end",
"end",
"class $7",
" include Interactor::Organizer",
"",
" organize $8, $9, $10",
"end"
]
},
"observer pattern example": {
"prefix": "rb.observer.pattern.example",
"description": "When you want to notify other objects of changes to an object.",
"body": [
"# When you want to notify other objects of changes to an object.",
"require 'observer'",
"class $1",
" include Observable",
"",
"attr_reader :$2",
"def initialize($3)",
" @record = $4",
"end",
"",
" def update_stock(stock)",
" @stock = stock",
" changed",
" notify_observers(self, stock)",
" end",
"end",
"class Notifier",
" def update($8, stock)",
" $9",
" end",
"end"
]
},
"service pattern example": {
"prefix": "rb.service.pattern.example",
"description": "When you want to encapsulate a complex process into a single object.",
"body": [
"# When you want to encapsulate a complex process into a single object.",
"class $1",
" def initialize($2)",
" @$3 = $4",
" end",
"",
" def call",
" $5",
" end",
"",
" private",
"",
" attr_reader :$6",
" def $7",
" $8",
" end",
"end"
]
},
"policy pattern example":{
"prefix": "rb.policy.pattern.example",
"description": "When you have multiple checks in your code and all of them return a boolean value.",
"body": [
"# When you have multiple checks in your code and all of them return a boolean value.",
"class PostPolicy",
" def initialize(post)",
" @post = post",
" end",
"",
" def update?",
" $1",
" end",
"",
" def destroy?",
" $2",
" end",
"end",
"",
"class PostHandler",
" def uploadPost(post)",
" policy = PostPolicy.new(post)",
"",
" if (policy.update? && policy.destroy?)",
" # save to database here",
" else",
" puts \"error occurred\"",
" end",
" end",
"end"
]
},
"presenter pattern example": {
"prefix": "rb.presenter.pattern.example",
"description": "When you want to encapsulate the logic of presenting data to the view.",
"body": [
"# When you want to encapsulate the logic of presenting data to the view.",
"class PostPresenter",
" def initialize(post)",
" @post = post",
" end",
"",
" def title",
" @post.image? ? $1 : $2",
" end",
"end"
]
},
"query pattern example": {
"prefix": "rb.query.pattern.example",
"description": "When you want to encapsulate the logic of fetching data from the database.",
"body": [
"# When you want to encapsulate the logic of fetching data from the database.",
"class PopularPostsQuery",
" def call(relation)",
" relation.where($1).order($2)",
" end",
"end"
]
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment