condensable 0.1.0 → 0.2.0
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 +4 -4
- data/README.md +88 -12
- data/lib/condensable/gravity_absorber.rb +56 -15
- 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: b8185f13ee6b748f2c4c635e756dc3620bf01bf2
|
4
|
+
data.tar.gz: 09b453e7b3a4d9eee56c31bd2f84d6b3b076a183
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fed5effcba967ed6a83634835a8a30edf0825398d531afb3ee4c4eed53fb2b695d1e22ed3abe46ddd9c5ca0a6d6e4846a02c6eae1cbb1e5ba3b405ccc0b46eb
|
7
|
+
data.tar.gz: a3e15e61ff992b80f2f517fefa932e2c3e195f55fe483051fd0dd97d6d042f8ab025c72f48315016ae647caf651e379febe88674b43c500107cc3bc5f6866fc2
|
data/README.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# Condensable
|
2
2
|
|
3
|
-
|
3
|
+
[ ](https://codeship.com/projects/110913)
|
4
4
|
|
5
|
-
|
5
|
+
In chemistry, condensation is the process of vapour or gas to become a liquid. But,
|
6
|
+
this is no chemistry things. Here, condensable is describing a condition of a class
|
7
|
+
that allow to make itself respond to getter and setter routines when they are not
|
8
|
+
(yet) defined.
|
9
|
+
|
10
|
+
Condensation works well with [Assignbot](https://rubygems.org/gems/assignbot)!
|
6
11
|
|
7
12
|
## Installation
|
8
13
|
|
@@ -22,18 +27,89 @@ Or install it yourself as:
|
|
22
27
|
|
23
28
|
## Usage
|
24
29
|
|
25
|
-
|
30
|
+
Simple enough, just include `Condensable` into your class and you are set.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class OrderParams
|
34
|
+
include Condensable
|
35
|
+
end
|
36
|
+
```
|
26
37
|
|
27
|
-
|
38
|
+
And, you can use your condensable instance as follow:
|
28
39
|
|
29
|
-
|
40
|
+
```ruby
|
41
|
+
order = OrderParams.new
|
42
|
+
order.order_id = 12
|
43
|
+
order.customer = "Adam Pahlevi Baihaqi"
|
44
|
+
```
|
45
|
+
|
46
|
+
Notice that both `order_id` and `customer` have not yet been defined, but a condensable
|
47
|
+
class would automatically define that on-demand.
|
48
|
+
|
49
|
+
### Specifying default condensation behaviour
|
50
|
+
|
51
|
+
By default, when a getter is not available, a condensable instance will return nil:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
order.shipping_address == nil # true
|
55
|
+
```
|
56
|
+
|
57
|
+
To alter the default behaviour, specify `condensable default` in your class, like below:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class OrderParams
|
61
|
+
include Condensable
|
62
|
+
|
63
|
+
condensable default: "not-available"
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
Therefore, when invoked:
|
68
|
+
|
69
|
+
```
|
70
|
+
order.shipping_address
|
71
|
+
```
|
30
72
|
|
31
|
-
|
73
|
+
It will returns `not-available`.
|
74
|
+
|
75
|
+
There are 4 condensation behaviours:
|
76
|
+
|
77
|
+
1. Returning nil (`condensable default: nil`)
|
78
|
+
2. Returning string (`condensable default: "some string"`)
|
79
|
+
3. Executing a method, by always passing a symbol (`condensable default: :execute_method`)
|
80
|
+
4. Raising an error (`condensable default: :raise_error`)
|
81
|
+
|
82
|
+
For more details, please see the spec which demonstrates the all four.
|
83
|
+
|
84
|
+
### Iterating condensed variables
|
85
|
+
|
86
|
+
To get to know what are all the condensed variables, use:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
order.condensed_variables
|
90
|
+
```
|
91
|
+
|
92
|
+
To get to know all of condensed values, use:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
order.condensed_values
|
96
|
+
```
|
97
|
+
|
98
|
+
Iterating both:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
order.each do |condensed_var, condensed_value|
|
102
|
+
puts "#{condensed_var}: #{condensed_value}"
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
### Checking whether an attribute is a result of condensation
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
order.is_condensed?(:order_id) # ==> true
|
110
|
+
order.is_condensed?(:shipping_address) # ==> false
|
111
|
+
```
|
32
112
|
|
33
|
-
##
|
113
|
+
## License
|
34
114
|
|
35
|
-
|
36
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
-
5. Create a new Pull Request
|
115
|
+
The gem is proudly available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -6,22 +6,63 @@ module Condensable
|
|
6
6
|
base.extend(GravityAbsorber::ClassLevelMethods)
|
7
7
|
end
|
8
8
|
|
9
|
+
# list all condensed variables
|
10
|
+
def condensed_variables
|
11
|
+
@condensed_variables = [] if @condensed_variables.nil?
|
12
|
+
@condensed_variables
|
13
|
+
end
|
14
|
+
|
15
|
+
# check whether a variable is a result of condensation
|
16
|
+
def is_condensed?(variable_name)
|
17
|
+
@condensed_variables.include?(variable_name.to_sym)
|
18
|
+
end
|
19
|
+
|
20
|
+
# all condensed variables' value
|
21
|
+
def condensed_values
|
22
|
+
values = []
|
23
|
+
condensed_variables.each do |var|
|
24
|
+
values << send(var)
|
25
|
+
end
|
26
|
+
values
|
27
|
+
end
|
28
|
+
|
29
|
+
# iterating all condensed variables and its value
|
30
|
+
def each
|
31
|
+
condensed_variables.each do |var|
|
32
|
+
yield var, send(var)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
9
36
|
def method_missing(method_name, *args, &block)
|
10
37
|
if method_name.to_s[-1] == '='
|
38
|
+
@condensed_variables = [] if @condensed_variables.nil?
|
11
39
|
# get proper attribute name, by removing "="
|
12
40
|
attribute_name = method_name[0..-2]
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
41
|
+
setter_name = "#{attribute_name}="
|
42
|
+
getter_name = attribute_name
|
43
|
+
|
44
|
+
unless respond_to?(setter_name)
|
45
|
+
instance_eval do
|
46
|
+
eval %Q{
|
47
|
+
def #{attribute_name}=(arg)
|
48
|
+
@#{attribute_name} = arg
|
49
|
+
end
|
50
|
+
}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
unless respond_to?(getter_name)
|
55
|
+
instance_eval do
|
56
|
+
eval %Q{
|
57
|
+
def #{attribute_name}
|
58
|
+
@#{attribute_name}
|
59
|
+
end
|
60
|
+
}
|
61
|
+
end # instance eval
|
62
|
+
@condensed_variables << getter_name.to_sym
|
63
|
+
end
|
64
|
+
|
65
|
+
send(setter_name, *args)
|
25
66
|
else
|
26
67
|
condensable_missing_attribute(method_name, *args)
|
27
68
|
end
|
@@ -43,6 +84,6 @@ module Condensable
|
|
43
84
|
elsif default_handle_arg.is_a?(String)
|
44
85
|
return default_handle_arg
|
45
86
|
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
87
|
+
end # condensable missing attribute
|
88
|
+
end # gravity absorber
|
89
|
+
end # condensable
|
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: 0.2.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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|