Skip to content

Instantly share code, notes, and snippets.

View christopherslee's full-sized avatar

Christopher Lee christopherslee

View GitHub Profile
@christopherslee
christopherslee / vue-sfc.code-snippets
Created April 19, 2019 14:31
vscode macro example
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. 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": {
// "scope": "javascript,typescript",
@christopherslee
christopherslee / descriptors.py
Created October 10, 2014 03:18
metaclass example with self registering descriptors
class Field(object):
def __init__(self, name):
self._name = name
def __get__(self, instance, owner):
return instance._data[self._name]
def __set__(self, instance, value):
instance._data[self._name] = value
@christopherslee
christopherslee / gist:6161539
Created August 6, 2013 02:33
Intro to DSLs - Part 3 Answer
# lib/part_3/advanced_credit_score.rb
require_relative 'advanced_consumer'
class AdvancedCreditScore
def self.simulate(&block)
consumer = AdvancedConsumer.new
# TODO Part 3
# Once again, use instance eval to invoke the block on the consumer
@christopherslee
christopherslee / gist:6153296
Created August 5, 2013 03:34
Intro to DSLs - Part 1 Answer
# lib/part_2/customer.rb
class Customer
attr_accessor :balance
def good_standing?
balance >= 0
end
end
@christopherslee
christopherslee / gist:6153279
Last active December 20, 2015 15:19
Intro to DSLs - Part 2 Answers
# lib/part_2/credit_score.rb
require_relative 'consumer'
module CreditScore
def self.simulate(&block)
consumer = Consumer.new
consumer.instance_eval(&block)
consumer.score
end
end
@christopherslee
christopherslee / gist:5521968
Created May 5, 2013 19:54
Introduction to Metaprogramming: Lesson 4 Regexp
datasource.methods.grep(/^get_(.*)_value/) { Customer.has_field $1 }
@christopherslee
christopherslee / customer.rb
Last active December 17, 2015 00:29
Introduction to Metaprogramming: Lesson 5
class Customer
attr_reader :id, :datasource
def initialize(id, datasource)
@id = id
@datasource = datasource
end
def respond_to?(method)
datasource.respond_to?("get_#{method}_value") || super
@christopherslee
christopherslee / customer.rb
Last active December 17, 2015 00:29
Introduction to Metaprogramming: Lesson 4
class Customer
attr_reader :id, :datasource
def initialize(id, datasource)
@id = id
@datasource = datasource
end
def method_missing(methodname, *args, &block)
super unless @datasource.respond_to? "get_#{methodname}_value"
@christopherslee
christopherslee / customer.rb
Created May 5, 2013 18:50
Introduction to Metaprogramming: Lesson 4
class Customer
attr_reader :id, :datasource
def initialize(id, datasource)
@id = id
@datasource = datasource
@datasource.methods.grep(/^get_(.*)_value/) { Customer.has_field $1 }
end
def self.has_field(fieldname)
@christopherslee
christopherslee / customer.rb
Last active December 17, 2015 00:29
Introduction to Metaprogramming: Lesson 3
class Customer
attr_reader :id, :datasource
def initialize(id, datasource)
@id = id
@datasource = datasource
end
def self.has_field(fieldname)
define_method fieldname do