contracter 1.0.5 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b32c9db01703d573151ddce123c0403746afa7e1
4
- data.tar.gz: 788477762465d109407c983cd3faf77354f8e4ea
3
+ metadata.gz: 5e8ab75360b23d74f04742fffe810649c520bed5
4
+ data.tar.gz: 8ffb585e5114bbdf3c9b14ab5ff225b711fa209d
5
5
  SHA512:
6
- metadata.gz: 64f3a1fa55628f59bad3d080778cd70a910ac44e34d47d4951d5faf7b3fcbe55f137a036ce40551a89a1434f87534a5ed6e2c4f8f1af0e783a07b7d3a4387b35
7
- data.tar.gz: 54d3847f29a36a0d8d9a39bc6946d2f09cd9a1b3b390500256484e345006fee1be5e66eee198faae78d5b53f2334901bc9f1b5e996414769618db6ce5b16aee3
6
+ metadata.gz: 988d156e1a4552c2f8466aaee3f40c53d398e334982fc2cb4fd78bb1c4e99a7faa86deaf6b0ffc4a676c5100586c1ce9074fe346b5dc3afe9271b14c6080169c
7
+ data.tar.gz: 374c18db65abad1e2b919c63e4ad74f1417fd20264bec0982b2ca517fe38141d3d84eeb8b0ced7de5e0b15265c2165046e9afc1b19a795fa90772ae388f8f137
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Contracter
1
+ ![contracter](assets/banner.png)
2
2
 
3
3
  Contracter is a quality of life gem that provides a simple Domain Specific Language for anyone to use in generating contracts for clients and services from templates. The default template is my personal contract template based on the popular open-source contract available [here](https://stuffandnonsense.co.uk/projects/contract-killer/).
4
4
 
@@ -26,22 +26,22 @@ First, require the gem:
26
26
  require 'contracter'
27
27
  ```
28
28
 
29
- Then generate a contract by using the Contracter DSL syntax:
29
+ Then generate a contract by using the Contracter syntax:
30
30
 
31
31
  ```ruby
32
- contract = Contracter.build do
33
- # Any property can be used here, and will get replaced with the given value in the template where the method name appears in the format [name].
34
- company 'Test Company Please Ignore'
35
-
36
- # Any property given multiple values will be replaced with the values on separate lines.
37
- browsers 'Safari', 'Google Chrome'
32
+ # This fills the default template
33
+ contract = Contracter.build_from do
34
+ # Any property can be used here, and will get replaced with the given value in the template where the method name appears in the format [name]
35
+ test_value 'Test Value Please Ignore'
36
+ end
38
37
 
39
- # If you would like to use your own template, you may explicitly pass Contracter the path to it. The template can be in any plain text UTF-8 encoded format.
40
- template_path 'path/to/your/template.txt'
38
+ # You can also fill a custom template, by passing Contracter the path
39
+ contract = Contracter.build_from 'path/to/custom/template' do
40
+ test_value 'Test Value Please Ignore'
41
41
  end
42
42
  ```
43
43
 
44
- The contract will be built from the template and returned as a Markdown-formatted string.
44
+ The contract will be built from the template and returned as a string.
45
45
 
46
46
  ## Development
47
47
 
data/assets/banner.png ADDED
Binary file
data/assets/banner.xcf ADDED
Binary file
data/lib/contracter.rb CHANGED
@@ -1,19 +1,12 @@
1
- require "contracter/contract"
1
+ Dir["#{File.dirname(__FILE__)}/contracter/**/*.rb"].each { |e| require e unless e == 'version.rb' }
2
2
 
3
3
  class Contracter
4
- def self.build &block
5
- c = Contract.new
4
+ def self.build_from(path = "#{File.expand_path('..', File.dirname(__FILE__))}/data/contracter/template.md", &block)
5
+ c = Contract.new File.read(path, :encoding => "UTF-8")
6
+ c.load_params
6
7
  c.instance_eval(&block)
8
+ c.replace_params
7
9
 
8
- t = (c.instance_variable_get("@template_path") or "#{Gem.datadir('contracter')}/template.md")
9
- self.fill_template(File.read(t, :encoding => "UTF-8"), c)
10
- end
11
-
12
- private
13
- def self.fill_template t, c
14
- c.instance_variables.map(&:to_s).each do |v|
15
- t.gsub!(/\[#{Regexp.quote(v.gsub("@", ""))}\]/, c.instance_variable_get(v))
16
- end
17
- t.gsub(/\[\w+?\]/, "MISSING")
10
+ c.template
18
11
  end
19
12
  end
@@ -1,15 +1,26 @@
1
1
  class Contract
2
- def method_missing(name_sym, *args)
3
- if args.length == 1
4
- self.instance_variable_set "@#{name_sym.to_s}", *args
5
- elsif args.length > 1
6
- self.instance_variable_set "@#{name_sym.to_s}", args.join('\n')
7
- else
8
- raise ArgumentError, 'Value not passed to template.'
9
- end
10
- end
11
-
12
- def template_path p
13
- @template_path = p
2
+ attr_accessor :template
3
+
4
+ def params
5
+ @params ||= Hash.new
6
+ end
7
+
8
+ def initialize p
9
+ @template = p
10
+ end
11
+
12
+ def method_missing(method_sym, *args)
13
+ raise ContractError, "Cannot find [#{method_sym}] in template." unless self.params.has_key?(method_sym)
14
+ raise ContractError, "Must provide only one value for [#{method_sym}]." unless args.length == 1
15
+ self.params[method_sym] = args.first
16
+ end
17
+
18
+ def load_params
19
+ self.template.scan(/\[(\w+?)\]/).flatten.each { |p| self.params[p.to_sym] = nil }
20
+ end
21
+
22
+ def replace_params
23
+ raise ContractError, "Missing values for #{self.params.select { |k, v| v.nil? }.keys.map { |e| "[#{e}]"}.join ', '}." if self.params.has_value? nil
24
+ self.params.each { |k, v| self.template.gsub!(/\[#{Regexp.quote(k.to_s)}\]/, v) }
14
25
  end
15
26
  end
@@ -0,0 +1,2 @@
1
+ class ContractError < StandardError
2
+ end
@@ -1,3 +1,3 @@
1
1
  class Contracter
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contracter
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
  - Remi Gelinas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2015-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,12 +65,15 @@ files:
65
65
  - Gemfile
66
66
  - README.md
67
67
  - Rakefile
68
+ - assets/banner.png
69
+ - assets/banner.xcf
68
70
  - bin/console
69
71
  - bin/setup
70
72
  - contracter.gemspec
71
73
  - data/contracter/template.md
72
74
  - lib/contracter.rb
73
75
  - lib/contracter/contract.rb
76
+ - lib/contracter/errors/contracterror.rb
74
77
  - lib/contracter/version.rb
75
78
  homepage: https://github.com/remi-gelinas/contracter
76
79
  licenses: []
@@ -91,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
94
  version: '0'
92
95
  requirements: []
93
96
  rubyforge_project:
94
- rubygems_version: 2.4.5.1
97
+ rubygems_version: 2.5.1
95
98
  signing_key:
96
99
  specification_version: 4
97
100
  summary: A quality of life gem for templated contract generation.