Skip to content

Commit

Permalink
refactor for ruru update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpclark committed Feb 20, 2018
1 parent bea7793 commit 189a8e7
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 245 deletions.
95 changes: 23 additions & 72 deletions lib/faster_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,13 @@ module FasterPath
new(Fiddle.dlopen(FFI_LIBRARY)['Init_faster_pathname'], [], Fiddle::TYPE_VOIDP).
call

FasterPathname::Public.class_eval do
private_class_method :absolute?
private_class_method :add_trailing_separator
Public.class_eval do
private_class_method :basename
private_class_method :children # String results
private_class_method :children_compat # wrap Pathname on each
private_class_method :children
private_class_method :children_compat
private_class_method :chop_basename
private_class_method :cleanpath_aggressive
private_class_method :directory?
private_class_method :dirname
private_class_method :entries # String results
private_class_method :entries_compat # wrap Pathname on each
private_class_method :extname
private_class_method :has_trailing_separator?
private_class_method :plus
private_class_method :relative?
end

FasterPathname.class_eval do
def initialize(arg)
unless arg.is_a? String
arg = arg.to_s
end
raise(ArgumentError, "null byte found") if arg.include?("\0")
@path = arg
end

# BAD; exposes private methods
# Need to reprivatize specific methods
def method_missing(method_name, *a, &b)
Public.send(method_name, @path, *a, &b)
end

def respond_to?(method_name, include_private = false)
Public.send(:respond_to?, method_name) || super
end
private_class_method :entries
private_class_method :entries_compat
end

def self.rust_arch_bits
Expand All @@ -58,61 +29,41 @@ def self.ruby_arch_bits
1.size * 8
end

def self.absolute?(pth)
FasterPathname::Public.send(:absolute?, pth)
end

def self.add_trailing_separator(pth)
FasterPathname::Public.send(:add_trailing_separator, pth)
end

def self.blank?(str)
"#{str}".strip.empty?
end

def self.basename(pth, ext="")
FasterPathname::Public.send(:basename, pth, ext)
Public.send(:basename, pth, ext)
end

def self.children(pth, with_directory=true)
FasterPathname::Public.send(:children, pth, with_directory)
end

def self.chop_basename(pth)
result = FasterPathname::Public.send(:chop_basename, pth)
result unless result.empty?
end

def self.cleanpath_aggressive(pth)
FasterPathname::Public.send(:cleanpath_aggressive, pth)
result = Public.send(:children, pth, with_directory)
return result if result
raise Errno::NOENT, "No such file or directory @ dir_initialize - #{pth}"
end

def self.directory?(pth)
FasterPathname::Public.send(:directory?, pth)
def self.children_compat(pth, with_directory=true)
result = Public.send(:children_compat, pth, with_directory)
return result if result
raise Errno::NOENT, "No such file or directory @ dir_initialize - #{pth}"
end

def self.dirname(pth)
FasterPathname::Public.send(:dirname, pth)
def self.chop_basename(pth)
result = Public.send(:chop_basename, pth)
result unless result.empty?
end

def self.entries(pth)
FasterPathname::Public.send(:entries, pth)
end

def self.extname(pth)
FasterPathname::Public.send(:extname, pth)
end

def self.has_trailing_separator?(pth)
FasterPathname::Public.send(:has_trailing_separator?, pth)
end

def self.plus(pth, pth2)
FasterPathname::Public.send(:plus, pth, pth2)
result = Public.send(:entries, pth)
return result if result
raise Errno::NOENT, "No such file or directory @ dir_initialize - #{pth}"
end

def self.relative?(pth)
FasterPathname::Public.send(:relative?, pth)
def self.entries_compat(pth)
result = Public.send(:entries_compat, pth)
return result if result
raise Errno::NOENT, "No such file or directory @ dir_initialize - #{pth}"
end

module Rust
Expand Down
4 changes: 2 additions & 2 deletions lib/faster_path/optional/monkeypatches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def add_trailing_separator(pth)
private :add_trailing_separator

def children(with_dir=true)
FasterPathname::Public.send(:children_compat, @path, with_dir)
FasterPath.children_compat(@path, with_dir)
end if !!ENV['WITH_REGRESSION']

def chop_basename(pth)
Expand All @@ -57,7 +57,7 @@ def directory?
end

def entries
FasterPathname::Public.send(:entries_compat, @path)
FasterPath.entries_compat(@path)
end if !!ENV['WITH_REGRESSION']

def has_trailing_separator?(pth)
Expand Down
4 changes: 2 additions & 2 deletions lib/faster_path/optional/refinements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def add_trailing_separator(pth)
private :add_trailing_separator

def children(with_dir=true)
FasterPathname::Public.send(:children_compat, @path, with_dir)
FasterPath.children_compat(@path, with_dir)
end if !!ENV['WITH_REGRESSION']

def chop_basename(pth)
Expand All @@ -53,7 +53,7 @@ def directory?
end

def entries
FasterPathname::Public.send(:entries_compat, @path)
FasterPath.entries_compat(@path)
end if !!ENV['WITH_REGRESSION']

def has_trailing_separator?(pth)
Expand Down
40 changes: 19 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate ruru;
#[macro_use]
extern crate lazy_static;

class!(FasterPathname);
module!(FasterPath);

mod helpers;
mod pathname;
Expand All @@ -24,12 +24,12 @@ mod prepend_prefix;
pub mod rust_arch_bits;
mod path_parsing;

use ruru::{Class, Object, RString, Boolean, Array};
use ruru::{Module, Object, RString, Boolean, Array, AnyObject};

// r_ methods are on the core class and may evaluate instance variables or self
// pub_ methods must take all values as parameters
methods!(
FasterPathname,
FasterPath,
_itself,

fn pub_add_trailing_separator(pth: RString) -> RString {
Expand All @@ -46,11 +46,11 @@ methods!(
pathname::pn_basename(pth, ext)
}

fn pub_children(pth: RString, with_dir: Boolean) -> Array {
fn pub_children(pth: RString, with_dir: Boolean) -> AnyObject {
pathname::pn_children(pth, with_dir)
}

fn pub_children_compat(pth: RString, with_dir: Boolean) -> Array {
fn pub_children_compat(pth: RString, with_dir: Boolean) -> AnyObject {
pathname::pn_children_compat(pth, with_dir)
}

Expand Down Expand Up @@ -87,12 +87,12 @@ methods!(
// }

// pub_entries returns an array of String objects
fn pub_entries(pth: RString) -> Array {
fn pub_entries(pth: RString) -> AnyObject {
pathname::pn_entries(pth)
}

// pub_entries_compat returns an array of Pathname objects
fn pub_entries_compat(pth: RString) -> Array {
fn pub_entries_compat(pth: RString) -> AnyObject {
pathname::pn_entries_compat(pth)
}

Expand Down Expand Up @@ -146,28 +146,26 @@ methods!(
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_faster_pathname(){
Class::new("FasterPathname", None).define(|itself| {
itself.define_nested_class("Public", None);
});

// Public methods
// * methods for refinements, monkeypatching
// * methods that need all values as parameters
Class::from_existing("FasterPathname").get_nested_class("Public").define(|itself| {
Module::from_existing("FasterPath").define(|itself| {
itself.def_self("absolute?", pub_is_absolute);
itself.def_self("add_trailing_separator", pub_add_trailing_separator);
itself.def_self("basename", pub_basename);
itself.def_self("children", pub_children);
itself.def_self("children_compat", pub_children_compat);
itself.def_self("chop_basename", pub_chop_basename);
itself.def_self("cleanpath_aggressive", pub_cleanpath_aggressive);
itself.def_self("directory?", pub_is_directory);
itself.def_self("dirname", pub_dirname);
itself.def_self("entries", pub_entries);
itself.def_self("entries_compat", pub_entries_compat);
itself.def_self("extname", pub_extname);
itself.def_self("has_trailing_separator?", pub_has_trailing_separator);
itself.def_self("plus", pub_plus);
itself.def_self("relative?", pub_is_relative);
itself.define_nested_class("Public", None);
});

// For methods requiring addition Ruby-side behavior
Module::from_existing("FasterPath").get_nested_class("Public").define(|itself| {
itself.def_self("basename", pub_basename);
itself.def_self("children", pub_children);
itself.def_self("children_compat", pub_children_compat);
itself.def_self("chop_basename", pub_chop_basename);
itself.def_self("entries", pub_entries);
itself.def_self("entries_compat", pub_entries_compat);
});
}
Loading

0 comments on commit 189a8e7

Please sign in to comment.