Class: Mattock::Configurable::FieldProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/mattock/configurable/field-processor.rb

Direct Known Subclasses

SettingsCopier, SettingsProxier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ FieldProcessor

Returns a new instance of FieldProcessor.



4
5
6
7
# File 'lib/mattock/configurable/field-processor.rb', line 4

def initialize(source)
  @source = source
  @field_names = filter(source.class.field_names)
end

Instance Attribute Details

#field_namesObject

Returns the value of attribute field_names.



8
9
10
# File 'lib/mattock/configurable/field-processor.rb', line 8

def field_names
  @field_names
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/mattock/configurable/field-processor.rb', line 9

def source
  @source
end

Instance Method Details

#filter(field_names) ⇒ Object



15
16
17
18
19
# File 'lib/mattock/configurable/field-processor.rb', line 15

def filter(field_names)
  field_names.find_all do |name|
    source.class.(name).is?(filter_attribute)
  end
end

#filter_attributeObject

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/mattock/configurable/field-processor.rb', line 11

def filter_attribute
  raise NotImplementedError
end

#to(target) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/mattock/configurable/field-processor.rb', line 25

def to(target)
  field_names.each do |name|
    field = source.class.(name)
    next unless target.respond_to?(field.writer_method)
    target.__send__(field.writer_method, value(field))
  end
end

#value(field) ⇒ Object



21
22
23
# File 'lib/mattock/configurable/field-processor.rb', line 21

def value(field)
  source.__send__(field.reader_method)
end