treant 0.0.1.alpha1 → 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
- SHA1:
3
- metadata.gz: 21738d98e59ed2b467cc9d8cac1999d4d3245f85
4
- data.tar.gz: 6b56a30df3e0ba728d0c30e048c2a61ba3db5fa7
2
+ SHA256:
3
+ metadata.gz: 04fae31228fcad8f52285ebfc8e8885b570210ff0d49aa868929086f97b73ea7
4
+ data.tar.gz: d89702266ff238a0287ae0321f28a6e1eb7bab100080c0f0ad159a4d836f7ac1
5
5
  SHA512:
6
- metadata.gz: ca4e36474563fffbc0a5d7634bef250f0e9e87731b124901e6dce629dda229c171eb168a652ef67ada17e473ccb9efcc3f41973f1ab1c6e257769ad75629b848
7
- data.tar.gz: 725e7ed9e68bda3c138eef7aca9f9b710e52df0711a71af1a1aa0ac4beb7c5324647506bda6ff9be8e1318b68fc8632df8660d40293b61a420e5d5968ffe5e3e
6
+ metadata.gz: f7ed78d2c089a1f2b26656d51993d64eb67588f1d9789788fb6b305943f9131386af702ad999316748d1cdccf7c211f5b42f4da2ac71dbd74704a5d4f13ddb3d
7
+ data.tar.gz: 1e2b229a1dc862e221c7af47313899264caf68c3dd234b8cd2f34d7c9b3e8f1d06482fd9202347280681de0bf96e9dcfeb80e6c76e9a022fe223259abda6f834
data/README.md CHANGED
@@ -1,2 +1,57 @@
1
- treant
2
- ======
1
+ # Treant
2
+
3
+ A small helper for your Seeds file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "treant"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install treant
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Generate seed data for given Active Record `model`:
28
+
29
+ ```ruby
30
+ class User < ApplicationRecord
31
+ end
32
+
33
+ UserSeeder = Seeder.new(User, :email)
34
+
35
+ UserSeeder.seed do |u|
36
+ u.email = 'user@example.com'
37
+ u.name = 'Bob'
38
+ end
39
+
40
+ User.count # => 1
41
+ User.first.name # => 'Bob'
42
+
43
+ # If the seed data is changed (except for the `keys` values),
44
+ # the record will be updated:
45
+
46
+ UserSeeder.seed do |u|
47
+ u.email = 'user@example.com'
48
+ u.name = 'Alice'
49
+ end
50
+
51
+ User.count # => 1
52
+ User.first.name # => 'Alice'
53
+ ```
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,43 +1,22 @@
1
- require "rack"
2
- require "seteable"
3
- require "syro"
1
+ # frozen_string_literal: true
4
2
 
5
- class Treant < Syro::Deck
6
- include Seteable
7
-
8
- def self.define(&block)
9
- app.run(Syro.new(self, &block))
10
- end
11
-
12
- def self.call(env)
13
- return prototype.call(env)
14
- end
15
-
16
- def self.prototype
17
- return @prototype ||= app.to_app
3
+ class Treant
4
+ def initialize(model, *unique_keys)
5
+ @model = model
6
+ @unique_keys = unique_keys
18
7
  end
19
8
 
20
- def self.app
21
- return @app ||= Rack::Builder.new
22
- end
9
+ def seed
10
+ attrs = OpenStruct.new
23
11
 
24
- def self.reset!
25
- @app = @prototype = nil
26
- end
12
+ yield(attrs)
27
13
 
28
- def self.use(middleware, *args, &block)
29
- app.use(middleware, *args, &block)
30
- end
14
+ conditions = @unique_keys.each_with_object({}) { |k, h| h[k] = attrs[k] }
31
15
 
32
- def self.plugin(mod, options = {})
33
- self.include(mod)
16
+ record = @model.find_or_initialize_by(conditions)
34
17
 
35
- if defined?(mod::ClassMethods)
36
- self.extend(mod::ClassMethods)
37
- end
18
+ record.update!(attrs.to_h)
38
19
 
39
- if mod.respond_to?(:setup)
40
- mod.setup(self, options)
41
- end
20
+ record
42
21
  end
43
22
  end
metadata CHANGED
@@ -1,107 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: treant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-28 00:00:00.000000000 Z
11
+ date: 2018-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: syro
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.7
20
- type: :runtime
19
+ version: '1.16'
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.7
26
+ version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rack
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.6.4
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 1.6.4
41
- - !ruby/object:Gem::Dependency
42
- name: seteable
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - '='
46
- - !ruby/object:Gem::Version
47
- version: 1.0.0
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - '='
53
- - !ruby/object:Gem::Version
54
- version: 1.0.0
55
- - !ruby/object:Gem::Dependency
56
- name: cutest
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - '='
60
- - !ruby/object:Gem::Version
61
- version: 1.2.2
33
+ version: '12.0'
62
34
  type: :development
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
- - - '='
67
- - !ruby/object:Gem::Version
68
- version: 1.2.2
69
- - !ruby/object:Gem::Dependency
70
- name: rack-test
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - '='
74
- - !ruby/object:Gem::Version
75
- version: 0.6.3
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - '='
38
+ - - "~>"
81
39
  - !ruby/object:Gem::Version
82
- version: 0.6.3
83
- description: treant
40
+ version: '12.0'
41
+ description: A small helper for your Seeds file.
84
42
  email:
85
- - frodsan@protonmail.ch
43
+ - frodsan@me.com
86
44
  executables: []
87
45
  extensions: []
88
46
  extra_rdoc_files: []
89
47
  files:
90
- - ".gems"
91
- - LICENSE
92
48
  - README.md
93
49
  - lib/treant.rb
94
- - lib/treant/version.rb
95
- - makefile
96
- - test/captures.rb
97
- - test/composition.rb
98
- - test/hello.rb
99
- - test/helper.rb
100
- - test/middleware.rb
101
- - test/plugin.rb
102
- - test/settings.rb
103
- - treant.gemspec
104
- homepage: https://github.com/harmoni/treant
50
+ homepage: https://github.com/frodsan/treant
105
51
  licenses:
106
52
  - MIT
107
53
  metadata: {}
@@ -116,13 +62,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
62
  version: '0'
117
63
  required_rubygems_version: !ruby/object:Gem::Requirement
118
64
  requirements:
119
- - - ">"
65
+ - - ">="
120
66
  - !ruby/object:Gem::Version
121
- version: 1.3.1
67
+ version: '0'
122
68
  requirements: []
123
69
  rubyforge_project:
124
- rubygems_version: 2.4.8
70
+ rubygems_version: 2.7.6
125
71
  signing_key:
126
72
  specification_version: 4
127
- summary: treant
73
+ summary: A small helper for your Seeds file.
128
74
  test_files: []
data/.gems DELETED
@@ -1,5 +0,0 @@
1
- cutest -v 1.2.2
2
- syro -v 0.0.7
3
- rack -v 1.6.4
4
- seteable -v 1.0.0
5
- rack-test -v 0.6.3
data/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015 Francesco Rodríguez
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1,3 +0,0 @@
1
- class Treant
2
- VERSION = "0.0.1.alpha1"
3
- end
data/makefile DELETED
@@ -1,2 +0,0 @@
1
- default:
2
- cutest -r ./test/helper.rb ./test/*.rb
@@ -1,20 +0,0 @@
1
- require_relative "helper"
2
-
3
- setup do
4
- Driver.new(Treant)
5
- end
6
-
7
- test "captures" do |app|
8
- Treant.define do
9
- on :foo do
10
- on :bar do
11
- res.write(sprintf("%s:%s", inbox[:foo], inbox[:bar]))
12
- end
13
- end
14
- end
15
-
16
- app.get("/foo/bar")
17
-
18
- assert_equal 200, app.res.status
19
- assert_equal "foo:bar", app.res.body
20
- end
@@ -1,27 +0,0 @@
1
- require_relative "helper"
2
-
3
- setup do
4
- Driver.new(Treant)
5
- end
6
-
7
- test "composition" do |app|
8
- class Foo < Treant
9
- end
10
-
11
- Foo.define do
12
- get do
13
- res.write(inbox[:foo])
14
- end
15
- end
16
-
17
- Treant.define do
18
- on "foo" do
19
- run(Foo, foo: 42)
20
- end
21
- end
22
-
23
- app.get("/foo")
24
-
25
- assert_equal 200, app.res.status
26
- assert_equal "42", app.res.body
27
- end
@@ -1,18 +0,0 @@
1
- require_relative "helper"
2
-
3
- setup do
4
- Driver.new(Treant)
5
- end
6
-
7
- test "hello world" do |app|
8
- Treant.define do
9
- get do
10
- res.write("hello world")
11
- end
12
- end
13
-
14
- app.get("/")
15
-
16
- assert_equal 200, app.res.status
17
- assert_equal "hello world", app.res.body
18
- end
@@ -1,19 +0,0 @@
1
- require "cutest"
2
- require "rack/test"
3
- require_relative "../lib/treant"
4
-
5
- prepare do
6
- Treant.reset!
7
- end
8
-
9
- class Driver
10
- include Rack::Test::Methods
11
-
12
- attr_reader :app
13
-
14
- def initialize(app)
15
- @app = app
16
- end
17
-
18
- alias_method :res, :last_response
19
- end
@@ -1,71 +0,0 @@
1
- require_relative "helper"
2
-
3
- class Shrimp
4
- def initialize(app)
5
- @app = app
6
- end
7
-
8
- def call(env)
9
- s, h, resp = @app.call(env)
10
-
11
- return [s, h, resp.reverse]
12
- end
13
- end
14
-
15
- setup do
16
- Driver.new(Treant)
17
- end
18
-
19
- test "use middleware in main application" do |app|
20
- Treant.use(Shrimp)
21
-
22
- class API < Treant
23
- end
24
-
25
- API.define do
26
- get do
27
- res.write("2")
28
- res.write("1")
29
- end
30
- end
31
-
32
- Treant.define do
33
- on "api" do
34
- run(API)
35
- end
36
-
37
- get do
38
- res.write("1")
39
- res.write("2")
40
- end
41
- end
42
-
43
- app.get("/")
44
-
45
- assert_equal 200, app.res.status
46
- assert_equal "21", app.res.body
47
- end
48
-
49
- test "use middleware in child application" do
50
- Treant.define do
51
- run(API)
52
- end
53
-
54
- class API < Treant
55
- use(Shrimp)
56
- end
57
-
58
- API.define do
59
- get do
60
- res.write("1")
61
- res.write("2")
62
- end
63
- end
64
-
65
- app = Driver.new(Treant)
66
-
67
- app.get("/")
68
-
69
- assert_equal 200, app.res.status
70
- assert_equal "21", app.res.body
71
- end
@@ -1,70 +0,0 @@
1
- require_relative "helper"
2
-
3
- module Helper
4
- def clean(str)
5
- return str.strip
6
- end
7
-
8
- module Number
9
- def self.setup(app, options = {})
10
- app.settings[:number] = options.fetch(:number, 1)
11
- end
12
-
13
- def number
14
- return settings[:number]
15
- end
16
- end
17
-
18
- def self.setup(app, options = {})
19
- app.plugin(Number, options)
20
- end
21
-
22
- module ClassMethods
23
- def foo
24
- "foo"
25
- end
26
- end
27
- end
28
-
29
- setup do
30
- Driver.new(Treant)
31
- end
32
-
33
- test "plugin" do |app|
34
- Treant.plugin(Helper)
35
-
36
- Treant.define do
37
- get do
38
- res.write(clean(" foo "))
39
- end
40
- end
41
-
42
- app.get("/")
43
-
44
- assert_equal "foo", app.res.body
45
- assert_equal "foo", Treant.foo
46
- end
47
-
48
- test "setup" do |app|
49
- Treant.plugin(Helper)
50
-
51
- Treant.define do
52
- res.write(number)
53
- end
54
-
55
- app.get("/")
56
-
57
- assert_equal "1", app.res.body
58
- end
59
-
60
- test "setup with options" do |app|
61
- Treant.plugin(Helper, number: 2)
62
-
63
- Treant.define do
64
- res.write(number)
65
- end
66
-
67
- app.get("/")
68
-
69
- assert_equal "2", app.res.body
70
- end
@@ -1,29 +0,0 @@
1
- require_relative "helper"
2
-
3
- test "use settings inside the application" do
4
- Treant.settings[:message] = "hello"
5
-
6
- Treant.define do
7
- get do
8
- res.write(settings[:message])
9
- end
10
- end
11
-
12
- app = Driver.new(Treant)
13
- app.get("/")
14
-
15
- assert_equal "hello", app.res.body
16
- end
17
-
18
- test "settings are inheritable and overridable" do
19
- Treant.settings[:foo] = "bar"
20
-
21
- class Admin < Treant; end
22
-
23
- assert_equal "bar", Admin.settings[:foo]
24
-
25
- Admin.settings[:foo] = "baz"
26
-
27
- assert_equal "bar", Treant.settings[:foo]
28
- assert_equal "baz", Admin.settings[:foo]
29
- end
@@ -1,20 +0,0 @@
1
- require_relative "lib/treant/version"
2
-
3
- Gem::Specification.new do |s|
4
- s.name = "treant"
5
- s.version = Treant::VERSION
6
- s.summary = "treant"
7
- s.description = s.summary
8
- s.authors = ["Francesco Rodríguez"]
9
- s.email = ["frodsan@protonmail.ch"]
10
- s.homepage = "https://github.com/harmoni/treant"
11
- s.license = "MIT"
12
-
13
- s.files = `git ls-files`.split("\n")
14
-
15
- s.add_dependency "syro", "~> 0.0.7"
16
- s.add_dependency "rack", "~> 1.6.4"
17
- s.add_dependency "seteable", "1.0.0"
18
- s.add_development_dependency "cutest", "1.2.2"
19
- s.add_development_dependency "rack-test", "0.6.3"
20
- end