Skip to content

Instantly share code, notes, and snippets.

@azi
azi / machine.js
Created November 16, 2020 06:09
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
{
"suggest.noselect": false,
"coc.preferences.formatOnSaveFiletypes": [
"javascript",
"typescript",
"typescriptreact",
"json",
"javascriptreact",
"typescript.tsx",
"graphql"
@azi
azi / kk
Last active July 21, 2018 11:48
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu1BRFhnEOYgYjY3QEtJUVdaudwNiH2fpFuAgxTzL8XQoBA1xbuJhsMREpPNkcdz+f2RIObNFrMhUmOb7Nt9aScOFL3iLp36lRxgSfBeuYkzM8INDhxqhAYZFMDCsnLFbopJ3KNv0jCsf54TahGKVgMBv8MCUwSi/TFCv5tOvmFwJxKOohbQNKqB9uC0WYRfomhxFOjuRJRhWIuRx8BhP0Sr694nUM+YeRtv4xU+VSYV7rEHCr2Tlu4IvkGUL2WNdxzthSfPzp3469/TYAct56FIhOTu7dUvK3cTYy8xR6eWAwnylb31iLOg3IJwuNuHs6G/2aQhfE9NuzWu7bG3DD azi@Azi-MBP.local
@azi
azi / example1.rb
Last active August 29, 2015 14:25
DSL_style_class_macro
經常會用到的 has_many, validate_presence_of,attr_reader...etc
- ClassMethods 和 InstanceMethods
class Contest < ActivieRecord::Base
has_many :votes
has_many :entites
validate_presence_of :title
end
class Book
@azi
azi / example1.rb
Last active August 29, 2015 14:25
DSL_style_fluent
in Rails2.X
Person.find(:all, :conditions => [ "category IN (?) and name = ?", categories, parmas[:name]], :limit => 50, :order => "created_at DESC")
Person.where(category: categories, name: params[:name])
.order('created_at DESC')
.limit(50)
@azi
azi / example1.rb
Last active August 29, 2015 14:25
DSL_style_block
something *args do |*items|
...
end
module CheckedAttributes
def attr_checked(attr, &validate)
define_method "#{attr}" do
eval("@attr")
end
define_method "#{attr}=" do |v|
if validate
raise 'Invalid attribute' unless validate.call(v)
class PatentJob
def run
update_patents(parse(download_file))
end
def download_file
temp = Tempfile.new('patents')
tempname = temp.path
temp.close
Net::FTP.open(config.host, config.login, config.password) do |ftp|
class Warehouse
attr_accessor :amount
attr_accessor :sale_cnt
attr_accessor :item_name
def initialize
@item_name = "item_#{rand(100)}"
@amount = rand(100)
@sale_cnt = rand(10)
@azi
azi / gist:c42e2606e943749e7a77
Last active August 29, 2015 14:20
lambda vs method
def ff
a = 1
def my_method
puts "My Method: #{local_variables.inspect}"
end
my_method
lab = lambda do
puts "Lambda #{local_variables.inspect}"