This is a port of the parts of ActiveSupport that make sense for RubyMotion.
To see what's there, generate the documentation with the rdoc
command from the repository root, or look into the lib folder. Almost everything is tested.
Install with
gem install motion-support
or add to your Gemfile
gem 'motion-support'
It is also possible to only use parts of this library. To do so, change your Gemfile
so that it reads:
gem 'motion-support', :require => false
Then add a require statement as shown below to your Rakefile
.
require 'motion-support'
Loads everything.
require 'motion-support/callbacks'
Loads the MotionSupport::Callbacks
module. It allows you to easily add Rails-style callbacks to any class.
require 'motion-support/concern'
Loads the MotionSupport::Concern
module. This simplifies separating classes into modules and managing module dependencies.
require 'motion-support/inflector'
Loads the Inflector
module and extensions to the String
class. See the "Inflector" app in the examples/
folder for what the inflector can do.
Example usage include:
"app_delegate".camelize
=> "AppDelegate"
"HomeController".constantize
=> HomeController
"thing".pluralize
=> "things"
"mice".singularize
=> "mouse"
require 'motion-support/core_ext'
Loads all the extensions to core classes.
require 'motion-support/core_ext/array'
Loads extensions to class Array
. Example usage include
%w( a b c d ).from(2)
=> ["c", "d"]
['one', 'two', 'three'].to_sentence
=> "one, two, and three"
[1, 2, 3, 4].in_groups_of(2)
=> [[1, 2], [3, 4]]
Extract options hash from variant args array
args = ['hello', 'world', { :foo => 'bar' }]
args.extract_options!
=> { :foo => 'bar' }
require 'motion-support/core_ext/class'
Loads extensions to class Class
.
class Foo
cattr_accessor :bar
class_attribute :foo
end
require 'motion-support/core_ext/hash'
Loads extensions to class Hash
, including class HashWithIndifferentAccess
.
{ 'foo' => 'bar', 'baz' => 'bam' }.symbolize_keys
=> { :foo => 'bar', :baz => 'bam' }
{ 'foo' => 'bar', 'baz' => 'bam' }.except('foo')
=> { 'baz' => 'bam' }
{ 'foo' => 'bar', 'baz' => 'bam' }.with_indifferent_access
=> #<HashWithIndifferentAccess>
require 'motion-support/core_ext/integer'
Loads extensions to class Integer
.
1.ordinalize
=> "1st"
3.ordinalize
=> "3rd"
require 'motion-support/core_ext/module'
Loads extensions to class Module
.
module Mod
mattr_accessor :foo, :bar
attr_internal :baz
delegate :boo, :to => :foo
remove_method :baz
end
require 'motion-support/core_ext/numeric'
Loads extensions to class Numeric
.
10.kilobytes
=> 10240
2.megabytes
=> 2097152
3.days
=> 3
require 'motion-support/core_ext/object'
Loads extensions to class Object
.
nil.blank?
=> true
Object.new.blank?
=> false
{ "hello" => "world", "foo" => "bar" }.to_query
=> "hello=world&foo=bar"
1.duplicable?
=> false
1.try(:to_s)
=> "1"
nil.try(:to_s)
=> nil
require 'motion-support/core_ext/range'
Loads extensions to class Range
.
(1..5).overlaps(3..9)
=> true
(1..5).include?(2..3)
=> true
require 'motion-support/core_ext/string'
Loads extensions to class String
.
"ruby_motion".camelize
=> "RubyMotion"
"User".pluralize
=> "Users"
"mice".singularize
=> "mouse"
"Foo::Bar".underscore
=> "foo/bar"
"AppDelegate".underscore
=> "app_delegate"
"UIView".constantize
=> UIView
require 'motion-support/core_ext/time'
Loads extensions to class Time
.
1.week.ago
17.days.from_now
Date.yesterday
Time.beginning_of_week
In general:
I18n
stuff was removed. Maybe it will be readded later.TimeWithZone
class (iOS apps don't need advanced time zone support)DateTime
classYAML
extensions were removed, since RubyMotion doesn't come with YAML supportKernel#silence_warnings
and stream manipulation methods were removedTest::Unit
were removedActiveSupport
namespace is called MotionSupport
Specifically:
Array#third
.. Array#fourty_two
were removedArray#to_xml
is missingArray#to_sentence
does not accept a :locale
parameterClass#subclasses
is missing. It depends on ObjectSpace.each_object
which is missing in RubyMotionHash#extractable_options?
is missingBigDecimal
conversions were removedTime.current
an alias for Time.now
Date.current
an alias for Date.today
Date#to_time
does not accept a timezone form (:local
, :utc
)Date#xmlschema
is missingString#parameterize
is missing because it needs to transliterate the string, which is dependent on the localeString#constantize
is very slow. Cache the result if you use it.String#to_time
, #to_date
, #to_datetime
are missing because they depend on Date#_parse
String#html_safe
and ERB
extensions are not needed in RubyMotionRange#to_s(:db)
was removedrfc822
time format was removed, since it relies on time zone support.LoadError
and NameError
were removedThreadSafe
versions of Hash
and Array
are just aliases to the standard classesMotionSupport::Callbacks
it is not possible to give a string containing Ruby code as a conditions via :if
, since RubyMotion doesn't support eval
.MotionSupport::Callbacks
, if a proc is given as a condition to :if
, the block must take the class instance as a parameter. self
is not automatically set.MotionSupport::Callbacks#define_callbacks
, the :terminator
argument must take a block of the form lambda { |result| }
where result
is the intermediate result from the callback methods.NumberHelper
and numeric conversions only support phone numbers for nowThings to do / to decide:
Date
class. in _stdlib
there is a stub of a Date class that makes the Date
extensions work. This stub needs to be completed.Object#to_xml
Hash#from_xml
Hash#extractable_options?
?Class#superclass_delegating_accessor
?InfiniteComparable
extensionsFile#atomic_write
for thread-safe file writing?Kernel#class_eval
(it delegates to class_eval
on this' singleton class)?Module#qualified_const_defined?
? What is that even for?Module#synchronize
?Numeric
conversions, especially NumberHelper
Object#with_options
?String#to_time
, String#to_date
. In the original ActiveSupport, they make use of the Date#_parse
method (which seems like it's supposed to be private). Here, they should be implemented on top of Cocoa APIsString#parameterize
? If so, we need to find a way to transliterate UTF-8 characters.StringInquirer
? It allows things like Rails.env.development?
instead of Rails.env == 'development'
Struct#to_h
?Thread
if they make sense.Configurable
module?OrderedOptions
class?NSArray
, NSDictionary
etc.), since we want to effectively handle return values from Objective-C methods (they return Cocoa classes). The way to do this is to write a conversion method from Cocoa class to Ruby class and delegate the extension methods in the Cocoa class to the conversion method. See motion/core_ext/ns_string.rb
for an example.ActiveSupport was originally written as part of Ruby on Rails by David Heinemeier Hansson. Over the years, countless contributors made many additions. They made this library possible.
Feel free to fork and submit pull requests!