Module: Questionable

Defined in:
lib/questionable.rb

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Extend ‘klass` with Questionable. This means nothing more than a user can use both `include` and `extend`

Parameters:

  • klass (Class)


8
9
10
# File 'lib/questionable.rb', line 8

def self.included(klass)
  klass.extend self
end

Instance Method Details

#questionable(*items, &block) ⇒ Object

Add questionable items

Examples:

class Person
  include Questionable

  questionable :name, :age
  questionable :has_password? => :password

  questionable :friends_with_robert? => :friends do |friends|
    friends.include? 'Robert'
  end

  questionable :has_friend? => :friends do |friends, friend|
    friends.include? friend
  end

  def initialize(name, age, password=nil)
    @name = name
    @age = age
    @password = password
    @friends = ['Phil', 'Robert']
  end
end

roy = Person.new 'Roy', 36, 'Sekret!!'
roy.name? #=> true
roy.has_password? #=> true
roy.friends_with_robert? #=> true

dave = Person.new 'Dave', 42
dave.age? #=> true
dave.has_password? #=> false
dave.has_friend? 'Steve' #=> false


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/questionable.rb', line 46

def questionable(*items, &block)
  if items.first.respond_to? :keys
    __create_questionable_method *items[0].shift, &block
  else
    if items.size == 1
      __create_questionable_method items.shift, &block
    else
      items.each { |i| __create_questionable_method i, &block }
    end
  end
end