Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1
2.3.1
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rvm:
- 2.0
- 2.1
- 2.2
- 2.3
- jruby
- rbx
- ruby-head
Expand All @@ -16,8 +17,4 @@ matrix:
- rvm: rbx
notifications:
email:
- [email protected]
- [email protected]
addons:
code_climate:
repo_token: 2b66fbb7c7c72503eb7841a479c0ad923f691729f4109b4aa8c9b4def1ebb42d
- [email protected]
39 changes: 37 additions & 2 deletions lib/virtus/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,39 @@ module MassAssignment
def attributes
attribute_set.get(self)
end
alias_method :to_hash, :attributes
alias_method :to_h, :attributes

# Returns a hash of all publicly accessible attributes (including nested attributes)
#
# @example
# class Child
# include Virtus
#
# attribute :name, String
# end
#
# class Parent
# include Virtus
#
# attribute :name, String
# attribute :child, Child
# end
#
# parent = Parent.new(name: 'John', child: {name: 'Jim'})
# parent.to_h # => { name: 'John', child: {name: 'Jim'} }
#
# @return [Hash]
#
# @api public
def to_h
attributes.each_with_object({}) do |(k, v), h|
if v.is_a? Array
h[k] = v.map { |v| hash_if_responds_or_value v }
else
h[k] = hash_if_responds_or_value v
end
end
end
alias_method :to_hash, :to_h

# Mass-assign attribute values
#
Expand Down Expand Up @@ -198,6 +229,10 @@ def set_default_attributes!

private

def hash_if_responds_or_value(value)
value.respond_to?(:to_h) ? value.to_h : value
end

# The list of allowed public methods
#
# @return [Array<String>]
Expand Down
26 changes: 26 additions & 0 deletions spec/unit/virtus/attributes_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,30 @@

it_behaves_like 'attribute hash'
end

context "#to_h / #to_hash" do
let(:model) {
child = Class.new {
include Virtus

attribute :foo, String
}

Class.new {
include Virtus

attribute :bar, String
attribute :nested_model, child
attribute :array_of_nested_model, Array[child]

}
}

subject { model.new bar: "1", nested_model: { foo: "2" }, array_of_nested_model: [{ foo: "3" }, { foo: "4" }] }

it "deeply converts to a hash" do
expect(subject.to_h).to eql(bar: "1", nested_model: { foo: "2" }, array_of_nested_model: [{ foo: "3" }, { foo: "4" }] )
expect(subject.to_hash).to eql(bar: "1", nested_model: { foo: "2" }, array_of_nested_model: [{ foo: "3" }, { foo: "4" }] )
end
end
end