Sha256: 9001a2fdfcd4b132787b0ed75c39f797d56299f6104218325191f2f643ef1777
Contents?: true
Size: 1.06 KB
Versions: 4
Compression:
Stored size: 1.06 KB
Contents
module Glman module InitRequired class InitializationError < StandardError; end module ClassMethods def attr_required(*attrs) attrs.each do |attr| self.send(:attr_accessor, attr) end @__required_attributes = (@__required_attributes || []) + attrs end end module InstanceMethods def initialize(params={}) params.each do |attr, value| self.send("#{attr}=", value) end if params required_attrs_valid?(params) end private def required_attrs_valid?(params) required_attrs = self.class.instance_variable_get(:@__required_attributes) || [] return if required_attrs.empty? required_attrs.each do |key| sym_key = key.to_sym raise Glman::InitRequired::InitializationError.new("Missing #{sym_key} param during initiaization #{self.class}") unless params.has_key?(sym_key) end end end def self.included(base) base.send(:include, InstanceMethods) base.extend(ClassMethods) end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
glman-0.1.3 | lib/glman/init_required.rb |
glman-0.1.2 | lib/glman/init_required.rb |
glman-0.1.1 | lib/glman/init_required.rb |
glman-0.1.0 | lib/glman/init_required.rb |