def_dsl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format d
3
+ -r support/spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in def_dsl.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexander K
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # DefDsl
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'def_dsl'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install def_dsl
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
data/def_dsl.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'def_dsl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "def_dsl"
8
+ spec.version = DefDsl::VERSION
9
+ spec.authors = ["Alexander K"]
10
+ spec.email = ["xpyro@ya.ru"]
11
+ spec.description = %q{ easy way to build dsl without using dsl... }.strip
12
+ spec.summary = %q{ easy way to build dsl without using dsl... }.strip
13
+ spec.homepage = "https://github.com/sowcow/def_dsl"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ end
data/lib/def_dsl.rb ADDED
@@ -0,0 +1,95 @@
1
+ # PROTIP:
2
+ # use mixins for behavior, and classes only for classification!
3
+ #
4
+ # extended at the end of context class
5
+ # rspec!!!!!!!!!!!!!
6
+
7
+
8
+ # def def_dsl klass
9
+ # end
10
+
11
+ require "def_dsl/version"
12
+
13
+ module DefDsl
14
+
15
+
16
+ def def_dsl const
17
+ define_dsl! self, [[const.name.to_s.underscore(self), const]]
18
+ end
19
+ alias define_dsl def_dsl
20
+
21
+
22
+ def shallow_constants who
23
+ (who.constants - who.superclass.constants).map { |name| * = name.to_s.underscore, who.const_get(name) }
24
+ end
25
+
26
+
27
+ def define_dsl! who, constants=shallow_constants(who) # ...
28
+ # constants = constants.nil?? shallow_constants(who) : constants_info(constants)
29
+ constants.each do |mini, const|
30
+
31
+ next if const.included_modules.include? So
32
+ who.send :include, So
33
+ define_dsl! const if const.is_a? Class
34
+
35
+ who.class_eval do
36
+ define_method(mini) do |*a,&b|
37
+ so mini,*a,&b
38
+ end
39
+ end
40
+
41
+ # const.send :include, So if const.is_a? Module # or even class
42
+ # who.send :include, So if who.is_a? Module # or even class
43
+ if who.class == Module
44
+ who.send :module_function, mini
45
+ who.send :module_function, :so
46
+ who.send :module_function, :so1
47
+ who.send :module_function, :so2
48
+ end
49
+ end
50
+ end
51
+
52
+ module So
53
+ def so klass_name=nil,*a,&b
54
+ return @so if klass_name.nil?
55
+
56
+ klass_name = klass_name.to_sym
57
+
58
+ if self.class == Module
59
+ klass = self.const_get(klass_name.to_s.camelize)
60
+ else
61
+ klass = self.class.const_get(klass_name.to_s.camelize)
62
+ end
63
+
64
+ obj = klass.new(*a) #,&b)
65
+ obj.instance_eval &b if b
66
+ (@so ||= {}).tap { |h| h[klass_name] = h[klass_name] ? [h[klass_name], obj].flatten(1) : obj }
67
+ end
68
+ def so1(name)
69
+ value = @so[name]
70
+ value.is_a?(Array) ? value.last : value
71
+ end
72
+ def so2(name)
73
+ value = @so[name]
74
+ value.is_a?(Array) ? value : [value]
75
+ end
76
+ end
77
+
78
+ end
79
+ DefDSL = DefDsl
80
+
81
+
82
+ class String
83
+ def camelize
84
+ self.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
85
+ end
86
+
87
+ def underscore(relative_to=nil)
88
+ name = relative_to.nil?? self : self.sub(/^#{ relative_to.to_s }::/,'')
89
+ name.gsub(/::/, '/').
90
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
91
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
92
+ tr("-", "_").
93
+ downcase
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module DefDsl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,68 @@
1
+ describe [DefDsl,DefDSL].join' or ' do
2
+ after { remove_const :My if defined? My }
3
+
4
+ example 'usage inside module' do
5
+ module My
6
+ # nice looking recursive dsl definition
7
+ class File < Struct.new :name; end
8
+ class Dir < Struct.new :name
9
+ Dir = Dir
10
+ File = File
11
+ end
12
+
13
+ extend DefDSL
14
+ def_dsl Dir
15
+ def_dsl File
16
+
17
+ file 'root.txt'
18
+ dir 'dir' do
19
+ dir 'inner' do
20
+ dir 'empty'
21
+ file 'any'
22
+ end
23
+ end
24
+
25
+ so.should == {file: so1(:file), dir: so1(:dir)}
26
+
27
+ so1(:file).name.should == 'root.txt'
28
+ so1(:dir).name.should == 'dir'
29
+ (inner = so1(:dir).send(:so1,:dir)).name.should == 'inner'
30
+ inner.send(:so2,:dir).map(&:name).should == ['empty']
31
+ inner.send(:so2,:file).map(&:name).should == ['any']
32
+ end
33
+ end
34
+
35
+ example 'usage inside object' do
36
+ class My
37
+
38
+ class File < Struct.new :name; end
39
+ class Dir < Struct.new :name
40
+ Dir = Dir
41
+ File = File
42
+ end
43
+
44
+ extend DefDSL
45
+ def_dsl Dir
46
+ def_dsl File
47
+
48
+ def initialize
49
+ file 'root.txt'
50
+ dir 'dir' do
51
+ dir 'inner' do
52
+ dir 'empty'
53
+ file 'any'
54
+ end
55
+ end
56
+
57
+ so.should == {file: so1(:file), dir: so1(:dir)}
58
+
59
+ so1(:file).name.should == 'root.txt'
60
+ so1(:dir).name.should == 'dir'
61
+ (inner = so1(:dir).send(:so1,:dir)).name.should == 'inner'
62
+ inner.send(:so2,:dir).map(&:name).should == ['empty']
63
+ inner.send(:so2,:file).map(&:name).should == ['any']
64
+ end
65
+ end
66
+ My.new
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ def remove_const name
2
+ Object.send :remove_const, name
3
+ end
@@ -0,0 +1 @@
1
+ require 'def_dsl'
@@ -0,0 +1,9 @@
1
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
2
+ RSpec.configure do |config|
3
+ config.treat_symbols_as_metadata_keys_with_true_values = true
4
+ config.run_all_when_everything_filtered = true
5
+ config.filter_run :focus
6
+ config.order = 'random'
7
+ end
8
+
9
+ Dir["./spec/support/**/*.rb"].each {|f| require f} # no comment
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: def_dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander K
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: easy way to build dsl without using dsl...
63
+ email:
64
+ - xpyro@ya.ru
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - def_dsl.gemspec
76
+ - lib/def_dsl.rb
77
+ - lib/def_dsl/version.rb
78
+ - spec/exmaple_spec.rb
79
+ - spec/support/remove_const.rb
80
+ - spec/support/require_self.rb
81
+ - spec/support/spec_helper.rb
82
+ homepage: https://github.com/sowcow/def_dsl
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: -563372381577040081
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -563372381577040081
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.25
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: easy way to build dsl without using dsl...
113
+ test_files:
114
+ - spec/exmaple_spec.rb
115
+ - spec/support/remove_const.rb
116
+ - spec/support/require_self.rb
117
+ - spec/support/spec_helper.rb