Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

support for overloading property getters/setters

  • Loading branch information...
commit fd37f387e37d56161b2631525bf1e86ca1cbcd9d 1 parent b082f58
Mislav Marohnić authored

Showing 2 changed files with 14 additions and 2 deletions. Show diff stats Hide diff stats

  1. +4 2 lib/mingo/properties.rb
  2. +10 0 spec/mingo_spec.rb
6 lib/mingo/properties.rb
@@ -11,12 +11,13 @@ module Properties
11 11 module ClassMethods
12 12 attr_reader :properties
13 13
14   - def property(name, options = {})
  14 + def property(name, options = nil)
15 15 self.properties << name.to_sym
16 16
17 17 setter_name = "#{name}="
18 18 unless method_defined?(setter_name)
19   - class_eval <<-RUBY, __FILE__, __LINE__
  19 + methods = Module.new
  20 + methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
20 21 def #{name}(&block)
21 22 self.[](#{name.to_s.inspect}, &block)
22 23 end
@@ -25,6 +26,7 @@ def #{setter_name}(value)
25 26 self.[]=(#{name.to_s.inspect}, value)
26 27 end
27 28 RUBY
  29 + include methods
28 30 end
29 31
30 32 if defined? @subclasses
10 spec/mingo_spec.rb
@@ -6,6 +6,10 @@
6 6 class User < Mingo
7 7 property :name
8 8 property :age
  9 +
  10 + def age=(value)
  11 + super(value.nil? ? nil : value.to_i)
  12 + end
9 13 end
10 14
11 15 describe User do
@@ -76,6 +80,12 @@ class User < Mingo
76 80 doc.should have_key('name')
77 81 end
78 82 end
  83 +
  84 + it "supports overloading the setter method" do
  85 + user = build
  86 + user.age = '12'
  87 + user.age.should == 12
  88 + end
79 89
80 90 context "existing doc" do
81 91 before do

0 comments on commit fd37f38

Please sign in to comment.
Something went wrong with that request. Please try again.