Gem allow you create class methods which delegate it to objects
In console:
gem install expositor
And in your ruby file:
require 'expositor'
You initiating Policy that allow you explicit some 'checks' to special policy class
class NamingPolicy
def initialize(name)
@name = name
end
def john?
@name == 'John'
end
def jack?
@name == 'Jack'
end
def james?
@name == 'James'
end
end
And now for get right answer you need do all this:
m = NamingPolicy.new('John')
m.john? #=> true
m.jack? #=> false
NamingPolicy.new('James').james? #=> true
too boring... not ruby-way...
require 'expositor'
class NamingPolicy
extend Expositor
def initialize(name)
@name = name
end
def john?
@name == 'John'
end
def jack?
@name == 'Jack'
end
def james?
@name == 'James'
end
end
And now you available:
NamingPolicy.john?('John') #=> true
NamingPolicy.jack('John') #=> false
NamingPolicy.max?('Max') #=> undefined method `max?'
By default it expose all public method, but you can easy handle this through expose
method:
Only some methods:
class NamingPolicy
extend Expositor
expose only: [:john?, :jack]
#...
NamingPolicy.john?('John') #=> true
NamingPolicy.jack('John') #=> false
NamingPolicy.james?('James') #=> undefined method `james?'
All except:
class NamingPolicy
extend Expositor
expose except: [:john?]
#...
NamingPolicy.john?('John') #=> undefined method `james?'
NamingPolicy.jack('Jack') #=> true
NamingPolicy.james?('John') #=> false
And combie:
class NamingPolicy
extend Expositor
expose only: [:jack, :john], except: [:john?]
#...
NamingPolicy.john?('John') #=> undefined method `john?'
NamingPolicy.jack('Jack') #=> true
NamingPolicy.james?('James') #=> true
- Some improvements ? =)