Guest User

Untitled

a guest
Jun 24th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. From c75fd5684970834d8e440deb899adba8a886e97b Mon Sep 17 00:00:00 2001
  2. From: Aiden Nibali <dismaldenizen@gmail.com>
  3. Date: Sun, 10 Oct 2010 11:09:56 +1100
  4. Subject: [PATCH 1/2] added block support for Mash
  5.  
  6. ---
  7. lib/hashie/mash.rb | 6 ++++--
  8. spec/hashie/mash_spec.rb | 14 ++++++++++++++
  9. 2 files changed, 18 insertions(+), 2 deletions(-)
  10.  
  11. diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
  12. index 97af028..6fa98ef 100644
  13. --- a/lib/hashie/mash.rb
  14. +++ b/lib/hashie/mash.rb
  15. @@ -67,7 +67,9 @@ module Hashie
  16. # Retrieves an attribute set in the Mash. Will convert
  17. # any key passed in to a string before retrieving.
  18. def [](key)
  19. - regular_reader(convert_key(key))
  20. + value = regular_reader(convert_key(key))
  21. + yield value if block_given?
  22. + value
  23. end
  24.  
  25. # Sets an attribute in the Mash. Key will be converted to
  26. @@ -141,7 +143,7 @@ module Hashie
  27. end
  28.  
  29. def method_missing(method_name, *args, &blk)
  30. - return self[method_name] if key?(method_name)
  31. + return self.[](method_name, &blk) if key?(method_name)
  32. match = method_name.to_s.match(/(.*?)([?=!]?)$/)
  33. case match[2]
  34. when "="
  35. diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
  36. index 5aa8818..f347557 100644
  37. --- a/spec/hashie/mash_spec.rb
  38. +++ b/spec/hashie/mash_spec.rb
  39. @@ -18,6 +18,20 @@ describe Hashie::Mash do
  40. @mash["test"] = "abc"
  41. @mash.test.should == "abc"
  42. end
  43. +
  44. + it "should be able to retrieve set values through blocks" do
  45. + @mash["test"] = "abc"
  46. + value = nil
  47. + @mash.[]("test") { |v| value = v }
  48. + value.should == "abc"
  49. + end
  50. +
  51. + it "should be able to retrieve set values through blocks with method calls" do
  52. + @mash["test"] = "abc"
  53. + value = nil
  54. + @mash.test { |v| value = v }
  55. + value.should == "abc"
  56. + end
  57.  
  58. it "should test for already set values when passed a ? method" do
  59. @mash.test?.should be_false
  60. --
  61. 1.7.0.4
  62.  
  63.  
  64. From 6f51fce29421f8da32b8091f05a2ef46fb4acc5a Mon Sep 17 00:00:00 2001
  65. From: Aiden Nibali <dismaldenizen@gmail.com>
  66. Date: Sun, 10 Oct 2010 11:23:40 +1100
  67. Subject: [PATCH 2/2] added block support for Dash
  68.  
  69. ---
  70. lib/hashie/dash.rb | 10 ++++++----
  71. spec/hashie/dash_spec.rb | 20 ++++++++++++++++++++
  72. 2 files changed, 26 insertions(+), 4 deletions(-)
  73.  
  74. diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
  75. index 46f877e..96a1b03 100644
  76. --- a/lib/hashie/dash.rb
  77. +++ b/lib/hashie/dash.rb
  78. @@ -34,12 +34,12 @@ module Hashie
  79.  
  80. unless instance_methods.map { |m| m.to_s }.include?("#{property_name}=")
  81. class_eval <<-ACCESSORS
  82. - def #{property_name}
  83. - _regular_reader(#{property_name.to_s.inspect})
  84. + def #{property_name}(&block)
  85. + self.[](#{property_name.to_s.inspect}, &block)
  86. end
  87.  
  88. def #{property_name}=(value)
  89. - _regular_writer(#{property_name.to_s.inspect}, value)
  90. + self.[]=(#{property_name.to_s.inspect}, value)
  91. end
  92. ACCESSORS
  93. end
  94. @@ -90,7 +90,9 @@ module Hashie
  95. # property's default value if it hasn't been set).
  96. def [](property)
  97. assert_property_exists! property
  98. - super(property.to_s)
  99. + value = super(property.to_s)
  100. + yield value if block_given?
  101. + value
  102. end
  103.  
  104. # Set a value on the Dash in a Hash-like way. Only works
  105. diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
  106. index 577bd65..f9d2c6d 100644
  107. --- a/spec/hashie/dash_spec.rb
  108. +++ b/spec/hashie/dash_spec.rb
  109. @@ -54,6 +54,26 @@ describe DashTest do
  110. subject.first_name.should == 'Franklin'
  111. end
  112. end
  113. +
  114. + context 'reading from properties' do
  115. + it 'fails reading from a non-existent property using []' do
  116. + lambda { subject['nonexistent'] }.should raise_error(NoMethodError)
  117. + end
  118. +
  119. + it "should be able to retrieve properties through blocks" do
  120. + subject["first_name"] = "Aiden"
  121. + value = nil
  122. + subject.[]("first_name") { |v| value = v }
  123. + value.should == "Aiden"
  124. + end
  125. +
  126. + it "should be able to retrieve properties through blocks with method calls" do
  127. + subject["first_name"] = "Frodo"
  128. + value = nil
  129. + subject.first_name { |v| value = v }
  130. + value.should == "Frodo"
  131. + end
  132. + end
  133.  
  134. describe '.new' do
  135. it 'fails with non-existent properties' do
  136. --
  137. 1.7.0.4
Add Comment
Please, Sign In to add comment