meta_vars 1.0.1

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/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in meta_vars.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,31 @@
1
+ # This code was proudly and shamelessly copied from
2
+ # http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/
3
+ # so let's all thank John Nunemaker with a loud and strong applause: PLAS!!!!'
4
+
5
+ module MetaVars
6
+ module ClassLevelInheritableAttributes
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def inheritable_attributes(*args)
13
+ @meta_vars_inheritable_attributes ||= [:meta_vars_inheritable_attributes]
14
+ @meta_vars_inheritable_attributes += args
15
+ args.each do |arg|
16
+ class_eval %(
17
+ class << self; attr_accessor :#{arg} end
18
+ )
19
+ end
20
+ @meta_vars_inheritable_attributes
21
+ end
22
+
23
+ def inherited(subclass)
24
+ @meta_vars_inheritable_attributes.each do |inheritable_attribute|
25
+ instance_var = "@#{inheritable_attribute}"
26
+ subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,65 @@
1
+ module MetaVars
2
+ class MetaContainer
3
+
4
+ attr_reader :collection
5
+
6
+ def initialize
7
+ @collection = {}
8
+ end
9
+
10
+ def add(meta_var, namespace)
11
+ if namespace.nil?
12
+ @collection[meta_var.name.to_s] = meta_var
13
+ else
14
+ current_level, tail = namespace.split('.', 2)
15
+ @collection[current_level.to_s] ||= MetaContainer.new
16
+ @collection[current_level.to_s].add(meta_var, tail)
17
+ end
18
+ end
19
+
20
+ def find(namespace)
21
+ current_level, tail = namespace.split('.', 2)
22
+ if tail.nil?
23
+ @collection[current_level.to_s]
24
+ else
25
+ @collection[current_level.to_s].present? ? @collection[current_level.to_s].find(tail) : nil
26
+ end
27
+ end
28
+
29
+ def meta_vars
30
+ @collection.keys.inject([]) do |a,k|
31
+ case @collection[k]
32
+ when MetaVars::MetaContainer
33
+ a += @collection[k].meta_vars
34
+ when MetaVars::MetaVar
35
+ a << @collection[k]
36
+ end
37
+ a
38
+ end
39
+ end
40
+
41
+ def vars(env)
42
+ @collection.keys.inject([]) do |a,k|
43
+ case @collection[k]
44
+ when MetaVars::MetaContainer
45
+ a += @collection[k].vars(env)
46
+ when MetaVars::MetaVar
47
+ a << @collection[k].to_var(env)
48
+ end
49
+ end
50
+ end
51
+
52
+ def to_hash
53
+ @collection.keys.inject({}) do |h,k|
54
+ if @collection[k].is_a?(MetaVars::MetaContainer)
55
+ h[k] = @collection[k].to_hash
56
+ else
57
+ h[k] = @collection[k]
58
+ end
59
+ h
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,25 @@
1
+ module MetaVars
2
+ class MetaVar
3
+
4
+ attr_accessor :name, :proc
5
+ attr_reader :cache_key, :expires_in
6
+
7
+ def initialize(name, options = {}, &block)
8
+ @name = name
9
+ @proc = block
10
+ @cache_key = options[:cache_key] || "meta_vars/#{name}"
11
+ @expires_in = options[:expires_in] || 0
12
+ end
13
+
14
+ def to_var(contxt)
15
+
16
+ if @expires_in > 0
17
+ Rails.cache.fetch(@cache_key, :expires_in => @expires_in) do
18
+ MetaVars::Var.new(name, contxt.instance_eval(&proc))
19
+ end
20
+ else
21
+ MetaVars::Var.new(name, contxt.instance_eval(&proc))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module MetaVars
2
+ class Var
3
+
4
+ attr_accessor :name, :value
5
+
6
+ def initialize(name, value)
7
+ @name = name
8
+ @value = value
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module MetaVars
2
+ VERSION = "1.0.1"
3
+ end
data/lib/meta_vars.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'meta_vars/version'
2
+ require 'class_level_inheritable_attributes'
3
+ require 'meta_vars/var'
4
+ require 'meta_vars/meta_var'
5
+ require 'meta_vars/meta_container'
6
+
7
+ module MetaVars
8
+ def self.included(base)
9
+ base.send(:include, MetaVars::ClassLevelInheritableAttributes)
10
+ base.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ def has_meta_var(name, options = {})
15
+
16
+ default_namespace = (options[:default_namespace])
17
+ method_name = name.to_s.singularize
18
+ container_name = "#{name.to_s.pluralize}_container"
19
+ inheritable = options[:inheritable]
20
+
21
+ meta_programming = %Q(
22
+ @#{container_name} = MetaVars::MetaContainer.new
23
+ @default_#{method_name}_namespace = '#{default_namespace}'
24
+
25
+ inheritable_attributes :#{container_name}, :default_#{method_name}_namespace if #{inheritable}.present?
26
+
27
+ class << self
28
+
29
+ attr_reader :#{container_name}
30
+ attr_accessor :default_#{method_name}_namespace
31
+
32
+ def #{method_name}(var_name, *namespaces, &block)
33
+ namespaces = ['#{default_namespace}'] if namespaces.empty?
34
+ namespaces.each do |namespace|
35
+ @#{container_name}.add(MetaVars::MetaVar.new(var_name, &block), namespace)
36
+ end
37
+ end
38
+
39
+ def find_meta_#{method_name}(var_name, namespace='#{default_namespace}')
40
+ @#{container_name}.find([namespace,var_name].join('.'))
41
+ end
42
+
43
+ end
44
+
45
+ def meta_#{method_name.pluralize}
46
+ #{container_name}.meta_vars
47
+ end
48
+
49
+ def find_meta_#{method_name.pluralize}(namespace = '#{default_namespace}')
50
+ #{container_name}.find(namespace).try(:meta_vars) || []
51
+ end
52
+
53
+ def find_meta_#{method_name}(var_name, namespace = '#{default_namespace}')
54
+ #{container_name}.find([namespace, var_name].join('.'))
55
+ end
56
+
57
+ def #{method_name.pluralize}(contxt)
58
+ #{container_name}.vars(contxt)
59
+ end
60
+
61
+ def find_#{method_name}(contxt, var_name, namespace = '#{default_namespace}')
62
+ #{container_name}.find([namespace, var_name].join('.')).try(:to_var,contxt)
63
+ end
64
+
65
+ def find_#{method_name.pluralize}(contxt, namespace = '#{default_namespace}')
66
+ #{container_name}.find(namespace).try(:vars,contxt) || []
67
+ end
68
+
69
+ def #{container_name}
70
+ self.class.#{container_name}
71
+ end
72
+
73
+ def default_#{method_name}_namespace
74
+ self.class.default_#{method_name}_namespace
75
+ end
76
+ )
77
+ self.class_eval(meta_programming)
78
+ end
79
+ end
80
+ end
data/meta_vars.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "meta_vars/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "meta_vars"
7
+ s.version = MetaVars::VERSION
8
+ s.authors = ["eLafo"]
9
+ s.email = ["javierlafora@gmail.com"]
10
+ s.homepage = "https://github.com/eLafo/meta_vars"
11
+ s.summary = %q{Creates variables for an object, which their value depends on some context}
12
+ s.description = %q{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}
13
+
14
+ s.rubyforge_project = "meta_vars"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta_vars
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - eLafo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-21 00:00:00 Z
19
+ dependencies: []
20
+
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
22
+ email:
23
+ - javierlafora@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - lib/class_level_inheritable_attributes.rb
35
+ - lib/meta_vars.rb
36
+ - lib/meta_vars/meta_container.rb
37
+ - lib/meta_vars/meta_var.rb
38
+ - lib/meta_vars/var.rb
39
+ - lib/meta_vars/version.rb
40
+ - meta_vars.gemspec
41
+ homepage: https://github.com/eLafo/meta_vars
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: meta_vars
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Creates variables for an object, which their value depends on some context
74
+ test_files: []
75
+