syntax_sugar 0.0.1.alpha → 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 +16 -2
- data/Rakefile +3 -1
- data/lib/syntax_sugar.rb +3 -4
- data/lib/syntax_sugar/null_object.rb +50 -0
- data/lib/syntax_sugar/version.rb +1 -3
- data/spec/spec_helper.rb +3 -0
- data/spec/syntax_sugar/null_object_spec.rb +96 -0
- data/syntax_sugar.gemspec +12 -7
- metadata +23 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0e7f25882903b4f9a0e694d1c3570d5841d4007
|
4
|
+
data.tar.gz: be7201d78ed8dfa055a96afa6e4196b70327fb8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43ba2fa8fa4dc06d72cfa0b82074311c1a1f43346f81c333396e2e33a82ab928b3fba31b18cebe44f1adbc855475d06d9403501c03d15281fe4883bb9c298d19
|
7
|
+
data.tar.gz: 60fa8da34eb9e6a5960223bc7dfb018636bcd0e0c661ef6c8df3b6ce07cad01b4d129e7395250b79742463720b30f69de7c170004079301fa53ab2db1973a0f3
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# SyntaxSugar
|
2
2
|
|
3
|
-
|
3
|
+
This lib target to help you create some dynamic syntax
|
4
|
+
where you could create method name based logic for factory like use cases.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -18,7 +19,20 @@ Or install it yourself as:
|
|
18
19
|
|
19
20
|
## Usage
|
20
21
|
|
21
|
-
|
22
|
+
```ruby
|
23
|
+
|
24
|
+
require 'syntax_sugar'
|
25
|
+
|
26
|
+
hash = Hash.new
|
27
|
+
sugar = SyntaxSugar::NullObject.new(hash, /^(.*)=$/ => :[]=, /^get_(.*)$/ => :[], /^fetch$/ => :[])
|
28
|
+
|
29
|
+
sugar.asd = 'cat'
|
30
|
+
p sugar.get_asd #> "cat"
|
31
|
+
p hash #> {"asd"=>"cat"}
|
32
|
+
p sugar.fetch 'asd' #> "cat"
|
33
|
+
|
34
|
+
```
|
35
|
+
|
22
36
|
|
23
37
|
## Contributing
|
24
38
|
|
data/Rakefile
CHANGED
data/lib/syntax_sugar.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
class SyntaxSugar::NullObject < BasicObject
|
2
|
+
|
3
|
+
VALID_TARGET_METHOD_TYPES = [::String, ::Symbol]
|
4
|
+
VALID_METHOD_MATCHERS_TYPES = [::Regexp]
|
5
|
+
|
6
|
+
def initialize(subject_object, regexp_pointers = {})
|
7
|
+
|
8
|
+
@__object__ = subject_object
|
9
|
+
|
10
|
+
unless regexp_pointers.is_a?(::Hash)
|
11
|
+
::Kernel.raise(::ArgumentError, 'method pointer collection should be instance of Hash or Array')
|
12
|
+
end
|
13
|
+
|
14
|
+
if regexp_pointers.find{|k,v| !VALID_METHOD_MATCHERS_TYPES.any?{|klass| k.is_a?(klass) } }
|
15
|
+
::Kernel.raise(::ArgumentError,'invalid pointer given in the method definitions, use regular expression')
|
16
|
+
end
|
17
|
+
|
18
|
+
if regexp_pointers.find{|k,v| !VALID_TARGET_METHOD_TYPES.any?{|klass| v.is_a?(klass) } }
|
19
|
+
::Kernel.raise(::ArgumentError,'invalid target method given in the method definitions, use string or symbol')
|
20
|
+
end
|
21
|
+
|
22
|
+
@regexp_pointers = regexp_pointers
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(method_name, *args, &block)
|
27
|
+
|
28
|
+
@regexp_pointers.each do |regexp, target_method|
|
29
|
+
|
30
|
+
if method_name.to_s =~ regexp
|
31
|
+
|
32
|
+
if regexp.source =~ /\(.*\)/
|
33
|
+
method_extraction = method_name.to_s.scan(regexp)[0][0]
|
34
|
+
args.unshift(method_extraction)
|
35
|
+
end
|
36
|
+
|
37
|
+
return @__object__.__send__(
|
38
|
+
target_method,
|
39
|
+
*args, &block
|
40
|
+
)
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
::Kernel.raise(::NameError,"undefined local variable or method `#{method_name}' for #{@__object__.inspect}:SyntaxSugar::NullObject")
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/lib/syntax_sugar/version.rb
CHANGED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
describe SyntaxSugar::NullObject do
|
3
|
+
|
4
|
+
let(:target_object) { double('sample object', :[] => 'GET', :[]= => 'SET') }
|
5
|
+
let(:method_pointers) {
|
6
|
+
{
|
7
|
+
/^(.*)=$/ => :[]=,
|
8
|
+
/^(.*)$/ => :[]
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
subject { self.described_class.new(target_object, method_pointers) }
|
13
|
+
|
14
|
+
it 'should be a BasicObject' do
|
15
|
+
subject_class= (
|
16
|
+
class << subject;
|
17
|
+
self
|
18
|
+
end
|
19
|
+
).superclass
|
20
|
+
expect(subject_class).to be_a BasicObject
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#initialize' do
|
24
|
+
context 'when invalid method pointer given' do
|
25
|
+
|
26
|
+
context "and it's type wrong" do
|
27
|
+
let(:method_pointers) { 'not valid pointer' }
|
28
|
+
|
29
|
+
it 'should raise error' do
|
30
|
+
expect { subject }.to raise_error(ArgumentError, /method pointer collection should be instance of Hash or Array/i)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when passed method pointer collection pointing objects are not regular expressions' do
|
35
|
+
|
36
|
+
let(:method_pointers) {{ 'invalid pointer' => :valid_target_method }}
|
37
|
+
it 'should raise error and warn about the invalid pointer type' do
|
38
|
+
expect{ subject }.to raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when passed method caller method is not a valid string or symbol' do
|
44
|
+
|
45
|
+
let(:method_pointers) {{ /valid/ => /not valid/ }}
|
46
|
+
it 'should raise error and warn about the invalid pointer type' do
|
47
|
+
expect{ subject }.to raise_error(ArgumentError)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#method_missing' do
|
57
|
+
|
58
|
+
context 'when method pointer regular expression has group' do
|
59
|
+
|
60
|
+
let(:method_pointers) { {/^(.*)=$/ => :[]=} }
|
61
|
+
|
62
|
+
it 'should use given method and method on the object' do
|
63
|
+
|
64
|
+
expect(target_object).to receive(method_pointers.to_a[0][1]).with('sample', 123)
|
65
|
+
subject.sample = 123
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'when method pointer does not containt group' do
|
72
|
+
|
73
|
+
let(:method_pointers) { {/^.*$/ => :some_call} }
|
74
|
+
it 'should not pass the extracted content as first parameter' do
|
75
|
+
|
76
|
+
expect(target_object).to receive(:some_call).with('hello')
|
77
|
+
subject.say 'hello'
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when no pointer find any match' do
|
84
|
+
|
85
|
+
let(:method_pointers){{}}
|
86
|
+
it 'should raise an undefined error' do
|
87
|
+
|
88
|
+
expect{ subject.undefined_method_call }.to raise_error(NameError)
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
data/syntax_sugar.gemspec
CHANGED
@@ -1,24 +1,29 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'syntax_sugar'
|
4
6
|
require 'syntax_sugar/version'
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
|
-
|
9
|
+
|
10
|
+
spec.name = 'syntax_sugar'
|
8
11
|
spec.version = SyntaxSugar::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
12
|
+
spec.authors = ['Adam Luzsi']
|
13
|
+
spec.email = ['adamluzsi@gmail.com']
|
11
14
|
spec.summary = %q{ Tool to create object that has dynamic reponse to methods, sended to it, and redirect it to a pre specified target object }
|
12
15
|
spec.description = %q{ Tool to create object that has dynamic reponse to methods, sended to it, and redirect it to a pre specified target object }
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
16
|
+
spec.homepage = 'https://github.com/adamluzsi/syntax_sugar'
|
17
|
+
spec.license = 'MIT'
|
15
18
|
|
16
19
|
spec.files = `git ls-files -z`.split("\x0")
|
17
20
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
21
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
22
|
spec.require_paths = ["lib"]
|
20
23
|
|
21
|
-
|
22
|
-
spec.add_development_dependency
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec'
|
23
28
|
|
24
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syntax_sugar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: " Tool to create object that has dynamic reponse to methods, sended to
|
42
56
|
it, and redirect it to a pre specified target object "
|
43
57
|
email:
|
@@ -53,7 +67,10 @@ files:
|
|
53
67
|
- README.md
|
54
68
|
- Rakefile
|
55
69
|
- lib/syntax_sugar.rb
|
70
|
+
- lib/syntax_sugar/null_object.rb
|
56
71
|
- lib/syntax_sugar/version.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/syntax_sugar/null_object_spec.rb
|
57
74
|
- syntax_sugar.gemspec
|
58
75
|
homepage: https://github.com/adamluzsi/syntax_sugar
|
59
76
|
licenses:
|
@@ -70,9 +87,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
87
|
version: '0'
|
71
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
89
|
requirements:
|
73
|
-
- - "
|
90
|
+
- - ">="
|
74
91
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
92
|
+
version: '0'
|
76
93
|
requirements: []
|
77
94
|
rubyforge_project:
|
78
95
|
rubygems_version: 2.2.2
|
@@ -80,4 +97,6 @@ signing_key:
|
|
80
97
|
specification_version: 4
|
81
98
|
summary: Tool to create object that has dynamic reponse to methods, sended to it,
|
82
99
|
and redirect it to a pre specified target object
|
83
|
-
test_files:
|
100
|
+
test_files:
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/syntax_sugar/null_object_spec.rb
|