Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache the property methods once they're called for the first time
- Loading branch information
Showing
5 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Humidifier | ||
module PropertyMethods | ||
|
||
def build_property_reader(name) | ||
define_method(name) do | ||
properties[name.to_s] | ||
end | ||
end | ||
|
||
def build_property_writer(name) | ||
define_method(name) do |value| | ||
update_property(name.to_s[0..-2], value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Humidifier | ||
VERSION = '0.0.15'.freeze | ||
VERSION = '0.0.16'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'test_helper' | ||
|
||
class PropertyMethodsTest < Minitest::Test | ||
class Slate | ||
extend Humidifier::PropertyMethods | ||
attr_accessor :properties | ||
|
||
def initialize(properties = {}) | ||
self.properties = properties | ||
end | ||
|
||
def update_property(key, value) | ||
properties[key] = value | ||
end | ||
end | ||
|
||
def test_build_property_reader | ||
Slate.build_property_reader(:foo) | ||
slate = Slate.new('foo' => 'bar') | ||
|
||
assert slate.respond_to?(:foo) | ||
assert_equal 'bar', slate.foo | ||
end | ||
|
||
def test_build_property_writer | ||
Slate.build_property_writer(:foo=) | ||
slate = Slate.new | ||
|
||
assert slate.respond_to?(:foo=) | ||
slate.foo = 'bar' | ||
assert_equal 'bar', slate.properties['foo'] | ||
end | ||
end |