jonuts-figure8 0.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/README.md +0 -0
- data/lib/ext.rb +10 -0
- data/lib/figure8.rb +76 -0
- data/lib/figure8/collection_mixin.rb +39 -0
- data/lib/figure8/configurator.rb +99 -0
- metadata +59 -0
data/README.md
ADDED
File without changes
|
data/lib/ext.rb
ADDED
data/lib/figure8.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
$:.unshift(dir) unless $:.include?(dir)
|
3
|
+
|
4
|
+
require 'ext'
|
5
|
+
require 'figure8/collection_mixin'
|
6
|
+
require 'figure8/configurator'
|
7
|
+
require 'figure8/config'
|
8
|
+
require 'figure8/scope'
|
9
|
+
|
10
|
+
# FIGURE8
|
11
|
+
# =======
|
12
|
+
# encapsulate your configuration details in a succinct and pretty way
|
13
|
+
#
|
14
|
+
# Basic Usage
|
15
|
+
# ===========
|
16
|
+
# To create the encapsulating class, there are a few options:
|
17
|
+
#
|
18
|
+
# f8 :Config do
|
19
|
+
# set :parameter, "value"
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# You can now access the config by calling
|
23
|
+
# Config>( :foo )
|
24
|
+
#
|
25
|
+
# You can also set groups
|
26
|
+
# f8 :Config do
|
27
|
+
# group :group do
|
28
|
+
# set :param, "value"
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# The group params can be accessed by calling
|
33
|
+
# Config[:group]
|
34
|
+
#
|
35
|
+
# Group configs will be separate from each other
|
36
|
+
# f8 :Config do
|
37
|
+
# set :param, "foo"
|
38
|
+
# group :group do
|
39
|
+
# set :param, "bar"
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# Config> :param #=> "foo"
|
44
|
+
# Config[:group]> :param #=> "bar"
|
45
|
+
#
|
46
|
+
#
|
47
|
+
# The config class can also be created manually:
|
48
|
+
# class Klass
|
49
|
+
# extend Figure8::Configurator
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# Klass.set :param, "value"
|
53
|
+
# Klass.group(:grouping){ set :param, "value" }
|
54
|
+
#
|
55
|
+
module Figure8
|
56
|
+
class NotConstantizableError < ArgumentError ; end
|
57
|
+
|
58
|
+
module F8
|
59
|
+
def f8(klass, &config)
|
60
|
+
raise ArgumentError, "`klass' must be a String or Symbol" unless klass.is_a?(String) || klass.is_a?(Symbol)
|
61
|
+
raise NotConstantizableError, "`#{klass}' is not able to be constantized. perhaps you meant `#{klass.to_s.capitalize}'?" unless
|
62
|
+
klass.to_s.capitalized?
|
63
|
+
|
64
|
+
klass = Object.const_get(klass)
|
65
|
+
rescue NameError
|
66
|
+
klass = Object.const_set(klass, Class.new)
|
67
|
+
ensure
|
68
|
+
if klass.respond_to?(:class_eval) && block_given?
|
69
|
+
klass.extend Figure8::Configurator
|
70
|
+
klass.class_eval(&config)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
Object.send :include, Figure8::F8
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Figure8
|
2
|
+
module CollectionMixin
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
extend ClassMethods
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :all
|
9
|
+
attr_reader :finder
|
10
|
+
|
11
|
+
def find_by(attribute)
|
12
|
+
@finder = attribute
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def [](thing)
|
20
|
+
find(thing)
|
21
|
+
end
|
22
|
+
|
23
|
+
def find(thing)
|
24
|
+
Array(all).find{ |e| e.send(self.finder) == thing }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(*args)
|
29
|
+
add_self_to_collection
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def add_self_to_collection
|
35
|
+
self.class.all ||= []
|
36
|
+
self.class.all << self unless self.class[self]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module Figure8
|
2
|
+
module Configurator
|
3
|
+
|
4
|
+
# NOTE: i suck
|
5
|
+
def self.extended(base)
|
6
|
+
base.class_eval do
|
7
|
+
class << self; attr_reader :scope; end
|
8
|
+
|
9
|
+
@scope = base
|
10
|
+
end
|
11
|
+
|
12
|
+
Object.class_eval("class #{base.name}::Config; end; class #{base.name}::Group; end")
|
13
|
+
|
14
|
+
base::Config.class_eval do
|
15
|
+
include CollectionMixin
|
16
|
+
|
17
|
+
find_by :name
|
18
|
+
|
19
|
+
def initialize(opts={})
|
20
|
+
@name = opts.delete(:name)
|
21
|
+
@value = opts.delete(:value)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_accessor :name, :value
|
26
|
+
end
|
27
|
+
|
28
|
+
base::Group.class_eval do
|
29
|
+
include CollectionMixin
|
30
|
+
|
31
|
+
class << self; attr_reader :scope; end
|
32
|
+
|
33
|
+
@scope = base
|
34
|
+
|
35
|
+
find_by :name
|
36
|
+
|
37
|
+
def initialize(opts={})
|
38
|
+
@name = opts.delete(:name)
|
39
|
+
@configs = []
|
40
|
+
super
|
41
|
+
end
|
42
|
+
|
43
|
+
attr_reader :name, :configs
|
44
|
+
|
45
|
+
def find_config(name, chain=false)
|
46
|
+
c = configs.find{ |c| c.name == name }
|
47
|
+
|
48
|
+
return c if chain
|
49
|
+
|
50
|
+
c ? c.value : c
|
51
|
+
end
|
52
|
+
alias :[] :find_config
|
53
|
+
|
54
|
+
def find_or_create_config(name)
|
55
|
+
find_config(name, true) || configs.push(self.class.scope::Config.new(:name => name)).last
|
56
|
+
end
|
57
|
+
|
58
|
+
def set(name, val)
|
59
|
+
find_or_create_config(name).value = val
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def set(name, val=nil, &blk)
|
66
|
+
if val.nil? && block_given?
|
67
|
+
group(name, &blk)
|
68
|
+
elsif val.nil?
|
69
|
+
raise ArgumentError, "you must supply a value with your setting"
|
70
|
+
else
|
71
|
+
find_or_create(scope::Config, name).value = val
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def group(name, &blk)
|
76
|
+
find_or_create(scope::Group, name).instance_eval(&blk)
|
77
|
+
end
|
78
|
+
|
79
|
+
def [](name)
|
80
|
+
return scope::Group[name] if scope::Group[name]
|
81
|
+
|
82
|
+
find_config(name)
|
83
|
+
end
|
84
|
+
|
85
|
+
def find_config(name)
|
86
|
+
c = scope::Config[name]
|
87
|
+
c.value if c && !c.value.nil?
|
88
|
+
end
|
89
|
+
|
90
|
+
def find_group(name)
|
91
|
+
scope::Group[name]
|
92
|
+
end
|
93
|
+
|
94
|
+
def find_or_create(klass, name)
|
95
|
+
klass[name] || klass.new(:name => name)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jonuts-figure8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jonah honeyman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-17 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: configuration encapsulation
|
17
|
+
email: jonah@honeyman.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.md
|
26
|
+
- lib/figure8
|
27
|
+
- lib/figure8.rb
|
28
|
+
- lib/ext.rb
|
29
|
+
- lib/figure8/collection_mixin.rb
|
30
|
+
- lib/figure8/configurator.rb
|
31
|
+
has_rdoc: false
|
32
|
+
homepage: http://github.com/jonuts/figure8
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- --inline-source
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: configuration encapsulation
|
58
|
+
test_files: []
|
59
|
+
|