womb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/lib/womb.rb +41 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 210d82c5031506a430336295cce6651d36b5e45a
|
4
|
+
data.tar.gz: 80342be41a52d99acd6778ad7b3cf53c067786ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f993ada4bd2e423ef88c0908eccd1b5a806d2cea4a7c704f18d6c67e8cac286ca4097da279d150b113c8f0b1e025ab02971e0b4ae3e404a66731b58b0e782b0
|
7
|
+
data.tar.gz: bc965594074750e3b27f2bb7341b591a853b230f478e45f3350ff3d3058218fa24bc54d0c2225378355572abcd70caaa12a881d117b409b2b47c0f523e268d79
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Max White
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# Womb
|
2
|
+
|
3
|
+
`Womb` wraps around an object to present a chainable interface for defining
|
4
|
+
objects. It also provides short aliases for longer method names and helpers to
|
5
|
+
reduce boiler plate.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Once wrapped around an object, `Womb` forwards messages via `method_missing` to
|
10
|
+
the object. All forwarded messages are sent with the `send` method, such that
|
11
|
+
normally private methods such as `attr_accessor` and `define_method` can be
|
12
|
+
used. After the object has been defined, the `birth` method returns the
|
13
|
+
object.
|
14
|
+
|
15
|
+
``` ruby
|
16
|
+
require 'womb'
|
17
|
+
|
18
|
+
pi = 3.14
|
19
|
+
|
20
|
+
Circle = Womb[Class.new]
|
21
|
+
.attr_reader(:radius)
|
22
|
+
.define_method(:initialize) { |radius| @radius = radius }
|
23
|
+
.define_method(:circumference) { 2 * pi * radius }
|
24
|
+
.define_method(:area) { pi * radius ** 2 }
|
25
|
+
.birth
|
26
|
+
|
27
|
+
Circle.new(3).area # 28.26
|
28
|
+
```
|
29
|
+
|
30
|
+
For shorthand, `def` and `assign` can be used as aliases for `define_method`
|
31
|
+
and `define_singleton_method` respectively, and `init` can be used to create
|
32
|
+
an `initialize` method that simply sets instance variables from it's arguments.
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
RegPolygon = Womb[Module.new]
|
36
|
+
.assign(:angle) { |sides| (sides - 2) * Math::PI / sides }
|
37
|
+
.birth
|
38
|
+
|
39
|
+
Triangle = Womb[Module.new]
|
40
|
+
.assign(:area) { |base, height| base * height / 2 }
|
41
|
+
.birth
|
42
|
+
|
43
|
+
RegTriangle = Womb[Class.new]
|
44
|
+
.init(:base)
|
45
|
+
.def(:height) { @base * Math.sin(RegPolygon.angle(3)) }
|
46
|
+
.def(:area) { Triangle.area(@base, height) }
|
47
|
+
.birth
|
48
|
+
|
49
|
+
RegTriangle.new(2).area # 1.7320508075688772
|
50
|
+
```
|
51
|
+
|
52
|
+
An additional helper, `send_to_singleton`, can be used to send messages to the
|
53
|
+
object's singleton class.
|
54
|
+
|
55
|
+
``` ruby
|
56
|
+
Matrix = Womb[Class.new]
|
57
|
+
.init(:v1, :v2)
|
58
|
+
.attr_reader(:v1, :v2)
|
59
|
+
.send_to_singleton(:alias_method, :[], :new)
|
60
|
+
.def(:determinant) { v1[0] * v2[1] - v2[0] * v1[1] }
|
61
|
+
.birth
|
62
|
+
|
63
|
+
Matrix[[2, 2], [1, 3]].determinant # 4
|
64
|
+
```
|
65
|
+
|
66
|
+
If you prefer not to define a method with a block directly, it can defined as
|
67
|
+
a lambda and passed in using the `to_proc` operator, `&`. The objects private
|
68
|
+
state can still be accessed with instance variables.
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
move = ->(dist, angle) do
|
72
|
+
angle = angle * Math::PI / 180
|
73
|
+
@x += dist * Math.cos(angle)
|
74
|
+
@y += dist * Math.sin(angle)
|
75
|
+
puts self
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
Turtle = Womb[Class.new]
|
80
|
+
.init(:x, :y)
|
81
|
+
.def(:move, &move)
|
82
|
+
.def(:to_s) { "Turtle (#{@x.round}, #{@y.round})" }
|
83
|
+
.birth
|
84
|
+
|
85
|
+
Turtle.new(0, 0).move(1, 0).move(2, 90)
|
86
|
+
# Turtle (1, 0)
|
87
|
+
# Turtle (1, 2)
|
88
|
+
```
|
89
|
+
|
90
|
+
## Installation
|
91
|
+
|
92
|
+
Add your Gemfile:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
gem 'womb'
|
96
|
+
```
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
1. [Fork it]( https://github.com/mushishi78/womb/fork)
|
101
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
102
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
103
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
104
|
+
5. Create a new Pull Request
|
data/lib/womb.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
class Womb < BasicObject
|
2
|
+
def self.[](obj)
|
3
|
+
new(obj)
|
4
|
+
end
|
5
|
+
|
6
|
+
def initialize(obj)
|
7
|
+
@obj = obj
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(name, *args, &b)
|
11
|
+
obj.send(aliases[name] || name, *args, &b)
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def init(*attributes)
|
16
|
+
define_method(:initialize) do |*values|
|
17
|
+
attributes.each_with_index do |attribute, i|
|
18
|
+
instance_variable_set("@#{attribute}", values[i])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def send_to_singleton(*args)
|
24
|
+
obj.singleton_class.send(*args)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def birth
|
29
|
+
@obj
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :obj
|
35
|
+
|
36
|
+
def aliases
|
37
|
+
@aliases ||= { assign: :define_singleton_method,
|
38
|
+
def: :define_method }
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: womb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max White
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.1.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.1.0
|
33
|
+
description:
|
34
|
+
email: mushishi78@gmail.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- lib/womb.rb
|
42
|
+
homepage: https://github.com/mushishi78/womb
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.4.8
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: A Ruby library for birthing objects
|
66
|
+
test_files: []
|