meta_vars 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +146 -0
- data/lib/class_level_inheritable_attributes.rb +2 -2
- data/lib/meta_vars/version.rb +1 -1
- metadata +5 -4
data/README.markdown
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# MetaVars
|
|
2
|
+
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.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
gem install meta_vars
|
|
6
|
+
|
|
7
|
+
## Based on and unaware collaborators
|
|
8
|
+
The idea of this gem came to me thanks to the [activenetwork's OmnitureClient gem](https://github.com/activenetwork/omniture_client "activenetwork's OmnitureClient")
|
|
9
|
+
|
|
10
|
+
There is some borrowed code from unawared coders. So thank you to
|
|
11
|
+
|
|
12
|
+
* [acatighera](https://github.com/acatighera "acatighera github homepage")
|
|
13
|
+
* [activenetwork](https://github.com/activenetwork "activenetwork github homepage")
|
|
14
|
+
* [John Nunemaker](http://railstips.org/about "railstips") for his [ClassLevelInheritableAttributes solution](http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/ "Class and instance variables in ruby")
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
###Defining meta_vars
|
|
18
|
+
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.
|
|
19
|
+
|
|
20
|
+
class Foo
|
|
21
|
+
include MetaVars
|
|
22
|
+
has_meta_var :var
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
has\_meta\_var accepts one options hash, with these options:
|
|
26
|
+
|
|
27
|
+
* **:inheritable**
|
|
28
|
+
|
|
29
|
+
_true_ or _false_, the meta\_vars will be inherited by subclasses
|
|
30
|
+
|
|
31
|
+
* **:default\_namespace**
|
|
32
|
+
|
|
33
|
+
default\_namespace for vars. If not given, the default namespace will be _''_
|
|
34
|
+
|
|
35
|
+
### Methods available
|
|
36
|
+
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'.
|
|
37
|
+
|
|
38
|
+
####Class methods:
|
|
39
|
+
|
|
40
|
+
* **var(varname, *namespaces, &block)**
|
|
41
|
+
|
|
42
|
+
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.
|
|
43
|
+
|
|
44
|
+
* **find\_meta\_var(var\_name, namespace=default\_var\_namespace)**
|
|
45
|
+
|
|
46
|
+
Returns the MetaVar or MetaContainer corresponding to the namespace given
|
|
47
|
+
|
|
48
|
+
####Instance methods
|
|
49
|
+
|
|
50
|
+
* **meta\_vars**
|
|
51
|
+
|
|
52
|
+
Returns an array with all meta\_vars
|
|
53
|
+
|
|
54
|
+
* **find\_meta\_vars(namespace=default\_var\_namespace)**
|
|
55
|
+
|
|
56
|
+
Returns the MetaVar or MetaContainer corresponding to the namespace given
|
|
57
|
+
|
|
58
|
+
* **find\_meta_var(var\_name, namespace=default\_var\_namespace)**
|
|
59
|
+
|
|
60
|
+
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}")
|
|
61
|
+
|
|
62
|
+
* **vars(contxt)**
|
|
63
|
+
|
|
64
|
+
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
|
|
65
|
+
|
|
66
|
+
* **find\_vars(contxt, namespace=default\_var\_namespace)**
|
|
67
|
+
|
|
68
|
+
Returns an array with all meta\_vars evaluated in the given contxt, which are inside the given namespace.
|
|
69
|
+
|
|
70
|
+
* **find\_var(contxt, var\_name, namespace=default\_var\_namespace)**
|
|
71
|
+
|
|
72
|
+
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}")
|
|
73
|
+
|
|
74
|
+
* **vars\_container**
|
|
75
|
+
|
|
76
|
+
Returns the MetaContainer object which keeps all meta\_vars
|
|
77
|
+
|
|
78
|
+
* **default\_var\_namespace**
|
|
79
|
+
|
|
80
|
+
Returns the default\_namespace given. This default\_namespace is set when declaring the meta\_vars
|
|
81
|
+
|
|
82
|
+
###Components
|
|
83
|
+
There are three components:
|
|
84
|
+
|
|
85
|
+
* MetaContainer
|
|
86
|
+
* MetaVar
|
|
87
|
+
* Var
|
|
88
|
+
|
|
89
|
+
####MetaContainer
|
|
90
|
+
It stores the meta\_vars. You should not use this class directly, but if you want to... take a look to its code ;-)
|
|
91
|
+
|
|
92
|
+
####MetaVar
|
|
93
|
+
The MetaVar itself.
|
|
94
|
+
|
|
95
|
+
* **name**
|
|
96
|
+
|
|
97
|
+
Returns the name of the instance
|
|
98
|
+
|
|
99
|
+
* **proc**
|
|
100
|
+
|
|
101
|
+
Returns the proc associated to this meta_var
|
|
102
|
+
|
|
103
|
+
* **to_var(contxt)**
|
|
104
|
+
|
|
105
|
+
Returns a Var object, which contains the value of the meta_var resulting from evaluating its proc in the given contxt
|
|
106
|
+
|
|
107
|
+
####Var
|
|
108
|
+
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
|
|
109
|
+
|
|
110
|
+
* **name**
|
|
111
|
+
|
|
112
|
+
Returns the name of the var -which it's the same as the meta_var
|
|
113
|
+
|
|
114
|
+
* **value**
|
|
115
|
+
|
|
116
|
+
Returns the value of the var
|
|
117
|
+
|
|
118
|
+
###Example
|
|
119
|
+
|
|
120
|
+
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.
|
|
121
|
+
|
|
122
|
+
class FooController
|
|
123
|
+
include MetaVars
|
|
124
|
+
|
|
125
|
+
has_meta_var :seo_var, :inheritable => true, :default_namespace => 'default'
|
|
126
|
+
|
|
127
|
+
before_filter :get_seo_title
|
|
128
|
+
|
|
129
|
+
seo_var :title, 'index' do
|
|
130
|
+
I18n.t('seo.index.title')
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
seo_var :title, 'show', 'edit' do
|
|
134
|
+
I18n.t("seo.#{params[:action]}.title", :foo => @foo)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
seo_var :title do
|
|
138
|
+
I18n.t('seo.global.title')
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def get_seo_title
|
|
142
|
+
@seo_title = (find\_seo_var(self, 'title', params[:action]) || find\_seo_var(self, 'title')).value
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
end
|
|
146
|
+
##### Copyright (c) 2011 Javier Lafora, released under MIT license
|
|
@@ -23,9 +23,9 @@ module MetaVars
|
|
|
23
23
|
def inherited(subclass)
|
|
24
24
|
@meta_vars_inheritable_attributes.each do |inheritable_attribute|
|
|
25
25
|
instance_var = "@#{inheritable_attribute}"
|
|
26
|
-
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
|
|
26
|
+
subclass.instance_variable_set(instance_var, instance_variable_get(instance_var).clone)
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
|
-
end
|
|
31
|
+
end
|
data/lib/meta_vars/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: meta_vars
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 19
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
version: 1.0.
|
|
9
|
+
- 2
|
|
10
|
+
version: 1.0.2
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- eLafo
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date:
|
|
18
|
+
date: 2012-04-04 00:00:00 Z
|
|
19
19
|
dependencies: []
|
|
20
20
|
|
|
21
21
|
description: Creates variables for an object, which their value depends on some context. These vars will be accessible by methods and will be contained in an instance var -a container
|
|
@@ -30,6 +30,7 @@ extra_rdoc_files: []
|
|
|
30
30
|
files:
|
|
31
31
|
- .gitignore
|
|
32
32
|
- Gemfile
|
|
33
|
+
- README.markdown
|
|
33
34
|
- Rakefile
|
|
34
35
|
- lib/class_level_inheritable_attributes.rb
|
|
35
36
|
- lib/meta_vars.rb
|