klassy 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/klassy.gemspec +24 -0
- data/lib/klassy/klassic.rb +64 -0
- data/lib/klassy/version.rb +3 -0
- data/lib/klassy.rb +20 -0
- data/spec/klassic_spec.rb +11 -0
- data/spec/klassy_spec.rb +48 -0
- data/spec/spec_helper.rb +17 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 876578480efd6ee6151b969189756f8aa85825a5
|
4
|
+
data.tar.gz: 7b2e075727aa9211bf872e80c9bef72177657a16
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8435d75c2b12d14cccc3d1b0328b0b500153884ed3afa9cc42ad1164eeccfafa38fee6ba8d13727080fee1bc7b4cafb5583653ae3ac7c41d392dc28fabe7c6bc
|
7
|
+
data.tar.gz: 96b63e82fc38aa47da01459febeada036c849f416135fd8f94f4cf610d306fa983628afd348a57d018c666334627bddaaae8426f60e7ff718d55aa9c4c40282a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Johnny Holton
|
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,53 @@
|
|
1
|
+
# Klassy
|
2
|
+
|
3
|
+
A gem that provides a DSL for dynamically creating classes and their
|
4
|
+
objects.
|
5
|
+
|
6
|
+
## Warning
|
7
|
+
|
8
|
+
This code is still in its infancy, and is under active development.
|
9
|
+
It is not to be trusted for anything important.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'klassy'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install klassy
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Example:
|
28
|
+
|
29
|
+
Klassy.klassify do
|
30
|
+
first_class(name: "bob") do
|
31
|
+
second_class(color: "green", answer: 42) do
|
32
|
+
third_class
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
results in:
|
38
|
+
```
|
39
|
+
#<Klassy::FirstClass @children=[
|
40
|
+
#<Klassy::SecondClass @children=[
|
41
|
+
#<Klassy::ThirdClass @children=[]>
|
42
|
+
], @color="green", @answer=42>
|
43
|
+
], @name="bob">
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/klassy.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'klassy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "klassy"
|
8
|
+
spec.version = Klassy::VERSION
|
9
|
+
spec.authors = ["Johnny Holton"]
|
10
|
+
spec.email = ["johnny@holton.co"]
|
11
|
+
spec.description = %q{DSL to dynamically define classes}
|
12
|
+
spec.summary = %q{DSL to dynamically define classes}
|
13
|
+
spec.homepage = ""
|
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
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Klassy
|
2
|
+
class Klassic
|
3
|
+
|
4
|
+
attr_accessor :parent
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@parent = options[:parent]
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.klassify(options = {}, &block)
|
11
|
+
klassic = new(options)
|
12
|
+
klassic.instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def method_missing(name, *args, &block)
|
18
|
+
# create the object
|
19
|
+
obj = create_object(name, *args, &block)
|
20
|
+
|
21
|
+
# if there's a parent, make this object one of it's children
|
22
|
+
if parent
|
23
|
+
parent.children << obj
|
24
|
+
end
|
25
|
+
|
26
|
+
# if block_given? then we have more objects to create.
|
27
|
+
if block_given?
|
28
|
+
Klassic.klassify({parent: obj}, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
obj
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_object(name, *args, &block)
|
35
|
+
klass = find_or_create_class(name)
|
36
|
+
attribs = args.find{|x| x.class == Hash }
|
37
|
+
obj = klass.new
|
38
|
+
if attribs
|
39
|
+
obj.instance_eval do
|
40
|
+
attribs.each do |key, val|
|
41
|
+
instance_variable_set "@#{key}", val
|
42
|
+
klass.class_eval do
|
43
|
+
attr_accessor key
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
obj
|
49
|
+
end
|
50
|
+
|
51
|
+
def find_or_create_class(name)
|
52
|
+
begin
|
53
|
+
Klassy.const_get("#{name.to_s.camelize}")
|
54
|
+
rescue
|
55
|
+
Klassy.const_set("#{name.to_s.camelize}", Class.new do
|
56
|
+
def initialize
|
57
|
+
@children = []
|
58
|
+
end
|
59
|
+
attr_accessor :children
|
60
|
+
end)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/klassy.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require_relative "klassy/version"
|
3
|
+
require_relative "klassy/klassic"
|
4
|
+
|
5
|
+
# Example:
|
6
|
+
# Klassy.klassify do
|
7
|
+
# first_class(name: "bob") do
|
8
|
+
# second_class(color: "green", answer: 42) do
|
9
|
+
# third_class
|
10
|
+
# end
|
11
|
+
# second_class(sky: "blue")
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
|
15
|
+
module Klassy
|
16
|
+
def self.klassify(options = {}, &block)
|
17
|
+
# just delegate it
|
18
|
+
Klassy::Klassic.klassify(options, &block)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'klassy/klassic'
|
3
|
+
|
4
|
+
describe Klassy::Klassic do
|
5
|
+
describe "#find_or_create_class" do
|
6
|
+
it "creates a class" do
|
7
|
+
klass = Klassy::Klassic.new.send(:find_or_create_class, :red_apple)
|
8
|
+
klass.to_s.should eq("Klassy::RedApple")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/spec/klassy_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'klassy'
|
3
|
+
|
4
|
+
describe Klassy do
|
5
|
+
def test_run
|
6
|
+
Klassy.klassify do
|
7
|
+
first_class(name: "bob") do
|
8
|
+
second_class(color: "green", answer: 42) do
|
9
|
+
third_class
|
10
|
+
end
|
11
|
+
second_class(sky: "blue")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
#subject(:result_object) { test_run }
|
17
|
+
before(:all) { @result_object = test_run }
|
18
|
+
|
19
|
+
describe "::klassify" do
|
20
|
+
it "should create classes" do
|
21
|
+
# @result_object.should be_an_instance_of(Klassy::FirstClass)
|
22
|
+
@result_object.class.name.should eq("Klassy::FirstClass")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should create children recursively" do
|
26
|
+
@result_object.children.first.should
|
27
|
+
be_an_instance_of(Klassy::SecondClass)
|
28
|
+
@result_object.children.first.children.first.should
|
29
|
+
be_an_instance_of(Klassy::ThirdClass)
|
30
|
+
@result_object.children.count.should eq(2)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should create attributes dynamically" do
|
34
|
+
@result_object.name.should eq("bob")
|
35
|
+
@result_object.children.first.color.should eq("green")
|
36
|
+
@result_object.children.first.answer.should eq(42)
|
37
|
+
@result_object.children.last.sky.should eq("blue")
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "resulting object" do
|
43
|
+
it "should respond to #children" do
|
44
|
+
@result_object.should respond_to(:children)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: klassy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Johnny Holton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
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'
|
55
|
+
description: DSL to dynamically define classes
|
56
|
+
email:
|
57
|
+
- johnny@holton.co
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- klassy.gemspec
|
69
|
+
- lib/klassy.rb
|
70
|
+
- lib/klassy/klassic.rb
|
71
|
+
- lib/klassy/version.rb
|
72
|
+
- spec/klassic_spec.rb
|
73
|
+
- spec/klassy_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: DSL to dynamically define classes
|
99
|
+
test_files:
|
100
|
+
- spec/klassic_spec.rb
|
101
|
+
- spec/klassy_spec.rb
|
102
|
+
- spec/spec_helper.rb
|