This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WithBlockVar | |
attr_accessor :z | |
def initialize(x, &blk) | |
@x = x | |
instance_eval(&blk) if block_given? | |
end | |
end | |
# Using block variable | |
a = WithBlockVar.new("A") do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An attempt to codify how we're using Service Objects now | |
# | |
# Most follow this pattern: | |
# | |
# class SomeService | |
# | |
# def self.call(a, b, &block) | |
# new(a, b).call(&block) | |
# end | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Entity | |
include Virtus.module | |
attribute :id, Integer | |
def persisted? | |
!id.nil? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeTypes | |
@all = [ | |
["TypeA", "Type A"], | |
["Type B", "Type B"], | |
] | |
Type = Struct.new(:type, :name, :slug, :sym) do | |
alias_method :to_ary, :to_a | |
# allows Type to be used for ifnone option in .find |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "mechanize" | |
# we need some methods to clean this up so make it a class | |
class InspectionScraper | |
BASE_URL = "http://eats.washoecounty.us/" | |
FACILITY_COLS = ["link", "name", "score", "facility_type", "address", "inspection_date"] | |
RESULTS_TABLE_ID = "table#ctl00_ContentPlaceHolder1_grdHealth" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "virtus" | |
class Teacher | |
include Virtus::ValueObject | |
attribute :id, String | |
def self.inherited(descendant) | |
super |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example: | |
# | |
# class EditForm < FormModel | |
# model :a | |
# | |
# attribute :title, String | |
# | |
# validates :title, presence: true | |
# end | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define | |
class Object | |
def cascade(method_name, *args, &block) | |
self.send(method_name, *args, &block) | |
self | |
end | |
# catch the cascade | |
def method_missing(method_name, *args, &block) | |
sig = /^__/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enuma | |
def enumerated(type_name, *members, &block) | |
@enums = [] | |
type_class = const_set(type_name, Enuma::Type.new(*members)) | |
instance_eval(&block) | |
finalize(@enums, type_class) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def initialize | |
@lambda = ->(x) { puts "lambda: #{x}" } | |
@proc = Proc.new { |x| puts "proc: #{x}" } | |
@method = method(:_method) | |
end | |
def lambda(x) | |
@lambda[x] | |
end |