condensable 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -0
- data/lib/condensable.rb +26 -0
- data/lib/condensable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2af3831d03fa1d430e4dea63b2c15d1eea4d4b8a
|
4
|
+
data.tar.gz: b097074c10e9a8cc3d2ff526289df584d532d5c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cdf20b8e7cd4850daf53cb48c34167af2011080576f16fdaf34fd1c9116b9527e10fea20788c016f67e52360db8e7bb5c84f9613d94158dbe2f40d2b93ad5d8
|
7
|
+
data.tar.gz: 59fe8e5c81cb002d5444cb8dd46f017b5f66e0cc737464a5461f138eef03d09aefa79444aa8c7728944a9575a5ec26b4a71e49d7e467ddc8fb619a5273dac117
|
data/README.md
CHANGED
@@ -46,6 +46,33 @@ order.customer = "Adam Pahlevi Baihaqi"
|
|
46
46
|
Notice that both `order_id` and `customer` have not yet been defined, but a condensable
|
47
47
|
class would automatically define that on-demand.
|
48
48
|
|
49
|
+
### Creating on-the-fly condensable class
|
50
|
+
|
51
|
+
You don't need to define a class, you can generate them on-demand:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
order = Condensable.new.new
|
55
|
+
order.order_id = 123
|
56
|
+
```
|
57
|
+
|
58
|
+
First `.new` will create a class on-the-fly for you. The second `.new` actually initialize the class.
|
59
|
+
|
60
|
+
You can also pass in block, and define what a usual class could be defined:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
order = Condensable.new do
|
64
|
+
def full_name
|
65
|
+
"#{first_name} #{last_name}"
|
66
|
+
end
|
67
|
+
end.new
|
68
|
+
|
69
|
+
order.first_name = 'Adam'
|
70
|
+
order.last_name = 'Pahlevi'
|
71
|
+
order.full_name == 'Adam Pahlevi' # true
|
72
|
+
```
|
73
|
+
|
74
|
+
For more, please have a look at our spec to demonstrate its power.
|
75
|
+
|
49
76
|
### Specifying default condensation behaviour
|
50
77
|
|
51
78
|
By default, when a getter is not available, a condensable instance will return nil:
|
data/lib/condensable.rb
CHANGED
@@ -10,4 +10,30 @@ module Condensable
|
|
10
10
|
base.include(Condensable::GravityAbsorber)
|
11
11
|
base.extend(Condensable::GravityAbsorber::ClassLevelMethods)
|
12
12
|
end
|
13
|
+
|
14
|
+
def self.new(*args, &block)
|
15
|
+
# make class on the fly, on the fly
|
16
|
+
klass = Class.new do
|
17
|
+
include Condensable
|
18
|
+
end
|
19
|
+
|
20
|
+
# initialize if args are specified
|
21
|
+
args.each do |arg|
|
22
|
+
if arg.is_a?(Hash)
|
23
|
+
default_value = arg[:default] || arg['default']
|
24
|
+
if default_value
|
25
|
+
klass.class_eval do
|
26
|
+
condensable default: default_value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# if block is given, execute the block
|
33
|
+
if block_given?
|
34
|
+
klass.class_eval(&block)
|
35
|
+
end
|
36
|
+
|
37
|
+
klass
|
38
|
+
end
|
13
39
|
end
|
data/lib/condensable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: condensable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Pahlevi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|