Module: AttrRequired

Defined in:
lib/attr_required.rb

Defined Under Namespace

Modules: ClassMethods Classes: AttrMissing

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


5
6
7
# File 'lib/attr_required.rb', line 5

def self.included(klass)
  klass.send :extend, ClassMethods
end

Instance Method Details

#attr_missingObject


63
64
65
66
67
68
69
70
71
72
# File 'lib/attr_required.rb', line 63

def attr_missing
  required_attributes.select do |key|
    value = send(key)
    if value.respond_to?(:empty?)
      value.empty?
    else
      value.nil?
    end
  end
end

#attr_missing!Object


57
58
59
60
61
# File 'lib/attr_required.rb', line 57

def attr_missing!
  if attr_missing?
    raise AttrMissing.new("'#{attr_missing.join('\', \'')}' required.")
  end
end

#attr_missing?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/attr_required.rb', line 53

def attr_missing?
  !attr_missing.empty?
end

#attr_required?(key) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/attr_required.rb', line 49

def attr_required?(key)
  self.class.attr_required? key
end

#required_attributesObject


45
46
47
# File 'lib/attr_required.rb', line 45

def required_attributes
  self.class.required_attributes
end