ENV Management: .pryrc

Worth doing

  • Low-level metaprogramming
  • Save time
  • The good kind of lazy
def execute_this_script
  set_variables
  define_businesses
  define_carriers
end

def set_variables
  @businesses = Business.all
  @carriers = Carrier.all
end

def define_businesses
  @businesses.each do |b|
    define_method b.subdomain do
      return b
    end
  end and nil
end

def define_carriers
  @carriers.each do |c|
    define_method(c.nickname){return c}
  end and nil
end

execute_this_script if defined?(Rails) 
    && Rails.env == "development"

Good Lazy

  • Simplify workflows
  • Low cognitive cost
  • Easily extensible
[1] pry(main)> Business.find(23)
> #<Business id: 23, name: \"Procter & Gamble\",...

vs

[1] pry(main)> pg
> #<Business id: 23, name: \"Procter & Gamble\",...




[1] pry(main)> Carrier.find(834256072)
> #<Carrier id: 834256072, name: \"AT&T United States\",...

vs

[1] pry(main)> att
> #<Carrier id: 834256072, name: \"AT&T United States\",...

Moar

  • Users
  • Roles
  • Whatever, go nuts
def cf
  @cf ||= People.find(2410474)
end

def define_role_scopes
  Role.all.each do |role|
    method_name = role.name.parameterize.underscore

    define_method method_name.pluralize do
      return Person.all
                   .joins(:roles)
                   .where(roles: {name: role.name})
    end

    define_method "#{method_name}!" do
      send(method_name.pluralize).take
    end

    define_method "#{method_name}" do
      return role
    end
  end
end


## Example: 
cf.roles = [enduser, workflow_editor]
cf.save

pryrc (SnT)

By ccflack

pryrc (SnT)

  • 620