Sha256: 97e79bae43c2344eb42609658b8e5c7ab0a2182a7f3a35f33b23a3a41414b1b3

Contents?: true

Size: 719 Bytes

Versions: 4

Compression:

Stored size: 719 Bytes

Contents

module ClassSupportMixin
  def self.included(base)
    base.send(:include, InstanceMethods)
    base.extend ClassMethods
  end
  
  module ClassMethods
    def set_attributes(attrs)
      attr_accessor *attrs.keys
      define_method(:default_attributes) { attrs }
    end
  end
  
  module InstanceMethods
    def initialize(options={})
      init = if self.respond_to?(:default_attributes)
        self.default_attributes.merge(options)
      else
        options
      end
      init.each { |k, v| self.send("#{k}=", v) if self.respond_to?(k) }
    end
    
    def attributes
      attrs = {}
      self.default_attributes.keys.each {|k| attrs[k] = self.send(k) if self.respond_to?(k)}
      attrs
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
aninipot-0.0.3 lib/aninipot/support.rb
aninipot-0.0.2 lib/aninipot/support.rb
smart-mobile-0.0.1 lib/smart/support.rb
aninipot-0.0.1 lib/aninipot/support.rb