Project

lazy_lazer

0.0
No commit activity in last 3 years
No release in over 3 years
Create lazily loadable models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 2.14
~> 0.11
~> 12.1
~> 3.6
~> 0.50
 Project Readme

require 'lazy_lazer'

class User
  include LazyLazer

  property :name, :required
  property :email, default: 'unknown@example.com'
  property :created_at, from: :creation_time_utc, with: ->(t) { Time.at(t) }
  property :age, with: :to_i
  property :twitter_handle, :nil
  property :favorite_ice_cream

  def say_flavor!
    if exists_locally?(:favorite_ice_cream)
      puts "#{name} currently likes #{favorite_ice_cream}."
    else
      puts "#{name} doesn't have a favorite ice cream flavor yet."
    end
  end

  def try_another_flavor!
    delete_attribute(:favorite_ice_cream)
    not_fully_loaded!
    puts "#{name} just tried #{favorite_ice_cream}. They love it!"
  end

  private

  def lazer_reload
    fully_loaded! # mark model as fully updated
    { favorite_ice_cream: %w[vanilla strawberry chocolate].sample }
  end
end

user = User.new(name: 'Blinky', creation_time_utc: 1500000000, age: '21')

user.name           #=> "Blinky"
user.email          #=> "unknown@example.com"
user.created_at     #=> 2017-07-14 03:40:00 +0100
user.age            #=> 21
user.twitter_handle #=> nil

user.favorite_ice_cream         #=> "chocolate"
user.favorite_ice_cream         #=> "chocolate"
user.reload.favorite_ice_cream  #=> "vanilla"

user.say_flavor!         #=> Blinky currently likes vanilla.
user.try_another_flavor! #=> Blinky just tried strawberry. They love it!

documentation - contributing - mit licensed
created for redd - logo font is zaguatica