Sha256: 73ee5161c8c67817769882ec06bdcf3c762d1972032c01300ad3c3aaf91070d9

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Mattock
  module Configurable
    class FieldProcessor
      def initialize(source)
        @source = source
        @field_names = filter(source.class.field_names)
      end
      attr_accessor :field_names
      attr_reader :source

      def filter_attribute
        raise NotImplementedError
      end

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

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

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

    class SettingsCopier < FieldProcessor
      def filter_attribute
        :copiable
      end

      def value(field)
        field.immediate_value_on(source)
      end
    end

    class SettingsProxier < FieldProcessor
      def filter_attribute
        :proxiable
      end

      def value(field)
        ProxyValue.new(source, field)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mattock-0.5.3 lib/mattock/configurable/field-processor.rb
mattock-0.5.2 lib/mattock/configurable/field-processor.rb
mattock-0.5.0 lib/mattock/configurable/field-processor.rb