Class: Enumerize::Predicates::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/enumerize/predicates.rb

Instance Method Summary collapse

Constructor Details

#initialize(attr, options) ⇒ Builder

Returns a new instance of Builder.


48
49
50
51
# File 'lib/enumerize/predicates.rb', line 48

def initialize(attr, options)
  @attr    = attr
  @options = options.is_a?(Hash) ? options : {}
end

Instance Method Details

#build(klass) ⇒ Object


71
72
73
# File 'lib/enumerize/predicates.rb', line 71

def build(klass)
  klass.delegate(*names, to: @attr.name, prefix: @options[:prefix], allow_nil: true)
end

#namesObject


67
68
69
# File 'lib/enumerize/predicates.rb', line 67

def names
  values.map { |v| "#{v.tr('-', '_')}?" }
end

#valuesObject


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/enumerize/predicates.rb', line 53

def values
  values = @attr.values

  if @options[:only]
    values &= Array(@options[:only]).map(&:to_s)
  end

  if @options[:except]
    values -= Array(@options[:except]).map(&:to_s)
  end

  values
end