Sha256: 6cd2859f2415105004a941bc6e59ef5951e695c7b9afe5b00de5b626321b0189

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module StateFu
  module Persistence
    class Attribute < StateFu::Persistence::Base

      def self.prepare_field( klass, field_name )
        # ensure getter exists
        unless klass.instance_methods.map(&:to_sym).include?( field_name.to_sym )
          Logger.debug "Adding attr_reader :#{field_name} for #{klass}"
          _field_name = field_name
          klass.class_eval do
            private
            attr_reader _field_name
          end
        end

        # ensure setter exists
        unless klass.instance_methods.map(&:to_sym).include?( :"#{field_name}=" )
          Logger.debug "Adding attr_writer :#{field_name}= for #{klass}"
          _field_name = field_name
          klass.class_eval do
            private
            attr_writer _field_name
          end
        end
      end

      private

      # Read / write our strings to a plain old instance variable
      # Define it if it doesn't exist the first time we go to read it

      def read_attribute
        string = object.send( field_name )
        Logger.debug "Read attribute #{field_name}, got #{string.inspect} for #{object.inspect}"
        string
      end

      def write_attribute( string_value )
        writer_method = "#{field_name}="
        Logger.debug "Writing attribute #{field_name} -> #{string_value.inspect} for #{object.inspect}"
        object.send( writer_method, string_value )
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
davidlee-state-fu-0.3.1 lib/state_fu/persistence/attribute.rb