Class: AttrHelper::RequiredAttr

Inherits:
BaseAttr
  • Object
show all
Defined in:
lib/attr_helper/required_attr.rb

Instance Attribute Summary collapse

Attributes inherited from BaseAttr

#default, #key, #name, #serialize

Instance Method Summary collapse

Methods inherited from BaseAttr

#serializable?, #serialized

Constructor Details

#initialize(name, options = {}) ⇒ RequiredAttr

Returns a new instance of RequiredAttr.



6
7
8
9
10
# File 'lib/attr_helper/required_attr.rb', line 6

def initialize(name, options = {})
  super(name, options)
  @if_cond = options[:if]
  @unless_cond = options[:unless]
end

Instance Attribute Details

#if_condObject (readonly)

Returns the value of attribute if_cond.



3
4
5
# File 'lib/attr_helper/required_attr.rb', line 3

def if_cond
  @if_cond
end

#unless_condObject (readonly)

Returns the value of attribute unless_cond.



4
5
6
# File 'lib/attr_helper/required_attr.rb', line 4

def unless_cond
  @unless_cond
end

Instance Method Details

#required?(parent) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/attr_helper/required_attr.rb', line 12

def required?(parent)
  if conditions_empty?
    true
  else
    included?(parent) && !excluded?(parent)
  end
end