Skip to content

Instantly share code, notes, and snippets.

class Hook
attr_reader :payload
def initialize(payload)
@payload = payload
end
def operation_attributes
send(:"#{action}_operation_attributes")
end
require 'ostruct'
# ClosedStruct is similar to OpenStruct, except:
#
# * Immutable. Cannot be changed after insantiation.
# * Raises NoMethodError on undefined attributes instead of returning nil.
#
# Example:
# struct = ClosedStruct.new do |attributes|
# attributes.name1 = 'value1'
def hash2openstruct(attributes)
attributes = attributes.reduce({}) do |attributes, (name, value)|
if value.is_a?(Hash)
value = hash2openstruct(value)
elsif value.is_a?(Array)
value = value.map { |v| hash2openstruct(v) }
end
attributes[name] = value
attributes
end
require 'heroku-api'
require 'tmpdir'
branch = ''
git_url = ''
heroku_host = ''
heroku_key = ''
app_name = "t8y-#{branch.downcase.gsub('_', '-').gsub(/[^a-z0-9\-]/, '')}"
heroku = Heroku::API.new(api_key: heroku_key)
@tatey
tatey / 1_form.rb
Last active December 13, 2015 22:09
class RetrieverForm
include ActiveModel::Conversion
include ActiveModel::Validations
validate :existance_of_email
def initialize(attributes = {})
@email = attributes[:email]
end
class RubyExample
CONSTANT = /^[0-9]+ regex awesomes$/
attr_reader :colorscheme
def initialize(attributes = {})
@colorscheme = attributes[:colorscheme]
end
def self.values
@tatey
tatey / gist:4592323
Created January 22, 2013 05:26
Typically we'd use hashes for this sort of things, but unlike hashes, going ChoiceTypes.doesnt_exist will raise an exception instead of returning nil.
# Known poll types. Unknown types will raise
# an exception.
#
# Poll::ChoiceTypes.time # => 'Choice::Time'
#
# @return [Struct]
ChoiceTypes = Struct.new(:time, :place, :text).new.tap do |types|
types.time = 'Choice::Time'
types.place = 'Choice::Place'
types.text = 'Choice::Text'
@tatey
tatey / new.html.erb
Created January 13, 2013 04:56
Rails ERB template for http://jsfiddle.net/tatejohnson/vLhNb/. Adding and removing children to a Rails nested form with AngularJS.
<h1>New Plan</h1>
<div ng-app>
<%= form_for @plan do |form| %>
<div ng-controller="PollCtrl" ng-init="polls = <%= @plan.polls.to_json %>">
<div ng-repeat="poll in polls">
<%= form.fields_for :polls, Poll.new, child_index: '{{$index}}' do |poll_form| %>
<%= poll_form.text_field :title, id: 'plan_poll_{{$index}}', value: '{{poll.title}}' %>
<% end %>
<a href="#" ng-click="remove($index)" ng-show="isRemovable()">Remove</a>
</div>
@tatey
tatey / gist:4458488
Last active December 10, 2015 16:08
Commit message when I upgraded rails from 2.3.8 to 2.3.9.
Upgrade rails to 2.3.9.
2.3.9 fixes a bug that we had come to depend on
(https://github.com/rails/rails/pull/7661). It's best described with an
example
2.3.8
p = Plan.new
p.member = Member.first
module Countable
def self.included do
define_method "#{self.name}s" do
# ...
end
end
def self.name
self.class.to_s.sub('FeedItem', '').downcase
end