ruby class instance variable configuration pattern

kreek

I'm trying to make a DSL like configuration for classes that include a module but to have the configured variable available to both class and instance methods seems to require littering the module with access methods. Is there a more elegant way to do this?

module DogMixin  
  class << self
    def included(base)
      base.extend ClassMethods
    end
  end

  module ClassMethods
    def breed(value)
      @dog_breed = value
    end

    def dog_breed
      @dog_breed
    end
  end
end

class Foo
  include DogMixin

  breed :havanese
end

puts Foo.dog_breed
# not implemented but should be able to do this as well
f = Foo.new
f.dog_breed
Felix

Your example is a bit weird I think :) Anyway, one way to avoid writing the accessors (the assignment - accessor is problematic in my eyes - especially in the given example) is to define constants, as in the example below. If however you need runtime-assignments, please edit your question (and thus render this answer invalid :) except you want to mess with runtime constant assignment, which is possible but messy).

module DogMixin
  # **include** DogMixin to get `Class.dog_breed`
  class << self
    def included(base)
      def base.dog_breed
        self::DOG_BREED || "pug"
      end
    end
  end

  # **extend** DogMixin to get `instance.dog_breed`
  def dog_breed
    self.class.const_get(:DOG_BREED) || "pug"
  end
end

class Foomer
  DOG_BREED = 'foomer'
  extend  DogMixin
  include DogMixin
end

f = Foomer.new
puts Foomer.dog_breed
puts f.dog_breed

# If I understand you correctly, this is the most important (?):
f.dog_breed == Foomer.dog_breed #=> true

It took some reading of (In Ruby) allowing mixed-in class methods access to class constants to get the Instance-And-Class Constant lookup from a module, but it works. I am not sure if I really like the solution though. Good question, although you could add a little detail.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ruby - instance variable as class variable

Is the `@count` an instance variable or class variable in Ruby?

Ruby class variable instance variable bug

How to initialize Ruby class instance variable to a new instance of another class?

Ruby class instance variable vs. class variable

What is the reason for moving the instance variable into an abstract class in the decorator pattern?

Calling a parent instance variable from a sub class in ruby

Ruby - How to find class name given an instance variable?

Understanding the self keyword when referring to a class instance variable in a ruby?

Ruby - set instance variable inside class method from string

Delete class instance in ruby

Class inheritance in PHP with Singleton Pattern only works if the instance variable in the inherited class is reinitialized. But why?

Class instance variable not updating

ambiguous variable in a class instance

Get class of instance variable

Why can't a class instance variable be accessed in a singleton-class definition in Ruby?

Class variable vs instance variable

Mypy with class variable that is an instance of the class

Class instance as class variable in python

Class Variable as Instance of Enclosing Class

Ruby instance variable changes unexpectedly

Ruby mark instance variable as unassignable

ruby access instance variable in instance_eval

Class Level Instance Variables in Ruby

Ruby class instance variables in subclasses

extend an initiated instance to a Class ruby

Why is EVERYTHING an instance of Class in Ruby?

Ruby instance variables within class?

Ruby class instance fields hidden

TOP Ranking

HotTag

Archive