lib/refcode/encodable.rb in refcode-0.1.0 vs lib/refcode/encodable.rb in refcode-0.1.1
- old
+ new
@@ -5,15 +5,16 @@
def self.included base
base.extend ClassMethods
end
def generate_refcode val
- raise 'You must call the has_refcode method in your class definition' unless self.class.refcode_options
+ check_refcode_options!
encoder.encode val
end
def parse_refcode code
+ check_refcode_options!
encoder.decode code
end
module ClassMethods
def has_refcode options
@@ -27,20 +28,25 @@
private
def encoder
Refcode::Encoder.new do |r|
+ check_refcode_options!
r.secret = self.class.refcode_options[:secret]
salt_option = self.class.refcode_options[:salt]
if salt_option.respond_to? :call
- r.salt = salt_option.call
+ r.salt = salt_option.call self
elsif salt_option.is_a? Symbol
r.salt = self.send salt_option
else
r.salt = salt_option
end
raise "Value for salt is nil (#{salt_option} given)" if r.salt.nil?
end
+ end
+
+ def check_refcode_options!
+ raise RefcodeInitException.new('You must call the has_refcode method in your class definition') unless self.class.refcode_options
end
end
end