Created
September 16, 2012 06:06
-
-
Save reidmv/3731222 to your computer and use it in GitHub Desktop.
Composite Namevar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Our title_patterns method for mapping titles to namevars for supporting | |
# composite namevars. | |
def self.title_patterns | |
identity = lambda {|x| x} | |
[ | |
[ | |
/^([^:]+)$/, | |
[[ :name, identity ]] | |
], | |
[ | |
/^(.*):(.*)$/, | |
[ | |
[ :database, identity ], | |
[ :service, identity ] | |
] | |
] | |
] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_title | |
h = {} | |
type = resource_type | |
if type.respond_to? :title_patterns | |
type.title_patterns.each { |regexp, symbols_and_lambdas| | |
if captures = regexp.match(title.to_s) | |
symbols_and_lambdas.zip(captures[1..-1]).each { |symbol_and_lambda,capture| | |
sym, lam = symbol_and_lambda | |
#self[sym] = lam.call(capture) | |
h[sym] = lam.call(capture, binding) | |
} | |
return h | |
end | |
} | |
else | |
return { :name => title.to_s } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Our title_patterns method for mapping titles to namevars for supporting | |
# composite namevars. | |
def self.title_patterns | |
identity = lambda {|x,b| x} | |
param_database = lambda { |x, dynbinding| dynbinding.eval("parameters[:database]") } | |
param_service = lambda { |x, dynbinding| dynbinding.eval("parameters[:service]") } | |
param_name = lambda { |x, dynbinding| dynbinding.eval('"#{parameters[:database]}:#{parameters[:service]}"') } | |
[ | |
[ | |
/^([^:]+)$/, | |
[ | |
[ :database, param_database ], | |
[ :service, param_service ] | |
] | |
], | |
[ | |
/^(.*):(.*)$/, | |
[ | |
[ :database, identity ], | |
[ :service, identity ] | |
] | |
] | |
] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment