Skip to content

sveredyuk/expositor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expositor

Description

Gem allow you create class methods which delegate it to objects

Installation & Setup

In console:

gem install expositor

And in your ruby file:

require 'expositor'

Example

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

Much more pretty if...

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?'

How to use

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

TODO

  • Some improvements ? =)

About

Simple method exposition to it Class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages