Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: funny-falcon/funfig
base: 1508709576
...
head fork: funny-falcon/funfig
compare: d90724b9e1
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 64 additions and 11 deletions.
  1. +8 −8 README
  2. +56 −3 lib/funfig.rb
View
16 README
@@ -4,12 +4,12 @@ Example
require 'funfig'
UploadConf = Funfig.new do
- param :root do '/services' end
- group :nginx do
- param :folder do File.join(_.root, 'nginx') end
- param :port do '8000' end
+ p.root '/services'
+ g.nginx do
+ p.folder { File.join(_.root, 'nginx') }
+ p.port '8000'
end
- param :public_ip_port do '*:80' end
+ p.public_ip_port '*:80'
end
conf = UploadConf.new
@@ -22,9 +22,9 @@ Example
p conf.to_hash(true)
Up1Conf = UploadConf.clone do
- group :nginx do
- param :folder do File.join(_.root, 'nganx') end
- param :location do '/public' end
+ g.nginx do
+ p.folder { File.join(_.root, 'nganx') }
+ p.location, '/public'
end
end
View
59 lib/funfig.rb
@@ -2,6 +2,28 @@
module Funfig
NOT_SET = Object.new.freeze
+ class ProxyParam < BasicObject
+ def initialize(group)
+ @group = group
+ end
+ def method_missing(name, value = NOT_SET, &block)
+ unless value.equal? NOT_SET
+ @group.param name, value
+ else
+ @group.param name, &block
+ end
+ end
+ end
+
+ class ProxyGroup < BasicObject
+ def initialize(group)
+ @group = group
+ end
+ def method_missing(name, &block)
+ @group.group name, &block
+ end
+ end
+
class Group
def initialize(parent=nil) # :nodoc:
@parent = parent
@@ -148,7 +170,7 @@ def self.group(name, &block)
_._cache_clear!
remove_instance_variable(vname) if instance_variable_defined?(vname)
end
- klass.class_eval &block
+ klass.class_exec ProxyParam.new(klass), &block
end
# define named parameter
@@ -157,11 +179,20 @@ def self.group(name, &block)
# config = Funfig.new do
# param :name_of_param do calculate_default_value end
# end
- def self.param(name, &block)
+ def self.param(name, value = NOT_SET, &block)
_params[name] = true
vname = :"@#{name}"
name = name.to_sym
+ block ||= proc{
+ begin
+ value.dup
+ rescue TypeError
+ block = proc { value }
+ value
+ end
+ }
+
define_method(name) do
if instance_variable_defined?(vname)
instance_variable_get(vname)
@@ -185,6 +216,28 @@ def self.param(name, &block)
end
end
+ # syntax sugar proxy for declaring params
+ #
+ # :call-seq
+ # conf = Funfig.new do
+ # p.name_of_param :default_value
+ # p.other_param { calculate_default }
+ # end
+ def self.p
+ @proxy_param ||= ProxyParam.new(self)
+ end
+
+ # syntax sugar proxy for declaring group
+ #
+ # :call-seq
+ # conf = Funfig.new do
+ # g.name_of_group do
+ # end
+ # end
+ def self.g
+ @group_param ||= ProxyGroup.new(self)
+ end
+
# Create a copy of configuration scheme
#
# :call-seq:
@@ -232,7 +285,7 @@ def self._sub_name(name)
# :call-seq:
def self.new(&block)
conf = Class.new(Root)
- conf.class_eval &block
+ conf.class_exec ProxyParam.new(conf), &block
conf
end
end

No commit comments for this range

Something went wrong with that request. Please try again.