Sha256: e7b758aa048fb4834175eba25bb88e54e050a55e0dc15d42acef3ab5b99a6871

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.info "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.info "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.info "Read attribute #{field_name}, got #{string.inspect} for #{object.inspect}"
        string
      end

      def write_attribute( string_value )
        writer_method = "#{field_name}="
        Logger.info "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.2.0 lib/state_fu/persistence/attribute.rb