Skip to content

Commit

Permalink
Merge branch 'master' into 0.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed May 6, 2015
2 parents c0974dd + 3efe25f commit 8701120
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/lotus/validations/attribute_definer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'set'
require 'lotus/utils/attributes'
require 'lotus/validations/nested_attributes'

module Lotus
module Validations
Expand Down Expand Up @@ -367,12 +368,8 @@ def define_lazy_reader(name, type)
#
# @since 0.2.4
# @api private
def build_validation_class(&block)
kls = Class.new do
include Lotus::Validations
end
kls.class_eval(&block)
kls
def build_validation_class(&blk)
NestedAttributes.fabricate(&blk)
end
end

Expand Down
22 changes: 22 additions & 0 deletions lib/lotus/validations/nested_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Lotus
module Validations
# @since x.x.x
# @api private
class NestedAttributes
# @since x.x.x
# @api private
def self.fabricate(&blk)
dup.tap do |klass|
klass.class_eval { include Lotus::Validations }
klass.class_eval(&blk)
end
end

# @since x.x.x
# @api private
def lotus_nested_attributes?
true
end
end
end
end
10 changes: 10 additions & 0 deletions test/nested_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
include Lotus::Validations

attribute :name, type: String
attribute :tags, type: Array
attribute :address do
attribute :line_one, type: String, presence: true
attribute :city, type: String
Expand Down Expand Up @@ -54,5 +55,14 @@

validator.to_h.must_equal(data)
end

# Bug
# See https://github.com/lotus/validations/issues/58#issuecomment-99144243
it 'safely serialize to Hash' do
data = {name: 'John Smith', tags: [1, 2]}
validator = @klass.new(data)

validator.to_h.must_equal(data)
end
end
end

0 comments on commit 8701120

Please sign in to comment.