Module: Darthjee::CoreExt::Class

Included in:
Class
Defined in:
lib/darthjee/core_ext/class.rb

Instance Method Summary collapse

Instance Method Details

#default_reader(name, value) ⇒ ::Symbol

Creates a method that will act as reader with default value

The method will be a reader, but when no value was defined for the instance variable, it will return a default

Examples:

Defining a default value

class Person
  attr_writer :name
  default_reader :name, 'John Doe'
end

model = Person.new

model.name # returns 'John Doe'

Changing the instance value


model = Person.new
model.name # returns 'John Doe'

model.name = 'Joe'
model.name # returns 'Joe'

model.name = nil
model.name # returns nil

Changing values accros instances


model = Person.new
model.name # returns 'John Doe'

model.name = 'Bob'
model.name # returns 'Bob'
Person.new.name # returns 'John Doe'

Parameters:

  • name (::Symbol, ::String)

    Name of the method to be added

  • value (::Object)

    default value

Returns:



132
133
134
135
136
137
# File 'lib/darthjee/core_ext/class.rb', line 132

def default_reader(name, value)
  define_method(name) do
    return value unless instance_variable_defined?("@#{name}")
    instance_variable_get("@#{name}")
  end
end

#default_readers(*names, value) ⇒ ::Array<::Symbol>

Creates methods that will act as reader with default value

The methods will be readers, but when no value was defined for the instance variable, it will return a default

Examples:

Defining default values

class Person
  attr_writer :cars, :houses
  default_reader :cars, :houses, 'none'
end

model = Person.new

model.cars # returns 'none'

Changing the instance value


model = Person.new
model.cars # returns 'none'

model.cars = ['volvo']
model.cars # returns ['volvo']

model.cars = nil
model.cars # returns nil

Changing values accros instances


model = Person.new
model.cars # returns 'none'

model.cars = ['volvo']
model.cars # returns ['volvo']
Person.new.cars # returns 'none'

Comparing value across methods

model.cars                           # returns 'none'
model.cars.equal?('none')            # returns false
model.nick_name.equal?(model.houses) # returns true

Parameters:

Returns:



187
188
189
190
191
# File 'lib/darthjee/core_ext/class.rb', line 187

def default_readers(*names, value)
  names.map do |name|
    default_reader(name, value)
  end
end

#default_value(name, value) ⇒ ::Symbol

Adds a method that will return a default value

the value is evaluated on class definition, meaning that everytime it is called it will be the same instance

Examples:

Defining a default value

class MyClass
  default_value :name, 'John'
end

MyClass.new.name # returns 'John'

Comparing value across instances

class MyClass
  default_value :name, 'John'
end

instance = MyClass.new
other = MyClass.new

instance.name.equal?('John')      # returns false
instance.name.equal?(other.name)  # returns true

Parameters:

  • name (::Symbol, ::String)

    Name of the method to be added

  • value (::Object)

    default value

Returns:

  • (::Symbol)

    The name of the added method



37
38
39
# File 'lib/darthjee/core_ext/class.rb', line 37

def default_value(name, value)
  define_method(name) { |*_| value }
end

#default_values(*names, value) ⇒ ::Array<::Symbol>

Adds methods that will return a default value

the value is evaluated on class definition, meaning that everytime any of them are called they will return the same instance of value

Examples:

Defining a default values

class MyClass
  default_values :name, :nick_name, 'John'
end

MyClass.new.name      # returns 'John'
MyClass.new.nick_name # returns 'John'

Comparing value across instances

class MyClass
  default_values :name, :nick_name, 'John'
end

instance = MyClass.new
other = MyClass.new

instance.name.equal?('John')     # returns false
instance.name.equal?(other.name) # returns true

Comparing value across methods

class MyClass
  default_values :name, :nick_name, 'John'
end

instance = MyClass.new

instance.nick_name.equal?('John')        # returns false
instance.nick_name.equal?(instance.name) # returns true

Parameters:

Returns:

See Also:



84
85
86
87
88
# File 'lib/darthjee/core_ext/class.rb', line 84

def default_values(*names, value)
  names.map do |name|
    default_value(name, value)
  end
end