Skip to content

Commit

Permalink
change #lr_method to #lazy_method
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Dec 8, 2018
1 parent 84ebcd9 commit 572efa2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ num = 6
Whatever.where party_value: -> { num * 2 }
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>]>
```
Use `lr_method` for an alternative API for defining short instance methods using lambda syntax.
Use `lazy_method` for an alternative API for defining short instance methods using lambda syntax.

`lr_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 `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.

```ruby
class Whatever < LazyRecord::Base
Expand All @@ -197,8 +197,8 @@ class Thing < LazyRecord::Base
attr_accessor :stuff, :junk
lr_validates :stuff, presence: true
lr_has_many :whatevers
lr_method :speak, -> (string) { puts string }
lr_method :what_am_i, -> { "I'm a #{self.class}" }
lazy_method :speak, -> (string) { puts string }
lazy_method :what_am_i, -> { "I'm a #{self.class}" }
end

thing = Thing.new stuff: 'stuff'
Expand Down
8 changes: 4 additions & 4 deletions example/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class Person < LazyRecord::Base
lr_scope :old, -> { where { |obj| obj.age > 30 } }
lr_scope :short_hair, -> { where(haircut: 'short') }

lr_method :speak, ->(phrase) { "#{name}: '#{phrase}'" }
lr_method :add_dog, ->(name) { dogs << Dog.new(name: name) }
lr_method :introduction, -> { puts "Hello, my name is #{name}" }
lr_method :say_hi, -> { "Hi from #{self}" }
lazy_method :speak, ->(phrase) { "#{name}: '#{phrase}'" }
lazy_method :add_dog, ->(name) { dogs << Dog.new(name: name) }
lazy_method :introduction, -> { puts "Hello, my name is #{name}" }
lazy_method :say_hi, -> { "Hi from #{self}" }

lr_validates :name, :age, presence: true

Expand Down
6 changes: 3 additions & 3 deletions lib/lazy_record/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ module LazyRecord
module Methods
METHODS_MODULE_NAME = :DynamicMethods

# Defines an instance method on the calling class. The first agrument
# Defines an instance method on the calling class. The first argument
# is a symbol that names the method. The second argument should be a lambda
# that defines the method execution. The method argument could technically
# be any object that responds to #to_proc and returns a Proc, but lambdas
# with their arity restrictions are preferred.
#
# === Example
# class Thing < LazyRecord::Base
# lr_method :say_hi, -> { "Hi from #{self}" }
# lazy_method :say_hi, -> { "Hi from #{self}" }
# end
#
# thing = Thing.new # => #<Thing>
Expand All @@ -25,7 +25,7 @@ module Methods
# @param method [Proc]
#
# @return [Symbol]
def lr_method(method_name, method)
def lazy_method(method_name, method)
mod = get_or_set_mod(METHODS_MODULE_NAME)

mod.module_eval do
Expand Down
8 changes: 4 additions & 4 deletions spec/lazy_record/methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
describe LazyRecord::Methods do
it_can_include_and_inherit 'MethodsSpec' do
MethodsSpec.class_eval do
lr_method :return_self, -> { self }
lr_method :name, -> { self.class.name }
lr_method :speak, ->(phrase) { "#{name}: #{phrase}" }
lazy_method :return_self, -> { self }
lazy_method :name, -> { self.class.name }
lazy_method :speak, ->(phrase) { "#{name}: #{phrase}" }
end

it 'can call methods defined by #lr_method, properly scoped' do
it 'can call methods defined by #lazy_method, properly scoped' do
obj = MethodsSpec.new
expect(obj.return_self).to be obj
expect(obj.name). to eq 'MethodsSpec'
Expand Down

0 comments on commit 572efa2

Please sign in to comment.