Skip to content

Commit

Permalink
Extract ruby_block code to a class (FC014).
Browse files Browse the repository at this point in the history
  • Loading branch information
fnichol committed Jun 1, 2012
1 parent 4246346 commit 52618ab
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 23 deletions.
76 changes: 76 additions & 0 deletions libraries/chef_rvm_passenger_calculate_attribute.rb
@@ -0,0 +1,76 @@
#
# Cookbook Name:: rvm_passenger
# Library:: Chef::RVMPassenger::CalculateAttribute
#
# Author:: Fletcher Nichol <fnichol@nichol.ca>
#
# Copyright 2012, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
module RVMPassenger
class CalculateAttribute
def initialize(node)
@node = node
end

def for_root_path
gem_home = rvm_env.info.first[1]['homes']['gem']
result = "#{gem_home}/gems/passenger-#{passenger_version}"

node.set['rvm_passenger']['root_path'] = result

This comment has been minimized.

Copy link
@fxposter

fxposter Sep 13, 2012

Why do you use node.set instead of node.default? and why can't use recalculate this path on every chef run? Cause storing this and changing ruby version will not change this variable.

Chef::Log.debug(%{Setting node['rvm_passenger']['root_path'] = } +
%{"#{node['rvm_passenger']['root_path']}"})

# NOTE: Warning, vicious hack! A not_if shell block gets interpolated
# at compile time and there was no other found way to delay eval
# until execution time. Here's a low level way: write a file, then
# read it out when you need it. I feel sick to my stomach. Somwhere a
# kitten is getting clubbed.
::File.open("/tmp/passenger_root_path", 'w') { |f| f.write(result) }
end

def for_ruby_wrapper
gem_home = rvm_env.info.first[1]['homes']['gem']
wrapper_home = gem_home.sub(/\/gems\//, '/wrappers/')
result = "#{wrapper_home}/ruby"

node.set['rvm_passenger']['ruby_wrapper'] = result
Chef::Log.debug(%{Setting node['rvm_passenger']['ruby_wrapper'] = } +
%{"#{node['rvm_passenger']['ruby_wrapper']}"})
end

private

attr_reader :node

def rvm_env
@rvm_env ||= begin
rvm_env = ::RVM::Environment.new
rvm_env.use rvm_ruby
rvm_env
end
end

def rvm_ruby
node['rvm_passenger']['rvm_ruby']
end

def passenger_version
node['rvm_passenger']['version']
end
end
end
end
25 changes: 2 additions & 23 deletions recipes/default.rb
Expand Up @@ -48,20 +48,7 @@ class Chef::Recipe
# installed. # installed.
ruby_block "Calculate node['rvm_passenger']['root_path']" do ruby_block "Calculate node['rvm_passenger']['root_path']" do
block do block do
rvm_env = ::RVM::Environment.new Chef::RVMPassenger::CalculateAttribute.new(node).for_root_path
rvm_env.use rvm_ruby
gem_home = rvm_env.info.first[1]['homes']['gem']
result = "#{gem_home}/gems/passenger-#{passenger_version}"

node.set['rvm_passenger']['root_path'] = result
Chef::Log.debug(%{Setting node['rvm_passenger']['root_path'] = } +
%{"#{node['rvm_passenger']['root_path']}"})

# NOTE: Warning, vicious hack! A not_if shell block gets interpolated at
# compile time and there was no other found way to delay eval until execution
# time. Here's a low level way: write a file, then read it out when you
# need it. I feel sick to my stomach. Somwhere a kitten is getting clubbed.
::File.open("/tmp/passenger_root_path", 'w') { |f| f.write(result) }
end end


not_if { node['rvm_passenger']['root_path'] } not_if { node['rvm_passenger']['root_path'] }
Expand All @@ -72,15 +59,7 @@ class Chef::Recipe
# installed. # installed.
ruby_block "Calculate node['rvm_passenger']['ruby_wrapper']" do ruby_block "Calculate node['rvm_passenger']['ruby_wrapper']" do
block do block do
rvm_env = ::RVM::Environment.new Chef::RVMPassenger::CalculateAttribute.new(node).for_ruby_wrapper
rvm_env.use rvm_ruby
gem_home = rvm_env.info.first[1]['homes']['gem']
wrapper_home = gem_home.sub(/\/gems\//, '/wrappers/')
result = "#{wrapper_home}/ruby"

node.set['rvm_passenger']['ruby_wrapper'] = result
Chef::Log.debug(%{Setting node['rvm_passenger']['ruby_wrapper'] = } +
%{"#{node['rvm_passenger']['ruby_wrapper']}"})
end end


not_if { node['rvm_passenger']['ruby_wrapper'] } not_if { node['rvm_passenger']['ruby_wrapper'] }
Expand Down

0 comments on commit 52618ab

Please sign in to comment.