Skip to content
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ class Song

This won't `clone` but simply pass the value on to the subclass.

## Inherit method

`::inheritable_attr` uses `Object#clone` method which doesn't work properly for cloning i.e. hashes. You can specify your own inherit method:

```ruby
require 'active_support/core_ext/object/deep_dup'

class Song
extend Uber::InheritableAttr
inheritable_attr :properties, inherit_method: :deep_dup
```

# Dynamic Options

Expand Down
7 changes: 6 additions & 1 deletion lib/uber/inheritable_attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def self.inherit_for(klass, name, options={})
value = klass.superclass.send(name) # could be nil

return value if options[:clone] == false
Clone.(value) # this could be dynamic, allowing other inheritance strategies.

if options[:inherit_method]
value.send(options[:inherit_method])
else
Clone.(value) # this could be dynamic, allowing other inheritance strategies.
end
end

class Clone
Expand Down
10 changes: 10 additions & 0 deletions test/inheritable_attr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class InheritableAttrTest < MiniTest::Spec
inheritable_attr :drinks
inheritable_attr :glass
inheritable_attr :guests, clone: false

inheritable_attr :dj, inherit_method: :dup
self.dj = "Paul Oakenfold".freeze
end
}

Expand Down Expand Up @@ -87,5 +90,12 @@ def assert_nothing_raised(*)

subklass.glass.must_equal false
end

it "respects :inherit_method" do
class B < subject
end

refute B.dj.frozen?
end
end
end