Skip to content

Instantly share code, notes, and snippets.

@eprothro
eprothro / gist:de0cb76631ccfb40b4a9
Last active August 29, 2015 14:21
Simple Object Oriented JS Example

Object Oriented Javascript Example

Consider a simple HTML element that we want to expand and contract by tapping a different target element.

<a href='expandable-element-1' data-expander>
  <!-- some link markup styled using an `expanded` class -->
</a>
@eprothro
eprothro / gist:80ab5e6ffa1d36c06951
Last active April 21, 2017 23:04
Our Daily Financial Tips

The following items have made the biggest difference in our lives, with respect to day-to-day finances. Some are obvious, some maybe not so! In order of impact:

1. Make a spending plan

Budgeting used to feel like a chore that created feelings of "lack". Over time, we've learned how to view budgeting as a reflection of how our family wants to be spending money, rather than how we're limited in our spending. That, in addition to the rest of these items, helps us feel in the driver's seat financially, with flexibility and joy. The keys here for us here are shown in this spreadsheet and are, namely:

  1. Try to think of everything!
  • We buy new tires every 3 years. We used to not budget that and get stressed when it was time to buy tires. Now, we accrue $300 every year and when it's time to buy tires -- great, no stress!
  • An incomplete spending plan is death by 1000 paper cuts. 20 in tolls, 10 fo

The problem

Exhibit A

class Record < ActiveRecord::Base
  validates_uniqueness_of :name
end

At some point, we realize we're performing an extra query to check uniqueness every time we validate the object.

Deadlock with Spring and cassandra_migrations

=> Rack::Timeout set to 1800 in development
Loading development environment (Rails 4.2.4)
[1] pry(main)> CassandraMigrations::Cassandra.client
=> #<Client:0x007ff17f6755e8
 @cluster=#<Cassandra::Cluster:0x3ff8bf4fc660>,
 @session=#<Cassandra::Session:0x3ff8bfb42574>,
 @sessions={:default=&gt;#, "timebomb_api_development"=&gt;#}&gt;

It can be difficult to test the different scenarious for a module intended for use by including in another class.

If the class is statically defined in the spec, any later definitions extend, not replace, the first definition - which can cause test issues and breaks isolation between examples.

RSpec.describe SomeModule do
  class SomeIncluder
    include SomeModule
  end
 
irb(main):001:0> module A
irb(main):002:1>   def sound_off
irb(main):003:2>     puts 'module a'
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0>
irb(main):007:0* module B
irb(main):008:1>   def sound_off
module ErrorsWithTypesExtensions
  attr_reader :types

  # Pass in the instance of the object that is using the errors object.
  #
  #   class Person
  #     def initialize
  #       @errors = ActiveModel::Errors.new(self)
  #     end

I love the concept and usability of an ActiveModel::Errors paradigm. However, I and others often find it frustrating to work with the implementation.

I'd love to see a proper ActiveModel::Error object, where the errors attribute of a model including ActiveModel::Errors was an Enumerator of these objects.

There are various implied benefits and added flexibility that could be introduced in a backwards compatible way later, and immedate benefits.

Immediate Benefits

The error type could be retained

# Serializing 1 string attribute, 1 overriden uuid.to_s attribute,
# and 1 empty array, 30 times.
# bmbm:
# user system total real
# serializer 0.010000 0.000000 0.010000 ( 0.002572)
# delegator 0.000000 0.000000 0.000000 ( 0.000424)
# ips:
# delegator: 3600.6 i/s
# serializer: 427.6 i/s - 8.42x slower
class Test
  
  def foo(value=default_value)
    value
  end

  def default_value
    'default'
 end