Anonymous avatar Anonymous committed 7e3fcae

added simple ruby sample

Comments (0)

Files changed (2)

Dynamic/ruby_dynamic/customer.rb

-
 class DynamicEntity
 
-  attr_reader :additional_data
+  attr_reader :additional_behavior
 
-  def initialize(additional_data)
-      @additional_data = additional_data
+  def initialize(additional_behavior)
+      @additional_behavior = additional_behavior
   end
   
-  def method_missing(name, *args)
-
-      if name =~ /=$/
-        self.additional_data[name.to_s.gsub("=", "")] = args[0]
-      else
-        self.additional_data[name] 
-      end
+  def respond_to?(name)
+    self.additional_behavior.include?(name)
   end
-
-  def respond_to(name)
-    raise self.additional_data.inspect
-    self.additional_data.include?(name)
-  end
+  
+  def method_missing(meth, *args, &block)
+    if self.additional_behavior.include?(meth)
+      self.additional_behavior[meth].call(self)
+    else
+      super
+    end
+   end
 end
 
 class Customer < DynamicEntity
-    attr_accessor :first_name, :list_name
+    attr_accessor :first_name, :last_name
 
 end

Dynamic/ruby_dynamic/spec/customer_spec.rb

 
     describe "Dynamic Properties" do
     
-      context "Given additional dynamic data for a customer" do
+      context "Given additional dynamic heavior for a customer" do
       
         before(:each) do
-            @additional_data = { :comments => "Some comments...",
-                                 :number_of_dependents => 3 }
+            @additional_behavior = { :full_name => lambda { |c| "#{c.first_name} #{c.last_name}" } }
         end
       
         describe "When object is constructed" do
           
-          let(:customer) { Customer.new(@additional_data) } 
+          let(:customer) { Customer.new(@additional_behavior) } 
 
-          it "should expose the additional properties" do
-              customer.should respond_to(:comments)
-              customer.should respond_to(:number_of_dependents)
+          it "should expose behaviors" do
+            customer.should respond_to(:full_name)
           end
           
-          it "should expose getters" do
-            customer.comments.should == "Some comments..."
-            customer.number_of_dependents.should == 3
-          end
-          
-          it "should expose setters" do
-            customer.comments = "New comment"
-            customer.number_of_dependents = 9
-            
-            customer.comments.should == "New comments"
-            customer.number_of_dependents.should == 9
+          it "should make behaviors accessible" do
+            customer.first_name = "Joe"
+            customer.last_name = "Jones"
+            customer.full_name.should == "Joe Jones"
           end
         end
       end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.