policy 1.0.5 → 1.1.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
2
  SHA1:
3
- metadata.gz: 707dc12513a50ae6567790a05ea8ab848f7055e0
4
- data.tar.gz: f28991b4dad44e835bfa43790b49fa3798a64921
3
+ metadata.gz: c43acc2f25d6b3056f4702697e77e317b6aca3e2
4
+ data.tar.gz: b79816c8eb97f48bfab2e17269942b88bec0f3f2
5
5
  SHA512:
6
- metadata.gz: ffe5458110d6d8fd68558fe42018f9c3ad27559f1444ded2cd1b0e46b3d7ac0938b68b8bbec5326ef0cbd58ea887e3571c48f64ee1397a82e4965be5c1a83bba
7
- data.tar.gz: cbe8335492c764aeaf6477470b848ecf4b3f21bf211681fcd06be7a3e05c430092fba3d9cd4f969f7edd6416c714c964bb9acbcbf16118917af8225493b70521
6
+ metadata.gz: de03f24c1ef2c65f67e6ad2a1f10c62da9e9cb31e47fa4860ed4e38fa02b887777bea518a885cd5a5ddd447e5292990b422fb8740344f79a1d66f2bee82b8ce4
7
+ data.tar.gz: 0903e3428ce0f25221340ff9f9ea7919f32f811204448da80cbd31f697e75b7f846e2116a4d2bc7be7a8d1759d9df0bc7de1b82216c52ca75d908677ee5a4544
data/.metrics CHANGED
@@ -1,5 +1,8 @@
1
1
  # Settings for metric_fu and its packages are collected in the `config/metrics`
2
2
  # and loaded by the Hexx::Suit::Metrics::MetricFu.
3
3
 
4
- require "hexx-suit"
5
- Hexx::Suit::Metrics::MetricFu.load
4
+ begin
5
+ require "hexx-suit"
6
+ Hexx::Suit::Metrics::MetricFu.load
7
+ rescue LoadError
8
+ end
data/Guardfile CHANGED
@@ -6,6 +6,8 @@ guard :rspec, cmd: "bundle exec rspec" do
6
6
 
7
7
  watch("lib/policy.rb") { "spec" }
8
8
 
9
+ watch("lib/policy/cli/*.*") { "spec/tests/policy/cli_spec.rb" }
10
+
9
11
  watch(/^lib(.+)\.rb$/) do |m|
10
12
  "spec/tests#{ m[1] }_spec.rb"
11
13
  end
data/README.md CHANGED
@@ -101,6 +101,20 @@ end
101
101
 
102
102
  Note a policy knows nothing about the complex nature of its attributes until their quack like transactions with `#sum` method defined.
103
103
 
104
+ ### Scaffolding a Policy
105
+
106
+ You can scaffold the policy with its specification and necessary translations using the generator:
107
+
108
+ ```
109
+ policy new
110
+ ```
111
+
112
+ For a list of available options call the generator with a `-h` option:
113
+
114
+ ```
115
+ policy new -h
116
+ ```
117
+
104
118
  [Struct]: http://ruby-doc.org//core-2.2.0/Struct.html
105
119
  [ActiveModel::Validations]: http://apidock.com/rails/ActiveModel/Validations
106
120
 
data/bin/policy ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require "policy/cli"
3
+
4
+ # Scaffolders runner from the command line
5
+ class CLI < Thor
6
+
7
+ register(
8
+ Policy::CLI,
9
+ "new",
10
+ "new NAME[ -a attribute[ attribute]][ -l en[ ru]][ -n namespace]",
11
+ "Scaffolds a policy object with a specification and translations."
12
+ )
13
+
14
+ end # class CLI
15
+
16
+ CLI.start
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ # Translations for error messages, generated by policy objects of type:
3
+ # <%= policy.type %>
4
+ ---
5
+ # <%= @locale %>:
6
+ # activemodel:
7
+ # errors:
8
+ # models:
9
+ # <%= policy.path %>:
10
+ # attributes:
11
+ # base:
12
+ <% attributes.each do |item| -%>
13
+ # <%= item %>:
14
+ <% end -%>
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ <% tabs = 0 -%>
3
+ <% policy.namespaces.each do |item| -%>
4
+
5
+ <%= " " * tabs %>module <%= item %>
6
+ <% tabs += 1 -%>
7
+ <% end -%>
8
+
9
+ <%= " " * tabs %># Implements the following policy (invariant):
10
+ <%= " " * tabs %>#
11
+ <%= " " * tabs %># @todo: describe the policy
12
+ <%= " " * tabs %>#
13
+ <%= " " * tabs %># @example
14
+ <%= " " * tabs %># <%= policy.type %>.new(<%= attributes.join(", ") %>)
15
+ <%= " " * tabs %>#
16
+ <% attributes.each do |item| -%>
17
+ <%= " " * tabs %># @param [@todo] <%= item %>
18
+ <%= " " * tabs %># @todo: describe the attribute
19
+ <% end -%>
20
+ <%= " " * tabs %>class <%= policy.const %> < Hexx::Policy.new(<%= attributes.map { |item| ":#{ item }" }.join(", ") %>)
21
+
22
+ <%= " " * tabs %> # Define necessary validations using `validates` or `validate` declarations.
23
+
24
+ <%= " " * tabs %>end # class <%= policy.const %>
25
+ <% policy.namespaces.reverse.each do |item| -%>
26
+
27
+ <% tabs -= 1 -%>
28
+ <%= " " * tabs %>end # module <%= item %>
29
+ <% end -%>
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ describe <%= policy.type %> do
4
+
5
+ # The specification checks whether the policy object is valid or not,
6
+ # depending on values of its attributes.
7
+
8
+ # default attributes for a valid policy
9
+ <% attributes.each do |item| -%>
10
+ # let(:<%= item %>) { @todo }
11
+ <% end -%>
12
+
13
+ subject { described_class.new <%= attributes.join(", ") %> }
14
+
15
+ context "when @todo: describe the context" do
16
+
17
+ it { is_expected.to be_valid }
18
+
19
+ end # context
20
+ <% attributes.each do |item| -%>
21
+
22
+ context "when <%= item %> @todo" do
23
+
24
+ # let(:<%= item %>) { @todo }
25
+ it { is_expected.not_to be_valid }
26
+
27
+ end # context
28
+ <% end -%>
29
+
30
+ end # describe <%= policy.type %>
data/lib/policy/cli.rb ADDED
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require "hexx-cli"
3
+
4
+ module Policy
5
+
6
+ # Scaffolds a policy with a specification and translations
7
+ class CLI < Hexx::CLI::Base
8
+
9
+ # @private
10
+ def self.source_root
11
+ ::File.expand_path "../cli", __FILE__
12
+ end
13
+
14
+ desc "Scaffolds a policy object with a specification and translations."
15
+ namespace :new
16
+
17
+ argument(
18
+ :name,
19
+ banner: "NAME",
20
+ desc: "The name of the policy.",
21
+ type: :string
22
+ )
23
+
24
+ class_option(
25
+ :attributes,
26
+ aliases: %w(-a),
27
+ banner: " debet credit",
28
+ default: %w(),
29
+ desc: "The list of attributes of the policy (the order is essential).",
30
+ type: :array
31
+ )
32
+
33
+ class_option(
34
+ :namespace,
35
+ aliases: %w(-n),
36
+ banner: " policies financial",
37
+ default: %w(policies),
38
+ desc: "The namespace for the policy.",
39
+ type: :array
40
+ )
41
+
42
+ class_option(
43
+ :locales,
44
+ aliases: %w(-l),
45
+ banner: " en jp",
46
+ default: %w(en ru),
47
+ desc: "The list of locales for policy messages' translations.",
48
+ type: :array
49
+ )
50
+
51
+ # @private
52
+ def add_spec
53
+ template "spec.erb", "spec/tests/#{ policy.path }_spec.rb"
54
+ end
55
+
56
+ # @private
57
+ def add_policy
58
+ template "policy.erb", "app/#{ policy.path }.rb"
59
+ end
60
+
61
+ # @private
62
+ def add_locales
63
+ locales.each do |locale|
64
+ @locale = locale
65
+ template "locale.erb", "config/locales/#{ policy.path }/#{ locale }.yml"
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def policy
72
+ @policy ||= Hexx::CLI::Name.new [options[:namespace], name].join("/")
73
+ end
74
+
75
+ def attributes
76
+ @attributes ||= options[:attributes].map(&:downcase)
77
+ end
78
+
79
+ def locales
80
+ @locales ||= options[:locales].map(&:downcase)
81
+ end
82
+
83
+ end # class CLI
84
+
85
+ end # module Policy
@@ -4,6 +4,6 @@ module Policy
4
4
 
5
5
  # The semantic version of the module.
6
6
  # @see http://semver.org/ Semantic versioning 2.0
7
- VERSION = "1.0.5".freeze
7
+ VERSION = "1.1.0".freeze
8
8
 
9
9
  end # module Hexx
data/policy.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.license = "MIT"
14
14
 
15
15
  gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
16
+ gem.executables = ["policy"]
16
17
  gem.test_files = Dir["spec/**/*.rb"]
17
18
  gem.extra_rdoc_files = Dir["README.md", "LICENSE"]
18
19
  gem.require_paths = ["lib"]
@@ -20,6 +21,7 @@ Gem::Specification.new do |gem|
20
21
  gem.required_ruby_version = "~> 2.0"
21
22
  gem.add_runtime_dependency "activemodel", ">= 3.1"
22
23
  gem.add_runtime_dependency "adamantium", "~> 0.2"
23
- gem.add_development_dependency "hexx-rspec", "~> 0.1"
24
+ gem.add_runtime_dependency "hexx-cli", "~> 0.0"
25
+ gem.add_development_dependency "hexx-rspec", "~> 0.3"
24
26
 
25
27
  end # Gem::Specification
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ describe "$ policy new", :sandbox, :capture do
4
+
5
+ let(:argv) { %w(foo) }
6
+
7
+ before { try_in_sandbox { `policy new #{ argv.join(" ") }` } }
8
+
9
+ it "runs a scaffolder" do
10
+ %w(
11
+ app/policies/foo.rb
12
+ config/locales/policies/foo/en.yml
13
+ config/locales/policies/foo/ru.yml
14
+ spec/tests/policies/foo_spec.rb
15
+ ).each { |file| expect(file).to be_present_in_sandbox }
16
+ end
17
+
18
+ end # describe $ policy new
@@ -0,0 +1,127 @@
1
+ # encoding: utf-8
2
+ require "policy/cli"
3
+
4
+ describe Policy::CLI, :sandbox, :capture do
5
+
6
+ describe ".start" do
7
+
8
+ subject { try_in_sandbox { described_class.start options } }
9
+
10
+ shared_examples "adding a policy" do |folder:|
11
+
12
+ let(:filename) { File.join("app", folder, "foo.rb") }
13
+
14
+ it "[creates file]" do
15
+ expect(filename).to be_present_in_sandbox
16
+ end
17
+
18
+ end # examples
19
+
20
+ shared_examples "adding a spec" do |folder:|
21
+
22
+ let(:filename) { File.join("spec/tests", folder, "foo_spec.rb") }
23
+
24
+ it "[creates file]" do
25
+ expect(filename).to be_present_in_sandbox
26
+ end
27
+
28
+ end # examples
29
+
30
+ shared_examples "adding locales" do |locales, folder:|
31
+
32
+ let(:dirname) { File.join("config/locales", folder, "foo") }
33
+
34
+ it "[creates files]" do
35
+ locales.each do |locale|
36
+ expect("#{ dirname }/#{ locale }.yml").to be_present_in_sandbox
37
+ end
38
+ end
39
+
40
+ end # examples
41
+
42
+ context "foo" do
43
+
44
+ let(:options) { %w(foo) }
45
+ before { subject }
46
+
47
+ it_behaves_like "adding a policy", folder: "policies"
48
+ it_behaves_like "adding a spec", folder: "policies"
49
+ it_behaves_like "adding locales", %w(en ru), folder: "policies"
50
+
51
+ end # context
52
+
53
+ context "foo -a bar baz" do
54
+
55
+ let(:options) { %w(foo -a bar baz) }
56
+ before { subject }
57
+
58
+ it_behaves_like "adding a policy", folder: "policies"
59
+ it_behaves_like "adding a spec", folder: "policies"
60
+ it_behaves_like "adding locales", %w(en ru), folder: "policies"
61
+
62
+ it "adds attributes to the policy" do
63
+ content = read_in_sandbox "app/policies/foo.rb"
64
+ expect(content).to include "class Foo < Hexx::Policy.new(:bar, :baz)"
65
+ end
66
+
67
+ it "checks attributes" do
68
+ content = read_in_sandbox "spec/tests/policies/foo_spec.rb"
69
+ expect(content).to include "let(:bar) {"
70
+ expect(content).to include "let(:baz) {"
71
+ end
72
+
73
+ end # context
74
+
75
+ context "foo -n bar/baz" do
76
+
77
+ let(:options) { %w(foo -n bar/baz) }
78
+ before { subject }
79
+
80
+ it_behaves_like "adding a policy", folder: "bar/baz"
81
+ it_behaves_like "adding a spec", folder: "bar/baz"
82
+ it_behaves_like "adding locales", %w(en ru), folder: "bar/baz"
83
+
84
+ it "adds namespace to the policy" do
85
+ content = read_in_sandbox "app/bar/baz/foo.rb"
86
+ expect(content).to include "module Bar"
87
+ expect(content).to include " module Baz"
88
+ end
89
+
90
+ it "describes the specification" do
91
+ content = read_in_sandbox "spec/tests/bar/baz/foo_spec.rb"
92
+ expect(content).to include "describe Bar::Baz::Foo do"
93
+ end
94
+
95
+ it "creates the proper locale" do
96
+ content = read_in_sandbox "config/locales/bar/baz/foo/en.yml"
97
+ expect(content).to include "# en:"
98
+ expect(content).to include " bar/baz/foo:"
99
+ end
100
+
101
+ end # context
102
+
103
+ context "foo -n bar baz" do
104
+
105
+ let(:options) { %w(foo -n bar baz) }
106
+ before { subject }
107
+
108
+ it_behaves_like "adding a policy", folder: "bar/baz"
109
+ it_behaves_like "adding a spec", folder: "bar/baz"
110
+ it_behaves_like "adding locales", %w(en ru), folder: "bar/baz"
111
+
112
+ end # context
113
+
114
+ context "foo -l ua jp" do
115
+
116
+ let(:options) { %w(foo -l ua jp) }
117
+ before { subject }
118
+
119
+ it_behaves_like "adding a policy", folder: "policies"
120
+ it_behaves_like "adding a spec", folder: "policies"
121
+ it_behaves_like "adding locales", %w(ua jp), folder: "policies"
122
+
123
+ end # context
124
+
125
+ end # describe .start
126
+
127
+ end # describe Policy::CLI
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: policy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-16 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -38,23 +38,38 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hexx-cli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '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: '0.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: hexx-rspec
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: '0.1'
61
+ version: '0.3'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '0.1'
68
+ version: '0.3'
55
69
  description: A tiny library implementing the Policy Object pattern.
56
70
  email: andrew.kozin@gmail.com
57
- executables: []
71
+ executables:
72
+ - policy
58
73
  extensions: []
59
74
  extra_rdoc_files:
60
75
  - README.md
@@ -72,6 +87,7 @@ files:
72
87
  - LICENSE
73
88
  - README.md
74
89
  - Rakefile
90
+ - bin/policy
75
91
  - config/metrics/STYLEGUIDE
76
92
  - config/metrics/cane.yml
77
93
  - config/metrics/churn.yml
@@ -85,6 +101,10 @@ files:
85
101
  - config/metrics/simplecov.yml
86
102
  - config/metrics/yardstick.yml
87
103
  - lib/policy.rb
104
+ - lib/policy/cli.rb
105
+ - lib/policy/cli/locale.erb
106
+ - lib/policy/cli/policy.erb
107
+ - lib/policy/cli/spec.erb
88
108
  - lib/policy/follower.rb
89
109
  - lib/policy/follower/followed_policies.rb
90
110
  - lib/policy/follower/followed_policy.rb
@@ -96,6 +116,8 @@ files:
96
116
  - policy.gemspec
97
117
  - spec/features/follower_spec.rb
98
118
  - spec/spec_helper.rb
119
+ - spec/tests/bin/policy_spec.rb
120
+ - spec/tests/policy/cli_spec.rb
99
121
  - spec/tests/policy/follower/followed_policies_spec.rb
100
122
  - spec/tests/policy/follower/followed_policy_spec.rb
101
123
  - spec/tests/policy/follower/names_spec.rb
@@ -131,6 +153,8 @@ summary: Policy Objects for Ruby.
131
153
  test_files:
132
154
  - spec/spec_helper.rb
133
155
  - spec/tests/policy_spec.rb
156
+ - spec/tests/bin/policy_spec.rb
157
+ - spec/tests/policy/cli_spec.rb
134
158
  - spec/tests/policy/follower_spec.rb
135
159
  - spec/tests/policy/interface_spec.rb
136
160
  - spec/tests/policy/validations_spec.rb