No commit activity in last 3 years
No release in over 3 years
Allows you to define lazy evaluated, constant-like, instances of your models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.8.4
~> 3.12

Runtime

 Project Readme

lazy_model_const¶ ↑

A convenient variant of lazy_const with an eye towards ActiveRecord models.

Installation¶ ↑

gem 'lazy_model_const'

Example¶ ↑

Here’s an example using Rails. Really, any class that has a class method find_or_create_by_name can be used. If you’re not using ActiveRecord, you probably want to use the lazy_const gem.

app/model/group.rb:

class Group < ActiveRecord::Base
  extend LazyConst
  lazy_const :EVERYONE, name: 'Everyone'
end

app/model/user.rb:

class User < ActiveRecord::Base
  has_many :groups
  after_create :add_to_everyone
  def add_to_everyone
    groups << Group.EVERYONE
  end
end

RSpec isn’t required, but if you happen to use it you can use the helper.

spec/spec_helper.rb:

require 'lazy_const/spec'

spec/model/group_spec.rb:

describe Group do
  defines_model_const :EVERYONE
  # ...
end

Contributing to lazy_model_const¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2013 Ryan Graham. See LICENSE.txt for further details. (TL;DR: MIT license)