Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private method and module function #82

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ keywords = ["cruby", "mri", "ruby", "ruru"]
license = "MIT"

[dependencies]
ruby-sys = "0.3.0"
ruby-sys = { git = "https://github.com/danielpclark/ruby-sys", branch = "playground" }
lazy_static = "0.2.1"
8 changes: 8 additions & 0 deletions src/binding/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ pub fn define_method<I: Object, O: Object>(klass: Value, name: &str, callback: C
}
}

pub fn define_private_method<I: Object, O: Object>(klass: Value, name: &str, callback: Callback<I, O>) {
let name = util::str_to_cstring(name);

unsafe {
class::rb_define_private_method(klass, name.as_ptr(), callback as CallbackPtr, -1);
}
}

pub fn define_singleton_method<I: Object, O: Object>(
klass: Value,
name: &str,
Expand Down
1 change: 1 addition & 0 deletions src/binding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod float;
pub mod gc;
pub mod global;
pub mod hash;
pub mod module;
pub mod rproc;
pub mod string;
pub mod symbol;
Expand Down
44 changes: 44 additions & 0 deletions src/binding/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use ruby_sys::class;

use binding::global::rb_cObject;
use binding::util as binding_util;
use types::{Value, Callback, CallbackPtr};
use util;

use Object;

pub fn define_module(name: &str) -> Value {
let name = util::str_to_cstring(name);

unsafe { class::rb_define_module(name.as_ptr()) }
}

pub fn define_nested_module(outer: Value, name: &str) -> Value {
let name = util::str_to_cstring(name);

unsafe { class::rb_define_module_under(outer, name.as_ptr()) }
}

pub fn define_module_function<I: Object, O: Object>(klass: Value, name: &str, callback: Callback<I, O>) {
let name = util::str_to_cstring(name);

unsafe {
class::rb_define_module_function(klass, name.as_ptr(), callback as CallbackPtr, -1);
}
}

pub fn include_module(klass: Value, module: &str) {
let object_module = unsafe { rb_cObject };

let module_value = binding_util::get_constant(module, object_module);

unsafe { class::rb_include_module(klass, module_value) };
}

pub fn prepend_module(klass: Value, module: &str) {
let object_module = unsafe { rb_cObject };

let module_value = binding_util::get_constant(module, object_module);

unsafe { class::rb_prepend_module(klass, module_value) };
}
110 changes: 108 additions & 2 deletions src/class/class.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::convert::From;

use binding::class;
use binding::{class, module};
use binding::global::rb_cObject;
use binding::util as binding_util;
use typed_data::DataTypeWrapper;
use types::{Value, ValueType};
use util;

use {AnyObject, Array, Object, VerifiedObject};
use {AnyObject, Array, Object, Module, VerifiedObject};

/// `Class`
///
Expand Down Expand Up @@ -277,6 +277,39 @@ impl Class {
Self::from(binding_util::get_constant(name, self.value()))
}

/// Retrieves a `Module` nested to current `Class`.
///
/// # Examples
///
/// ```
/// use ruru::{Class, Module, Object, VM};
/// # VM::init();
///
/// Class::new("Outer", None).define(|itself| {
/// itself.define_nested_module("Inner");
/// });
///
/// Class::from_existing("Outer").get_nested_module("Inner");
/// ```
///
/// Ruby:
///
/// ```ruby
/// class Outer
/// module Inner
/// end
/// end
///
/// Outer::Inner
///
/// # or
///
/// Outer.const_get('Inner')
/// ```
pub fn get_nested_module(&self, name: &str) -> Module {
Module::from(binding_util::get_constant(name, self.value()))
}

/// Creates a new `Class` nested into current class.
///
/// `superclass` can receive the following values:
Expand Down Expand Up @@ -318,6 +351,39 @@ impl Class {
Self::from(class::define_nested_class(self.value(), name, superclass))
}

/// Creates a new `Module` nested into current `Class`.
///
/// # Examples
///
/// ```
/// use ruru::{Class, Module, Object, VM};
/// # VM::init();
///
/// Class::new("Outer", None).define(|itself| {
/// itself.define_nested_module("Inner");
/// });
///
/// Module::from_existing("Outer").get_nested_module("Inner");
/// ```
///
/// Ruby:
///
/// ```ruby
/// class Outer
/// module Inner
/// end
/// end
///
/// Outer::Inner
///
/// # or
///
/// Outer.const_get('Inner')
/// ```
pub fn define_nested_module(&mut self, name: &str) -> Module {
Module::from(module::define_nested_module(self.value(), name))
}

/// Retrieves a constant from class.
///
/// # Examples
Expand Down Expand Up @@ -408,6 +474,46 @@ impl Class {
class::const_set(self.value(), name, value.value());
}

/// Includes module into current class
///
/// # Examples
///
/// ```
/// use ruru::{Class, Module, VM};
/// # VM::init();
///
/// let a_module = Module::new("A");
/// Class::new("B", None).include("A");
///
/// let b_class_ancestors = Class::from_existing("B").ancestors();
/// let expected_ancestors = vec![Module::from_existing("A")];
///
/// assert!(expected_ancestors.iter().any(|anc| *anc == a_module));
/// ```
pub fn include(&self, md: &str) {
module::include_module(self.value(), md);
}

/// Prepends module into current class
///
/// # Examples
///
/// ```
/// use ruru::{Class, Module, VM};
/// # VM::init();
///
/// let a_module = Module::new("A");
/// Class::new("B", None).prepend("A");
///
/// let b_class_ancestors = Class::from_existing("B").ancestors();
/// let expected_ancestors = vec![Module::from_existing("A")];
///
/// assert!(expected_ancestors.iter().any(|anc| *anc == a_module));
/// ```
pub fn prepend(&self, md: &str) {
module::prepend_module(self.value(), md);
}

/// Defines an `attr_reader` for class
///
/// # Examples
Expand Down
1 change: 1 addition & 0 deletions src/class/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod float;
pub mod gc;
pub mod hash;
pub mod integer;
pub mod module;
pub mod nil_class;
pub mod rproc;
pub mod string;
Expand Down
Loading