Skip to content

eLafo/meta_vars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MetaVars

This gem provides you a way to declare variables which value depends on some context. You can declare these variables inside a namespace so you can access easily to them.

Installation

gem install meta_vars

Based on and unaware collaborators

The idea of this gem came to me thanks to the activenetwork's OmnitureClient gem

There is some borrowed code from unawared coders. So thank you to

Usage

###Defining meta_vars You must include MetaVars and then declare the meta_vars you are going to use calling the has_meta_var function passing a generic name for your meta_vars.

  class Foo
      include MetaVars
      has_meta_var :var
  end

has_meta_var accepts one options hash, with these options:

  • :inheritable

    true or false, the meta_vars will be inherited by subclasses

  • :default_namespace

    default_namespace for vars. If not given, the default namespace will be ''

Methods available

Once you have done this, you will be able to call all these methods inside your class. Note that these methods are generated for the generic name 'var'.

####Class methods:

  • *var(varname, namespaces, &block)

    It creates a new meta_var, with its name like var_name and a block, which will be evaluated when the value is needed inside a given contxt.

  • find_meta_var(var_name, namespace=default_var_namespace)

    Returns the MetaVar or MetaContainer corresponding to the namespace given

####Instance methods

  • meta_vars

    Returns an array with all meta_vars

  • find_meta_vars(namespace=default_var_namespace)

    Returns the MetaVar or MetaContainer corresponding to the namespace given

  • find_meta_var(var_name, namespace=default_var_namespace)

    The same as find_meta_vars, but you can specify a var name. In fact, it is the same as find_meta_vars("#{namespace}.#{var_name}")

  • vars(contxt)

    Returns an array with all meta_vars evaluated in the given contxt. The evaluation is done by calling an instance_eval in the contxt passed of the proc in the var

  • find_vars(contxt, namespace=default_var_namespace)

    Returns an array with all meta_vars evaluated in the given contxt, which are inside the given namespace.

  • find_var(contxt, var_name, namespace=default_var_namespace)

    The same as find_vars, but you can specify a var name. In fact, it is the same as find_vars(contxt, "#{namespace}.#{var_name}")

  • vars_container

    Returns the MetaContainer object which keeps all meta_vars

  • default_var_namespace

    Returns the default_namespace given. This default_namespace is set when declaring the meta_vars

###Components There are three components:

  • MetaContainer
  • MetaVar
  • Var

####MetaContainer It stores the meta_vars. You should not use this class directly, but if you want to... take a look to its code ;-)

####MetaVar The MetaVar itself.

  • name

    Returns the name of the instance

  • proc

    Returns the proc associated to this meta_var

  • to_var(contxt)

    Returns a Var object, which contains the value of the meta_var resulting from evaluating its proc in the given contxt

####Var The result of the evaluation of the MetaVar in a context. You can get this by calling the to_var method in a MetaVar instance

  • name

    Returns the name of the var -which it's the same as the meta_var

  • value

    Returns the value of the var

###Example

Let's create a meta_var for storing the title of the page. Since this value it's not going to be the same in every action, we can use action names as namespaces.

  class FooController
      include MetaVars

      has_meta_var :seo_var, :inheritable => true, :default_namespace => 'default'

      before_filter :get_seo_title

      seo_var :title, 'index' do
          I18n.t('seo.index.title')
      end

      seo_var :title, 'show', 'edit' do
          I18n.t("seo.#{params[:action]}.title", :foo => @foo)
      end

      seo_var :title do
          I18n.t('seo.global.title')
      end

      def get_seo_title
        @seo_title = (find\_seo_var(self, 'title', params[:action]) || find\_seo_var(self, 'title')).value
      end

  end
Copyright (c) 2011 Javier Lafora, released under MIT license

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages