Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #477 (closed defect: fixed)

Opened 4 years ago

Last modified 2 years ago

[PATCH] inheritable class attributes

Reported by: bitsweat Assigned to: rails@bitsweat.net
Priority: normal Milestone: 1.2.4
Component: ActiveSupport Version: 1.2.3
Severity: normal Keywords: inheritable
Cc:

Description

You've seen them hiding in read_inheritable_attribute, write_inheritable_attributes, write_inheritable_array, and write_inheritable_hash. You've wonder, how the heck can I have class attributes that are inherited into my subclasses?

  class Foo
    class_inheritable_reader :read_me
    class_inheritable_writer :write_me
    class_inheritable_accessor :read_and_write_me
    class_inheritable_array :read_and_concat_me
    class_inheritable_hash :read_and_update_me
  end

  # Bar gets a clone of (not a reference to) Foo's attributes.
  class Bar < Foo
  end

  Bar.read_and_write_me == Foo.read_and_write_me
  Bar.read_and_write_me = 'bar'
  Bar.read_and_write_me != Foo.read_and_write_me

Reader makes class and instance reader methods, writer makes class and instance writer methods, accessor makes both. Array_writer and hash_writer make normal readers and writers which concatenation and update the argument, respectively.

Note that include ClassInheritableAttributes is no longer required since Class has these methods. The module has been retained for backward compatibility. A full test suite is included and all of Rails passes with the changes.

Attachments

class_inheritables.diff (8.2 kB) - added by bitsweat on 01/15/05 06:34:36.

Change History

01/15/05 06:34:36 changed by bitsweat

  • attachment class_inheritables.diff added.

01/15/05 06:36:33 changed by bitsweat

The read_inheritable_attribute, write_inheritable_attribute, etc. methods should be at least protected, but that would break compatibility. ActiveRecord passes with protected methods but ActionPack (layout.rb) does not.

01/15/05 13:53:06 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

05/09/07 22:12:53 changed by bramski

  • status changed from closed to reopened.
  • type changed from enhancement to defect.
  • version changed from 0.9.3 to 1.2.3.
  • resolution deleted.
  • milestone changed from 0.9.5 to 1.2.4.

Hi! So, a recent look at Rails 1.2.3, and making use of the class_inheritable_hash, or class_inheritable_array, with the following code:

$ ruby script/console Loading development environment.

class FooBar; class_inheritable_hash :my_hash; end

=> [:my_hash]

FooBar.my_hash.update(:data,"My Value")

NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.update

from (irb):2

Seems like, if this is going to really be a hash attribute, it should start out as an empty hash and NOT nil. Otherwise all of my code which makes of this attribute needs to check for nil and assign the hash to an empty hash if it's nil. Similarly for the inheritable array.

Thanks!

06/16/07 19:03:22 changed by josh

  • status changed from reopened to closed.
  • resolution set to fixed.

Best open a new ticket to report the bug.