Skip to content

Commit

Permalink
Various changes for Ruby 1.9 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
kainosnoema committed Feb 17, 2010
1 parent 871f915 commit 1835dec
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 83 deletions.
5 changes: 3 additions & 2 deletions app/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ def invoke
raise RUBYAMFException.new(RUBYAMFException.UNDEFINED_OBJECT_REFERENCE_ERROR, "There was an error loading the service class #{@amfbody.service_class_name}")
end

if @service.private_methods.include?(@amfbody.service_method_name)
# Ruby 1.9 Compatibility - public_methods.include? to public_methods.any?
if @service.private_methods.any? { |m| m.to_s == @amfbody.service_method_name }
raise RUBYAMFException.new(RUBYAMFException.METHOD_ACCESS_ERROR, "The method {#{@amfbody.service_method_name}} in class {#{@amfbody.service_class_file_path}} is declared as private, it must be defined as public to access it.")
elsif !@service.public_methods.include?(@amfbody.service_method_name)
elsif !@service.public_methods.any? { |m| m.to_s == @amfbody.service_method_name }
raise RUBYAMFException.new(RUBYAMFException.METHOD_UNDEFINED_METHOD_ERROR, "The method {#{@amfbody.service_method_name}} in class {#{@amfbody.service_class_file_path}} is not declared.")
end

Expand Down
23 changes: 14 additions & 9 deletions app/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ def register(mapping) #register a value object map
ClassMappings.ignore_fields.to_a.each{|k| hashed_ignores[k] = true} # strings and nils will be put into an array with to_a
mapping[:ignore_fields].to_a.each{|k| hashed_ignores[k] = true}
mapping[:ignore_fields] = hashed_ignores # overwrite the original ignore fields
mapping[:parameter_mapping] = mapping[:parameter_mapping] || Hash.new

# if they specify custom attributes, ensure that AR ids are being passed as well if they opt for it.
if force_active_record_ids && mapping[:attributes] && mapping[:type]=="active_record" && !mapping[:attributes].include?("id")
mapping[:attributes] << "id"
# if they specify custom attributes, ensure that AR primary_keys are being passed as well if they opt for it.
if force_active_record_ids && mapping[:attributes] && mapping[:type]=="active_record"
rubyobj = mapping[:ruby].constantize.new
if !mapping[:attributes].include?(rubyobj.class.primary_key)
mapping[:attributes] << rubyobj.class.primary_key
end
end

# created caching hashes for mapping
Expand All @@ -51,12 +55,13 @@ def register(mapping) #register a value object map
# for deserialization - looking up in a hash is faster than looking up in an array.
begin
if mapping[:type] == "active_record"
@attribute_names[mapping[:ruby]] = (mapping[:ruby].constantize.new.attribute_names + ["id"]).inject({}){|hash, attr| hash[attr]=true ; hash} # include the id attribute
rubyobj = mapping[:ruby].constantize.new
@attribute_names[mapping[:ruby]] = (rubyobj.attribute_names + [rubyobj.class.primary_key]).inject({}){|hash, attr| hash[attr]=true ; hash} # include the id attribute
end
rescue StandardError => e
rescue ActiveRecord::StatementInvalid => e
# This error occurs during migrations, since the AR constructed above will check its columns, but the table won't exist yet.
# We'll ignore the error if we're migrating.
raise unless ARGV.include?("migrate") or ARGV.include?("db:migrate") or ARGV.include?("rollback") or ARGV.include?("db:rollback")
raise unless ARGV.include?("migrate") or ARGV.include?("db:migrate")
end
end

Expand All @@ -65,7 +70,7 @@ def get_vo_mapping_for_ruby_class(ruby_class)
scoped_class_mapping[@current_mapping_scope] ||= (if vo_mapping = @class_mappings_by_ruby_class[ruby_class]
vo_mapping = vo_mapping.dup # need to duplicate it or else we will overwrite the keys from the original mappings
vo_mapping[:attributes] = vo_mapping[:attributes][@current_mapping_scope]||[] if vo_mapping[:attributes].is_a?(Hash) # don't include any of these attributes if there is no scope
vo_mapping[:associations] = vo_mapping[:associations][@current_mapping_scope]||[] if vo_mapping[:associations].is_a?(Hash) # don't include any of these attributes
vo_mapping[:associations] = vo_mapping[:associations][@current_mapping_scope]||[] if vo_mapping[:associations].is_a?(Hash) # don't include any of these attributes
vo_mapping
end
)
Expand Down Expand Up @@ -98,7 +103,7 @@ def update_request_parameters(controller_class_name, controller_action_name, req
if scaffolding && val.is_a?(ActiveRecord::Base)
request_params[k.to_sym] = val.attributes.dup
val.instance_variables.each do |assoc|
next if "@new_record" == assoc
next if :@new_record == assoc.to_sym
request_params[k.to_sym][assoc[1..-1]] = val.instance_variable_get(assoc)
end
else
Expand All @@ -114,7 +119,7 @@ def update_request_parameters(controller_class_name, controller_action_name, req
key = first.class.to_s.to_snake!.downcase.to_sym # a generated scaffold expects params in snake_case, rubyamf_params gets them for consistency in scaffolding
rubyamf_params[key] = first.attributes.dup
first.instance_variables.each do |assoc|
next if "@new_record" == assoc
next if :@new_record == assoc.to_sym
rubyamf_params[key][assoc[1..-1]] = first.instance_variable_get(assoc)
end
if always_add_to_params #if wanted in params, put it in
Expand Down
10 changes: 5 additions & 5 deletions app/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def run(amfobj)
if (auth_header = amfobj.get_header_by_key('Credentials'))
RequestStore.auth_header = auth_header #store the auth header for later
case ClassMappings.hash_key_access
when :string:
when :string then
auth = {'username' => auth_header.value['userid'], 'password' => auth_header.value['password']}
when :symbol:
when :symbol then
auth = {:username => auth_header.value['userid'], :password => auth_header.value['password']}
when :indifferent:
when :indifferent then
auth = HashWithIndifferentAccess.new({:username => auth_header.value['userid'], :password => auth_header.value['password']})
end
RequestStore.rails_authentication = auth
Expand All @@ -66,7 +66,7 @@ def run(amfobj)
begin #this is where any exception throughout the RubyAMF Process gets transformed into a relevant AMF0/AMF3 faultObject
# action.run(body)
seconds = Benchmark.realtime{ action.run(body) }
puts ">>>>>>>> RubyAMF >>>>>>>>> #{action} took: #{'%.5f' % seconds} secs"
#puts ">>>>>>>> RubyAMF >>>>>>>>> #{action} took: #{'%.5f' % seconds} secs"
rescue RUBYAMFException => ramfe
puts ramfe.message
puts ramfe.backtrace
Expand All @@ -89,7 +89,7 @@ class AMFSerializeFilter
def run(amfobj)
# AMFSerializer.new(amfobj).run
seconds = Benchmark.realtime{ AMFSerializer.new(amfobj).run }
puts ">>>>>>>> RubyAMF >>>>>>>>> Serialization took: #{'%.5f' % seconds} secs"
# puts ">>>>>>>> RubyAMF >>>>>>>>> Serialization took: #{'%.5f' % seconds} secs"
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions app/mime_type.rb

This file was deleted.

7 changes: 2 additions & 5 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
$:.unshift(File.expand_path(RAILS_ROOT) + '/vendor/plugins/ruby_amf/')
$:.unshift(File.expand_path(RAILS_ROOT) + '/vendor/plugins/rubyamf/')

#utils must be first
require 'util/string'
require 'util/vo_helper'
require 'util/active_record'
require 'util/action_controller'
require 'app/mime_type'
require 'app/fault_object'
require 'app/rails_gateway'
require File.expand_path(RAILS_ROOT) + '/config/rubyamf_config' #run the configuration


require File.expand_path(RAILS_ROOT) + '/config/rubyamf_config' #run the configuration
16 changes: 9 additions & 7 deletions install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
begin
require 'fileutils'
overwrite = true


install_files_root = File.join( File.dirname(__FILE__), "rails_installer_files")

if !File.exist?('./config/rubyamf_config.rb')
FileUtils.copy_file("./vendor/plugins/ruby_amf/rails_installer_files/rubyamf_config.rb", "./config/rubyamf_config.rb", false)
FileUtils.copy_file( File.join(install_files_root, "rubyamf_config.rb", "./config/rubyamf_config.rb", false)
end

FileUtils.copy_file("./vendor/plugins/ruby_amf/rails_installer_files/rubyamf_controller.rb","./app/controllers/rubyamf_controller.rb",false)
FileUtils.copy_file("./vendor/plugins/ruby_amf/rails_installer_files/rubyamf_helper.rb","./app/helpers/rubyamf_helper.rb",false)
FileUtils.copy_file("./vendor/plugins/ruby_amf/rails_installer_files/crossdomain.xml","./public/crossdomain.xml", false)
FileUtils.copy_file( File.join(install_files_root, "rubyamf_controller.rb","./app/controllers/rubyamf_controller.rb",false)
FileUtils.copy_file( File.join(install_files_root, "rubyamf_helper.rb","./app/helpers/rubyamf_helper.rb",false)
FileUtils.copy_file( File.join(install_files_root, "crossdomain.xml","./public/crossdomain.xml", false)

mime = true
mime_types_file_exists = File.exists?('./config/initializers/mime_types.rb')
Expand All @@ -33,7 +35,7 @@
route_amf_controller = true
File.open('./config/routes.rb', 'r') do |f|
while line = f.gets
if line.match("map.rubyamf_gateway 'amf', :controller => 'rubyamf', :action => 'gateway")
if line.match("map.rubyamf_gateway 'rubyamf_gateway', :controller => 'rubyamf', :action => 'gateway")
route_amf_controller = false
break
end
Expand All @@ -43,7 +45,7 @@
if route_amf_controller
routes = File.read('./config/routes.rb')
updated_routes = routes.gsub(/(ActionController::Routing::Routes.draw do \|map\|)/) do |s|
"#{$1}\n map.rubyamf_gateway 'amf', :controller => 'rubyamf', :action => 'gateway'\n"
"#{$1}\n map.rubyamf_gateway 'rubyamf_gateway', :controller => 'rubyamf', :action => 'gateway'\n"
end
File.open('./config/routes.rb', 'w') do |file|
file.write updated_routes
Expand Down
4 changes: 2 additions & 2 deletions io/amf_deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ def read_amf3_string
#thanks Karl von Randow for this
if length > 0
str = String.new(readn(length)) #specifically cast as string, as we're reading verbatim from the stream
str.toutf8 #convert to utf8
#convert to utf8 - Ruby 1.9 compatibility
defined? str.toutf8 ? str.toutf8 : (str.respond_to?(:force_encoding) ? str.force_encoding("UTF-8") : str)
@stored_strings << str
end
return str
Expand Down Expand Up @@ -375,7 +376,6 @@ def read_amf3_object
@stored_defs << class_definition
end
action_class_name = class_definition['as_class_name'] #get the className according to type

# check to see if its the first main amf object or a flex message obj, because then we need a _explicitType field type and skip some things
skip_mapping = if action_class_name && action_class_name.include?("flex.messaging")
obj = VoHash.new # initialize an empty VoHash value holder
Expand Down
2 changes: 1 addition & 1 deletion io/amf_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def write_amf3_array(array)
write_amf3_string("flex.messaging.io.ArrayCollection")
@stored_objects_count += 1
end
store_object array
store_object array.object_id
@stream << "\t" # represents an amf3 array
write_amf3_integer(array.length << 1 | 1)
@stream << "\001" # represents an amf3 empty string #write empty for string keyed elements here, as it's never allowed from ruby
Expand Down
36 changes: 18 additions & 18 deletions util/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ def amf_credentials
end

#remoteObject setRemoteCredentials retrieval
def html_credentials
auth_data = request.env['RAW_POST_DATA']
auth_data = auth_data.scan(/DSRemoteCredentials\006.([A-Za-z0-9\+\/=]*).*?\006/)[0][0]
auth_data.gsub!("DSRemoteCredentialsCharset", "")
if auth_data.size > 0

remote_auth = Base64.decode64(auth_data).split(':')[0..1]
else
return nil
end
case RubyAMF::Configuration::ClassMappings.hash_key_access
when :string:
return {'username' => remote_auth[0], 'password' => remote_auth[1]}
when :symbol:
return {:username => remote_auth[0], :password => remote_auth[1]}
when :indifferent:
return HashWithIndifferentAccess.new({:username => remote_auth[0], :password => remote_auth[1]})
end
def html_credentials
auth_data = request.env['RAW_POST_DATA']
auth_data = auth_data.scan(/DSRemoteCredentials\006.([A-Za-z0-9\+\/=]*).*?\006/)[0][0]
auth_data.gsub!("DSRemoteCredentialsCharset", "")
if auth_data.size > 0

remote_auth = Base64.decode64(auth_data).split(':')[0..1]
else
return nil
end
case RubyAMF::Configuration::ClassMappings.hash_key_access
when :string then
return {'username' => remote_auth[0], 'password' => remote_auth[1]}
when :symbol then
return {:username => remote_auth[0], :password => remote_auth[1]}
when :indifferent then
return HashWithIndifferentAccess.new({:username => remote_auth[0], :password => remote_auth[1]})
end
end
end
2 changes: 1 addition & 1 deletion util/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class String
def to_snake! # no one should change these unless they can benchmark and prove their way is faster. =)
@cached_snake_strings ||= {}
@cached_snake_strings[self] ||= (
while x = index(/([a-z\d])([A-Z\d])/) # unfortunately have to use regex for this one
while x = index(/([a-z\d])([A-Z])/) # unfortunately have to use regex for this one
y=x+1
self[x..y] = self[x..x]+"_"+self[y..y].downcase
end
Expand Down
Loading

0 comments on commit 1835dec

Please sign in to comment.