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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8185f13ee6b748f2c4c635e756dc3620bf01bf2
4
- data.tar.gz: 09b453e7b3a4d9eee56c31bd2f84d6b3b076a183
3
+ metadata.gz: 2af3831d03fa1d430e4dea63b2c15d1eea4d4b8a
4
+ data.tar.gz: b097074c10e9a8cc3d2ff526289df584d532d5c3
5
5
  SHA512:
6
- metadata.gz: 1fed5effcba967ed6a83634835a8a30edf0825398d531afb3ee4c4eed53fb2b695d1e22ed3abe46ddd9c5ca0a6d6e4846a02c6eae1cbb1e5ba3b405ccc0b46eb
7
- data.tar.gz: a3e15e61ff992b80f2f517fefa932e2c3e195f55fe483051fd0dd97d6d042f8ab025c72f48315016ae647caf651e379febe88674b43c500107cc3bc5f6866fc2
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
@@ -1,3 +1,3 @@
1
1
  module Condensable
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
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.2.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-25 00:00:00.000000000 Z
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler