Skip to content

Commit

Permalink
change remaining #lr_* methods to #lazy_* w/ deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Dec 9, 2018
1 parent 94af6f6 commit 29ec052
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 33 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ Earlier implementations used a custom `lr_attr_accessor` method, however this ha

See @dbrady's `scoped_attr_accessor` gem for more info on the `scoped_attr_*` methods.

Validate presence of attributes with `lr_validates` like you would with ActiveRecord. Failed validations will return false and the ID will not be incremented. More validation options coming in the future.
Validate presence of attributes with `lazy_validates` like you would with ActiveRecord. Failed validations will return false and the ID will not be incremented. More validation options coming in the future.

```ruby
class Thing < LazyRecord::Base
attr_accessor :stuff, :junk
lr_validates :stuff, presence: true
lazy_validates :stuff, presence: true
end

thing = Thing.new junk: 'junk'
Expand All @@ -100,16 +100,16 @@ stuff must be given
#<Thing stuff: nil, junk: "junk">
# => false
```
Use `lr_has_many` to set up associated collections of another class. `lr_belongs_to` will be added in a future update.
Use `lazy_has_many` to set up associated collections of another class. `lazy_belongs_to` will be added in a future update.

```ruby
class Whatever < LazyRecord::Base
end

class Thing < LazyRecord::Base
attr_accessor :stuff, :junk
lr_validates :stuff, presence: true
lr_has_many :whatevers
lazy_validates :stuff, presence: true
lazy_has_many :whatevers
end

whatever = Whatever.new
Expand All @@ -125,19 +125,19 @@ thing.whatevers
# => #<WhateverRelation [#<Whatever>]>
```

Use `lr_scope` and `#where` to create class scope methods and query objects.
Use `lazy_scope` and `#where` to create class scope methods and query objects.

```ruby
class Whatever < LazyRecord::Base
attr_accessor :party_value, :sleepy_value
lr_scope :big_party, -> { where { |w| w.party_value > 10 } }
lr_scope :low_sleepy, -> { where { |w| w.sleepy_value < 10 } }
lazy_scope :big_party, -> { where { |w| w.party_value > 10 } }
lazy_scope :low_sleepy, -> { where { |w| w.sleepy_value < 10 } }
end

class Thing < LazyRecord::Base
lr_attr_accessor :stuff, :junk
lr_validates :stuff, presence: true
lr_has_many :whatevers
attr_accessor :stuff, :junk
lazy_validates :stuff, presence: true
lazy_has_many :whatevers
end

Whatever.new party_value: 12, sleepy_value: 12
Expand Down Expand Up @@ -184,19 +184,19 @@ Whatever.where party_value: -> { num * 2 }
```
Use `lazy_method` for an alternative API for defining short instance methods using lambda syntax.

`lazy_method` and `lr_scope` work identically except the former is for instance methods and evaluates `self` in the instance scope, while the latter defines class methods and `self` is evaluated in the class scope.
`lazy_method` and `lazy_scope` work identically except the former is for instance methods and evaluates `self` in the instance scope, while the latter defines class methods and `self` is evaluated in the class scope.

```ruby
class Whatever < LazyRecord::Base
attr_accessor :party_value, :sleepy_value, :right
lr_scope :big_party, -> { where { |w| w.party_value > 10 } }
lr_scope :low_sleepy, -> { where { |w| w.sleepy_value < 10 } }
lazy_scope :big_party, -> { where { |w| w.party_value > 10 } }
lazy_scope :low_sleepy, -> { where { |w| w.sleepy_value < 10 } }
end

class Thing < LazyRecord::Base
attr_accessor :stuff, :junk
lr_validates :stuff, presence: true
lr_has_many :whatevers
lazy_validates :stuff, presence: true
lazy_has_many :whatevers
lazy_method :speak, -> (string) { puts string }
lazy_method :what_am_i, -> { "I'm a #{self.class}" }
end
Expand Down
6 changes: 3 additions & 3 deletions example/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Example class
class Person < LazyRecord::Base
attr_accessor :name, :age, :haircut
lr_has_many :dogs
lr_has_many :kitties, class_name: 'Cat'
lazy_has_many :dogs
lazy_has_many :kitties, class_name: 'Cat'
lazy_has_one :friend
lr_accepts_nested_attributes_for :dogs, :kitties
lazy_accepts_nested_attributes_for :dogs, :kitties

lr_scope :new_with_dog, lambda { |opts = {}|
dog = opts.fetch(:dog) { {} }
Expand Down
8 changes: 8 additions & 0 deletions lib/lazy_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def lazy_has_one(*args)
mod.module_eval { add_has_one_methods(args) }
end

def lr_has_one(*args) # Will be removed in version 1.0.0
puts 'Using `.lr_has_one` is deprecated. Use '\
'`lazy_has_one` instead. `.lr_has_one` will be removed in version 1.0.0'
lazy_has_one(*args)
end

undef_method(:lr_has_one) if LazyRecord::VERSION >= '1.0.0'

def add_has_one_methods(args)
define_has_one_associations(*args)
define_has_one_associations_to_s
Expand Down
21 changes: 19 additions & 2 deletions lib/lazy_record/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ def _define_collections
define_method(:collections) { collections }
end

def lr_has_many(*collections)
def lazy_has_many(*collections)
include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
mod.extend(Collections)
mod.module_eval { _add_collection_methods(*collections) }
end

def lr_has_many(*collections) # Will be removed in version 1.0.0
puts 'Using `.lr_has_many` is deprecated. Use '\
'`lazy_has_many` instead. `.lr_has_many` will be removed in version 1.0.0'
lazy_has_many(*collections)
end

undef_method(:lr_has_many) if LazyRecord::VERSION >= '1.0.0'

def _add_collection_methods(*collections)
_add_to_collections(*collections)
_define_collections
Expand All @@ -92,7 +100,7 @@ def #{collection}_attributes=(collection_attributes)
RUBY
end

def lr_accepts_nested_attributes_for(*collections)
def lazy_accepts_nested_attributes_for(*collections)
include mod = get_or_set_mod(COLLECTION_MODULE_NAME)
mod.extend(Collections)
mod.module_eval do
Expand All @@ -104,6 +112,15 @@ def lr_accepts_nested_attributes_for(*collections)
end
end

def lr_accepts_nested_attributes_for(*collections) # Will be removed in version 1.0.0
puts 'Using `.lr_accepts_nested_attributes_for` is deprecated. Use '\
'`lazy_accepts_nested_attributes_for` instead. `.lr_accepts_nested_attributes_for` '\
'will be removed in version 1.0.0'
lazy_accepts_nested_attributes_for(*collections)
end

undef_method(:lr_accepts_nested_attributes_for) if LazyRecord::VERSION >= '1.0.0'

def _no_collection_error(collection)
klass = collection.to_s.classify
klass = _collections.find { |_col, opt| opt[:class_name] == klass }.first
Expand Down
8 changes: 8 additions & 0 deletions lib/lazy_record/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ def lazy_method(method_name, method)

include mod unless include?(mod)
end

def lr_method(method_name, method) # Will be removed in version 1.0.0
puts 'Using `.lr_method` is deprecated. Use '\
'`lazy_method` instead. `.lr_method` will be removed in version 1.0.0'
lazy_method(method_name, method)
end

undef_method(:lr_method) if LazyRecord::VERSION >= '1.0.0'
end
end
10 changes: 9 additions & 1 deletion lib/lazy_record/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ module LazyRecord
module Scopes
SCOPE_MODULE_NAME = :ScopeMethods

def lr_scope(method_name, lambda)
def lazy_scope(method_name, lambda)
extend mod = get_or_set_mod(SCOPE_MODULE_NAME)

mod.module_eval do
define_method(method_name, &lambda)
end
end

def lr_scope(method_name, lambda) # Will be removed in version 1.0.0
puts 'Using `.lr_scope` is deprecated. Use '\
'`lazy_scope` instead. `.lr_scope` will be removed in version 1.0.0'
lazy_scope(method_name, lambda)
end

undef_method(:lr_scope) if LazyRecord::VERSION >= '1.0.0'
end
end
10 changes: 9 additions & 1 deletion lib/lazy_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def define_validation
end
end

def lr_validates(*args)
def lazy_validates(*args)
include mod = get_or_set_mod(VALIDATIONS_MODULE_NAME)
mod.extend(Validations)
opts = args.extract_options!
Expand All @@ -28,5 +28,13 @@ def lr_validates(*args)
define_validation
end
end

def lr_validates(*args) # Will be removed in version 1.0.0
puts 'Using `.lr_validates` is deprecated. Use '\
'`lazy_validates` instead. `.lr_validates` will be removed in version 1.0.0'
lazy_validates(*args)
end

undef_method(:lr_validates) if LazyRecord::VERSION >= '1.0.0'
end
end
2 changes: 1 addition & 1 deletion lib/lazy_record/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module LazyRecord
VERSION = '0.8'
VERSION = '0.8.0'
end
8 changes: 4 additions & 4 deletions spec/lazy_record/collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
describe 'Collections' do
it_can_include_and_inherit 'Parent', 'Child', 'Sibling' do
Parent.class_eval do
lr_has_many :children
lr_has_many :brothers, class_name: 'Sibling'
lr_accepts_nested_attributes_for :children, :brothers
lazy_has_many :children
lazy_has_many :brothers, class_name: 'Sibling'
lazy_accepts_nested_attributes_for :children, :brothers
end

Child.class_eval { attr_accessor :age }
Expand Down Expand Up @@ -90,7 +90,7 @@
end

it 'does not allow accepting nested attributes unless it has the collection' do
block = -> { Parent.class_eval { lr_accepts_nested_attributes_for :siblings } }
block = -> { Parent.class_eval { lazy_accepts_nested_attributes_for :siblings } }

expect(&block).to raise_error(ArgumentError, "Parent::Collections doesn't have a collection of siblings. Did you mean brothers?")
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lazy_record/scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
it_can_include_and_inherit 'ScopesSpec' do
ScopesSpec.class_eval do
attr_accessor :attr_one, :attr_two
lr_scope :attr_one_eql_one, -> { where attr_one: 1 }
lr_scope :attr_two_eql_two, -> { where attr_two: 2 }
lr_scope :attr_one_eql, ->(val) { where attr_one: val }
lr_scope :attr_two_eql, ->(val) { where attr_two: val }
lazy_scope :attr_one_eql_one, -> { where attr_one: 1 }
lazy_scope :attr_two_eql_two, -> { where attr_two: 2 }
lazy_scope :attr_one_eql, ->(val) { where attr_one: val }
lazy_scope :attr_two_eql, ->(val) { where attr_two: val }
end

let!(:one) { ScopesSpec.new(attr_one: 1, attr_two: 1) }
Expand Down
2 changes: 1 addition & 1 deletion spec/lazy_record/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
it_can_include_and_inherit 'ValidationSpec1' do
ValidationSpec1.class_eval do
attr_accessor :name, :age
lr_validates :name, presence: true
lazy_validates :name, presence: true
end

before :each do
Expand Down

0 comments on commit 29ec052

Please sign in to comment.