Commits

Anonymous committed fcd98e2

added example of dynamic attribute with method_missing

Comments (0)

Files changed (3)

RSpec_samples/Gemfile.lock

+GEM
+  remote: http://rubygems.org/
+  specs:
+    diff-lcs (1.1.3)
+    rspec (2.7.0)
+      rspec-core (~> 2.7.0)
+      rspec-expectations (~> 2.7.0)
+      rspec-mocks (~> 2.7.0)
+    rspec-core (2.7.1)
+    rspec-expectations (2.7.0)
+      diff-lcs (~> 1.1.2)
+    rspec-mocks (2.7.0)
+
+PLATFORMS
+  x86-mingw32
+
+DEPENDENCIES
+  rspec

RSpec_samples/spec/user_spec.rb

+require './user'
+
+describe User do
+
+  describe "Doctor" do
+    subject { User.new 'doctor' }
+    it { should be_doctor }
+    it { should_not be_patient }
+  end
+
+  describe "Patient" do
+    subject { User.new 'patient' }
+    it { should be_patient }
+    it { should_not be_doctor }
+  end
+  
+  describe "Nurse" do
+    subject { User.new 'nurse' }
+    it { should be_nurse }
+    it { should_not be_doctor }
+  end
+end

RSpec_samples/user.rb

+class String
+   def ends_with?(suffix)
+     suffix = suffix.to_s
+     self[-suffix.length..-1] == suffix
+   end
+ end
+
+class User
+
+  def initialize(profile)
+    @profile = profile
+  end
+
+#  def doctor?
+#    @profile == 'doctor'
+#  end
+  
+#  def patient?
+#    @profile == 'patient'
+#  end
+
+  def method_missing(method_name, *args, &block)
+    
+    method_name = method_name.to_s
+    if method_name.ends_with?("?")
+      return @profile == method_name.chop
+    end
+    
+    super if self.response_to?(method_name)
+  end
+end