goku2 asked:

Default property value?

Tags: og nitro 0.29

How can i set up a default value to a Og property?

I’ve tried to override the intialize method but doesn’t work.

If i have a class like this:

class Company
  property :name, String
  property :active, TrueClass

  def initialize
    active = true
  end
end

When i create a Company object, all the properties are nil.

def test_initialization
        
    c = Company.new
    assert_equal nil, c.active
    assert_equal nil, c.name
end

And the result of this test is

Loaded suite test/tc_company
Started
I, [2006-05-02T15:41:59.609782 #19194]  INFO -- : Og uses the Mysql store.
Database "db_test" dropped
I, [2006-05-02T15:41:59.884925 #19194]  INFO -- : Database 'db_test' not found!
I, [2006-05-02T15:42:00.976958 #19194]  INFO -- : Created table 'ogcompany'.
.
Finished in 1.093455 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

I just need to set a default value to some properties, or in this case, the active property. Is there a special param 4 this? Or do i have to use a before aspect? Thanks a lot!

(1 attempts)

riffraff answered:

if you just write "foo = bar"

it will be interpreted as a local variable and not a method call, so it will be just forgotten when #initialize ends. You should initialize an instance variable or use "self.foo=name"

Rating: 5