Skip to content

Instantly share code, notes, and snippets.

View giovannibenussi's full-sized avatar

Giovanni giovannibenussi

View GitHub Profile
class Array
# Returns the flatten version of the array
# example: [[1,2,[3]],4] -> [1, 2, 3, 4]
def flatten
out = []
self.each do | e |
if e.is_a? Array
out += e.flatten
else
out.push(e)
@giovannibenussi
giovannibenussi / 0_reuse_code.js
Created December 27, 2016 02:39
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
#################### app/models/task.rb
class Task < ApplicationRecord
belongs_to :user
belongs_to :lead, required: false
belongs_to :creator, class_name: "User", foreign_key: "creator_id"
belongs_to :responsible, class_name: "User", foreign_key: "responsible_id"
update_index 'leads#lead', :lead
import math
a = float(input("Ingrese el valor de a: "))
b = float(input("Ingrese el valor de b: "))
c = float(input("Ingrese el valor de c: "))
print("Ecuación: " + str(a) + " * x * x + " + str(b) + " * x + " + str(c))
print("a: " + str(a))
print("b: " + str(b))
def index
items_with_images = news_items.each do |item|
item[:image] = UserScope::FetchNewsItem.call(id: params[:id]).news_item.image
end
respond_with items_with_images
end
resolve lambda { |_, args, _|
unless args[:name].present?
raise GraphQL::ExecutionError, 'You must provide a value for name'
end
unless args[:address].present?
raise GraphQL::ExecutionError, 'You must provide a value for address'
end
Model.find_by(address: args[:address], name: args[:name])
}
resolve lambda { |_, args, _|
Validators::Model.validate!(args)
Model.find_by(address: args[:address], name: args[:name])
}
module Validators
class Model
def self.validate!(args)
unless args[:name].present?
raise GraphQL::ExecutionError, 'You must provide a value for name'
end
unless args[:address].present?
raise GraphQL::ExecutionError, 'You must provide a value for address'
end
end
module Validators
class GraphqlValidator
def self.validate_presence(attribute, value)
return if value.present?
raise GraphQL::ExecutionError, “You must provide a value for #{attribute}”
end
end
end
module Validators
class Model
def self.validate!(args)
validate_presence('name', args[:name])
validate_presence('address', args[:address])
end
end
end