Sha256: 201754dd3077965f82f48bd6523ea395c3cfbd23726bdf8f1212d39fcf6271e5

Contents?: true

Size: 806 Bytes

Versions: 3

Compression:

Stored size: 806 Bytes

Contents

module AppConfiguration

  module OptionalAttr
  
    module ClassMethods
      
      def defaults
        @defaults ||= {}
      end
      protected :defaults

      def optional_attr(attribute, options = {})
        define_method(attribute) do |value| 
          instance_variable_set("@#{attribute}", value) 
        end

        defaults[attribute] = options[:default] if options[:default]
      end 
      protected :optional_attr
    
    end
    
    module InstanceMethods
      
      def initialize
        self.class.send(:defaults).each do |attribute, value|
          instance_variable_set("@#{attribute}", value)
        end
      end

    end
    
    def self.included(receiver)
      receiver.extend         ClassMethods
      receiver.send :include, InstanceMethods
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
app_configuration-0.0.3 lib/app_configuration/optional_attr.rb
app_configuration-0.0.2 lib/app_configuration/optional_attr.rb
app_configuration-0.0.1 lib/app_configuration/optional_attr.rb