Class: Inject::Target

Inherits:
Object show all
Defined in:
lib/attr_inject/target.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, attribute, opts = {}) ⇒ Target

Returns a new instance of Target.



4
5
6
7
8
9
10
# File 'lib/attr_inject/target.rb', line 4

def initialize(klass, attribute, opts={})
  @klass = klass
  @attribute = attribute

  apply_options opts
  add_accessor(attribute, klass)
end

Instance Method Details

#apply(params, target) ⇒ Object



12
13
14
15
16
17
# File 'lib/attr_inject/target.rb', line 12

def apply(params, target)
  validate! params

  set_value(target, default) if !default.nil?
  set_value(target, attribute_value(params)) if params.include?(attribute)
end

#attributeObject



27
28
29
# File 'lib/attr_inject/target.rb', line 27

def attribute
  @attribute
end

#defaultObject



23
24
25
# File 'lib/attr_inject/target.rb', line 23

def default
  options[:default]
end

#klassObject



31
32
33
# File 'lib/attr_inject/target.rb', line 31

def klass
  @klass
end

#optionsObject



35
36
37
# File 'lib/attr_inject/target.rb', line 35

def options
  @options
end

#required?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/attr_inject/target.rb', line 19

def required?
  options[:required] && default.nil?
end