Module: Guise::Syntax

Defined in:
lib/guise/syntax.rb

Instance Method Summary collapse

Instance Method Details

#guise_for(source_class_name, options = {}) ⇒ Object

Configures the other end of the association defined by #has_guises. Defines equivalent scopes defined on the model configured with #has_guises.

Given the following configuring of has_guises:

class User < ActiveRecord::Base
  has_guises :DeskWorker, :MailForwarder, association: :roles, attribute: :title
end

The following call to guise_for:

class Role < ActiveRecord::Base
  guise_for :User
end

Is equivalent to:

class Role < ActiveRecord::Base
  belongs_to :user

  validates :title, presence: true, uniqueness: { scope: :user_id }, inclusion: { in: %w( DeskWorker MailForwarder ) }

  scope :desk_workers, -> { where(title: "DeskWorker") }
  scope :mail_forwarder, -> { where(title: "MailForwarder") }
end

Parameters:

  • source_class_name (Symbol, String)

    name of the class configured with #has_guises

  • options (Hash) (defaults to: {})

    options to configure the belongs_to association.

Options Hash (options):

  • :validate (false)

    specify false to skip validations for the :attribute specified in #has_guises

  • :foreign_key (Symbol)

    foreign key used to build the association.



131
132
133
# File 'lib/guise/syntax.rb', line 131

def guise_for(source_class_name, options = {})
  Guise.register_association(self, source_class_name, options)
end

#guise_of(source_class_name) ⇒ Object

Specifies that the model is a subclass of a model configured with #has_guises specified by class_name.

Configures the caller with the correct default_scope. Given the following definition with has_guises:

class User < ActiveRecord::Base
  has_guises :DeskWorker, :MailForwarder, association: :roles, attribute: :title
end

The following call to guise_of:

class DeskWorker < User guise_of :User end


Is equivalent to:

```ruby
class DeskWorker < User
  default_scope -> { desk_workers }

  after_initialize do
    self.guises.build(title: 'DeskWorker')
  end

  after_create do
    self.guises.create(title: 'DeskWorker')
  end
end

Parameters:

  • source_class_name (String, Symbol)

    name of the superclass configured with #has_guises.



84
85
86
87
88
89
# File 'lib/guise/syntax.rb', line 84

def guise_of(source_class_name)
  options = Guise.registry[source_class_name]

  default_scope GuiseOfScope.new(name, options)
  after_initialize SourceCallback.new(name, options.attribute)
end

#has_guises(*guises, options) ⇒ Object

Setup the model's guises association. Given the following setup:

class User < ActiveRecord::Base
  has_guises :DeskWorker, :MailForwarder, association: :roles, attribute: :value
end

The following is configured:

  • has_many association named according to the :association option.
  • User.desk_workers and User.mail_forwarders model scopes.
  • User#has_guise? that checks if a user is a particular type.
  • User#desk_worker?, User#mail_forwarder? that proxy to User#has_guise?.
  • User#has_guises? that checks if a user has records for all the types supplied. This is aliased to User#has_roles?.
  • User#has_any_guises? that checks if a user has records for any of the types supplied. This is aliased to User#has_any_roles?.
  • If the association name is not :guises:
    • Aliases the association methods to equivalent methods for guises (i.e. guises= to roles=).
    • Aliases the introspection methods (i.e. has_guise? to has_role? and has_any_guises? to has_any_roles?

Parameters:

  • *guises (Array<Symbol, String>)

    names of guises that should be allowed

  • options (Hash)

    options to configure the association

Options Hash (options):

  • :association (Symbol)

    name of the association to define. This option is required.

  • :attribute (Symbol)

    name of the association's column where the value of which guise is being represented is stored. This option is required.



41
42
43
44
45
# File 'lib/guise/syntax.rb', line 41

def has_guises(*guises)
  include Introspection

  Guise.register_source(self, *guises)
end

#scoped_guise_for(association_class_name) ⇒ Object

Specifies that the model is a subclass of a model configured with #guise_for specified by class_name. The name of the calling class must be <value|parent_class_name>.

Given the following configuration with guise_for:

class Role < ActiveRecord::Base
  guise_for :User
end

The following call to scoped_guise_for:

class DeskWorkerRole < Role
  scoped_guise_for :Role
end

Is equivalent to:

class DeskWorkerRole < Role
  default_scope -> { desk_workers }

  after_initialize do
    self.title = "DeskWorker"
  end
end

Parameters:

  • association_class_name (Symbol, String)

    name of the superclass configured with #guise_for.



169
170
171
172
173
174
175
# File 'lib/guise/syntax.rb', line 169

def scoped_guise_for(association_class_name)
  options = Guise.registry[association_class_name]
  value = name.chomp(options.association_class.name)

  default_scope options.scope(value, :guise_for)
  after_initialize AssociationCallback.new(value, options.attribute)
end