eapi 0.1.0 → 0.1.1

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: 4eaa6b7386dfe974f725d80fa008f3d8dbe5ee46
4
- data.tar.gz: a9b8fb3d839460128e7595c11db9754b18ac0347
3
+ metadata.gz: ce4210341572090ddcab6a924eef6512c26790a8
4
+ data.tar.gz: 1773ccd9aa977cd193f4efd802cc1a11abe6d2b4
5
5
  SHA512:
6
- metadata.gz: f6cd68cc945520b4e4eaf62a8f60be1f53e06bd21226befb7817cfcefeb01168d04dd75cb7491eae835cd2aa1b24cf3321d838fa56bae973f709b05b191c5db2
7
- data.tar.gz: d5688563cada1584200aa2ca6febca6303fba83115484a3dfe80e103dda2282fdb5a7dc3871aa9858415a9eb8f478669ffd255ed674bbb6b63a9a5d00fc67d06
6
+ metadata.gz: 2c9beaa7358443dee5acdc658dd422a679376c2a29af2db1d6efdc780c47020ea8fc69c2aa2bdeb2e8d5c9b098544fd1a6bf1cb785eb18cc38ce85c9b588f682
7
+ data.tar.gz: f4d071de2e5e10e4300509088e3f60fcd6d173a1492766f95eab6600bb991ae28e77eda3b2d0484ed003a51432c5f2480700bec076b17804c85934d7c1856f78
data/README.md CHANGED
@@ -516,6 +516,50 @@ obj.is_an_one_thing? # => true
516
516
  obj.is_a_super_duper_thing? # => false
517
517
  ```
518
518
 
519
+ ### Use in your own library
520
+
521
+ You can add the functionality of Eapi to your own library module, and use it instead of `Eapi::Common`.
522
+
523
+ Method-call-initialise shortcut can ignore the base name:
524
+
525
+ ```ruby
526
+ module MyExtension
527
+ extend Eapi
528
+ end
529
+
530
+ class TestKlass
531
+ include MyExtension::Common
532
+ property :something
533
+ end
534
+
535
+ obj = MyExtension.test_klass something: 1
536
+ obj.something # => 1
537
+
538
+
539
+ # if the class is in the same module, it can be omitted when using the object creation shortcut
540
+
541
+ module MyExtension
542
+ class TestKlassInside
543
+ include MyExtension::Common
544
+ property :something
545
+ end
546
+ end
547
+
548
+ obj = MyExtension.my_extension_test_klass_inside something: 1
549
+ obj.something # => 1
550
+
551
+ obj = MyExtension.test_klass_inside something: 1
552
+ obj.something # => 1
553
+ ```
554
+
555
+ ### important note:
556
+
557
+ As it works now, the children of your extension will be also children of `Eapi`, so calling `Eapi.your_klass` and `YourExtension.your_klass` will do the same.
558
+
559
+ ## TODO
560
+
561
+ 1. `type` option in property definition to accept symbol -> if a class can be recognised by that name, it works ok. If not, it still uses that for type validation (using `is?`) but it does not use that in the `init_` method.
562
+
519
563
  ## Contributing
520
564
 
521
565
  1. Fork it ( https://github.com/eturino/eapi/fork )
@@ -13,12 +13,24 @@ require 'eapi/common'
13
13
 
14
14
 
15
15
  module Eapi
16
- def self.method_missing(method, *args, &block)
17
- klass = Eapi::Children.get(method)
18
- if klass
19
- klass.new *args, &block
20
- else
21
- super
16
+ def self.add_method_missing(klass)
17
+ def klass.method_missing(method, *args, &block)
18
+ child_klass = Eapi::Children.get(method, self)
19
+ if child_klass
20
+ child_klass.new *args, &block
21
+ else
22
+ super
23
+ end
22
24
  end
23
25
  end
26
+
27
+ add_method_missing self
28
+
29
+ def self.extended(mod)
30
+ mod.class_eval <<-CODE
31
+ Common = Eapi::Common
32
+ Children = Eapi::Children
33
+ CODE
34
+ Eapi.add_method_missing mod
35
+ end
24
36
  end
@@ -11,20 +11,36 @@ module Eapi
11
11
  CHILDREN[k] = klass
12
12
  end
13
13
 
14
- def self.get(klass_name)
15
- k = self.key_for klass_name
16
- CHILDREN[k] ||
17
- CHILDREN[k.gsub('__', '/')] ||
18
- CHILDREN.select { |key, _| key.gsub('/', '_') == k }.values.first
14
+ def self.get(klass_name, base_class = nil)
15
+ k = key_for klass_name
16
+
17
+ find(k) || find_bare(base_class, k)
19
18
  end
20
19
 
21
20
  def self.has?(klass_name)
22
21
  !!self.get(klass_name)
23
22
  end
24
23
 
24
+ private
25
+ def self.find(k)
26
+ CHILDREN[k] ||
27
+ CHILDREN[k.gsub('__', '/')] ||
28
+ CHILDREN.select { |key, _| key.gsub('/', '_') == k }.values.first
29
+ end
30
+
25
31
  def self.key_for(klass_name)
26
32
  k = klass_name.to_s
27
33
  k.underscore
28
34
  end
35
+
36
+ def self.find_bare(base_class, k)
37
+ if base_class.present?
38
+ base_key = key_for(base_class)
39
+ find "#{base_key}/#{k}"
40
+ else
41
+ nil
42
+ end
43
+ end
44
+
29
45
  end
30
46
  end
@@ -1,11 +1,22 @@
1
1
  module Eapi
2
2
  module Common
3
- extend ActiveSupport::Concern
4
- included do |klass|
3
+ def self.add_features(klass)
5
4
  Eapi::Children.append klass
6
5
  klass.send :include, ActiveModel::Validations
7
6
  klass.send :include, Eapi::Methods::Properties::InstanceMethods
8
7
  klass.send :include, Eapi::Methods::Types::InstanceMethods
8
+
9
+ klass.send :extend, ClassMethods
10
+ end
11
+
12
+ def self.extended(mod)
13
+ def mod.included(klass)
14
+ Eapi::Common.add_features klass
15
+ end
16
+ end
17
+
18
+ def self.included(klass)
19
+ Eapi::Common.add_features klass
9
20
  end
10
21
 
11
22
  def initialize(** properties)
@@ -1,3 +1,3 @@
1
1
  module Eapi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Eapi do
4
+
5
+ context 'extension in other modules' do
6
+ module MyExtension
7
+ extend Eapi
8
+ end
9
+
10
+ class MyExtensionExternalKlass
11
+ include MyExtension::Common
12
+
13
+ property :something
14
+ end
15
+
16
+ module MyExtension
17
+ class TestKlass
18
+ include MyExtension::Common
19
+
20
+ property :something
21
+ end
22
+ end
23
+
24
+ describe 'creates a MyExtension::Common module that works as Eapi::Common' do
25
+ it 'adds classes that includes the new module to Eapi children' do
26
+ expect(Eapi::Children).to be_has MyExtensionExternalKlass
27
+ expect(MyExtension::Children).to be_has MyExtensionExternalKlass
28
+
29
+ expect(Eapi::Children).to be_has MyExtension::TestKlass
30
+ expect(MyExtension::Children).to be_has MyExtension::TestKlass
31
+ end
32
+ end
33
+
34
+ describe 'creation shortcut' do
35
+ it 'allows the new module to be used for object creation shortcut' do
36
+ obj = MyExtension.my_extension_external_klass something: 1
37
+ expect(obj.to_h).to eq({something: 1})
38
+ end
39
+
40
+ describe 'if classes are inside the module, the name can be ignored with creation shortcut' do
41
+ it 'MyExtension.test_klass(..) => MyExtension::TestKlass.new(..)' do
42
+ obj = MyExtension.test_klass something: 1
43
+ expect(obj).to be_a_kind_of MyExtension::TestKlass
44
+ expect(obj.to_h).to eq({something: 1})
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Turiño
@@ -195,6 +195,7 @@ files:
195
195
  - lib/eapi/version.rb
196
196
  - spec/basic_spec.rb
197
197
  - spec/definition_spec.rb
198
+ - spec/extension_spec.rb
198
199
  - spec/function_spec.rb
199
200
  - spec/list_spec.rb
200
201
  - spec/spec_helper.rb
@@ -229,6 +230,7 @@ summary: ruby gem for building complex structures that will end up in hashes (in
229
230
  test_files:
230
231
  - spec/basic_spec.rb
231
232
  - spec/definition_spec.rb
233
+ - spec/extension_spec.rb
232
234
  - spec/function_spec.rb
233
235
  - spec/list_spec.rb
234
236
  - spec/spec_helper.rb