Skip to content

Commit

Permalink
Ensure that attributes are returned after initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Aug 5, 2014
1 parent e9e309d commit 9ed9ff7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/lotus/validations.rb
Expand Up @@ -20,17 +20,20 @@ def valid?
value = Lotus::Utils::Kernel.send(coercer.to_s, value)
end

instance_variable_set(:"@#{ attribute }", value)
@attributes[attribute] = value
end
end
end

module ClassMethods
def attribute(name, options = {})
class_eval do
attributes << [name, options]
attr_reader name
end
attributes << [name, options]

class_eval %{
def #{ name }
@attributes.fetch(:#{ name })
end
}
end

# FIXME make this private
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures.rb
@@ -1,3 +1,9 @@
class InitializerTest
include Lotus::Validations

attribute :attr
end

class TypeValidatorTest
include Lotus::Validations

Expand Down
13 changes: 13 additions & 0 deletions test/initialize_test.rb
@@ -0,0 +1,13 @@
require 'test_helper'

describe Lotus::Validations do
describe '#initialize' do
before do
@validator = InitializerTest.new(attr: 23)
end

it 'returns a value for the given attribute' do
@validator.attr.must_equal 23
end
end
end

0 comments on commit 9ed9ff7

Please sign in to comment.