honor_codes 0.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.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .ruby-version
19
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in honor_codes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robbie Gill
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,32 @@
1
+ # HonorCodes
2
+
3
+ Generate honor code style licences.
4
+
5
+ The 'license' is entirely reversible and is not intended to protect from malicious use, but provides a nice generator
6
+ and interpreter for creating non-directly editable licences or config files.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'honor_codes'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install honor_codes
21
+
22
+ ## Usage
23
+
24
+ View the help with `bundle exec honor_codes` and `bundle exec honor_codes help <command>`
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it ( http://github.com/robbiegill/honor_codes/fork )
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/honor_codes ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Exit cleanly from an early interrupt
4
+ Signal.trap("INT") { exit 1 }
5
+
6
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
7
+
8
+ require 'honor_codes'
9
+ require 'honor_codes/cli'
10
+
11
+ HonorCodes::CLI.start(ARGV, :debug => true)
@@ -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 'honor_codes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'honor_codes'
8
+ spec.version = HonorCodes::VERSION
9
+ spec.authors = ['Robbie Gill']
10
+ spec.email = ['gill.rob@gmail.com']
11
+ spec.summary = %q{Generate honor code style licences}
12
+ spec.homepage = 'https://github.com/robbiegill/honor_codes'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = %w{honor_codes}
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = %w{lib}
19
+
20
+ spec.add_runtime_dependency 'thor', '~> 0.18.1'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec', '~> 3.0.0.beta1'
25
+ end
@@ -0,0 +1,44 @@
1
+ require 'thor'
2
+ require 'pp'
3
+ require 'honor_codes'
4
+ require 'honor_codes/generators/template'
5
+
6
+ module HonorCodes
7
+ class CLI < Thor
8
+ package_name 'HonorCodes'
9
+
10
+ desc 'generate_from LICENSE_TEMPLATE [options]', 'generate license from LICENSE_TEMPLATE'
11
+ long_desc <<-LONGDESC
12
+ Generates a simple license from a supplied yaml file. The resulting license will be dumped
13
+ into the same directory as the LICENSE_TEMPLATE.
14
+ LONGDESC
15
+ method_option :filename, :default => HonorCodes::LICENSE_NAME, :aliases => '-f', :desc => 'sets the output filename'
16
+ method_option :print, :type => :boolean, :lazy_default => true, :aliases => '-p', :desc => 'prints the interpreted license'
17
+ def generate_from(template_path)
18
+ license_path = HonorCodes.generate template_path, options[:filename]
19
+ puts %Q(License stored to: #{license_path})
20
+ puts inspect license_path if options[:print]
21
+ end
22
+
23
+ desc 'inspect LICENSE_FILE', 'inspect LICENSE_FILE'
24
+ long_desc <<-LONGDESC
25
+ Pretty prints the contents of the license.
26
+ LONGDESC
27
+ def inspect(license_path=HonorCodes::LICENSE_NAME)
28
+ puts HonorCodes.interpret license_path
29
+ end
30
+
31
+ desc 'template [option ...]', 'generate a license template.yml'
32
+ long_desc <<-LONGDESC
33
+ Generate a license_template.yml file by providing space separated options
34
+
35
+ option: key[:value[:type]] #types include int, date. If no type is supplied, the value will be a string
36
+
37
+ Example:
38
+ honor_code template users:100:int vendor:my_company expires:2015-12-31:date
39
+ LONGDESC
40
+ def template(*fields)
41
+ HonorCodes::Generators::Template.start fields
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,80 @@
1
+ require 'pathname'
2
+ require 'yaml'
3
+ require 'base64'
4
+
5
+ module HonorCodes
6
+ LICENSE_NAME = 'honor_code.license'
7
+
8
+ def self.generate(template_path, filename)
9
+ Generator.new(template_path, filename).generate
10
+ end
11
+
12
+ def self.interpret(license_path)
13
+ Interpreter.new(license_path).interpret
14
+ end
15
+
16
+ def self.make_template(props)
17
+ TemplateMaker.new(props).make_template
18
+ end
19
+
20
+ class Generator
21
+ attr_accessor :template_path, :filename
22
+
23
+ def initialize(template_path, filename)
24
+ self.template_path = template_path
25
+ self.filename = filename
26
+ end
27
+
28
+ def generate
29
+ template = Pathname.new(template_path)
30
+ license = template.dirname.join(filename)
31
+ license.open('w') do |file|
32
+ file << Base64.encode64(template.read)
33
+ end
34
+ license.to_s
35
+ end
36
+ end
37
+
38
+ class Interpreter
39
+ attr_accessor :license_path
40
+
41
+ def initialize(license_path)
42
+ self.license_path = license_path
43
+ end
44
+
45
+ def interpret
46
+ license = Pathname.new license_path
47
+ YAML.load Base64.decode64(license.read)
48
+ end
49
+ end
50
+
51
+ class TemplateMaker
52
+ attr_accessor :props
53
+
54
+ def initialize(props)
55
+ self.props = props
56
+ end
57
+
58
+ def make_template
59
+ content = props.inject({}) do |memo, prop|
60
+ key, default, type = prop.split ':'
61
+ memo[key] = convert_type default, type
62
+ memo
63
+ end
64
+ YAML.dump :license => content
65
+ end
66
+
67
+ private
68
+
69
+ def convert_type(val, type)
70
+ case type
71
+ when /(int|integer)/
72
+ Integer(val)
73
+ when /(date)/
74
+ Date.parse(val)
75
+ else
76
+ val
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,15 @@
1
+ require 'thor/group'
2
+ require 'yaml'
3
+
4
+ module HonorCodes
5
+ module Generators
6
+ class Template < Thor::Group
7
+ include Thor::Actions
8
+
9
+ argument :properties, :type => :array
10
+ def write_template
11
+ create_file 'license_template.yml', HonorCodes.make_template(properties)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module HonorCodes
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ PATCH = 0
5
+
6
+ VERSION = [MAJOR, MINOR, PATCH].join('.')
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'honor_codes/version'
2
+ require 'honor_codes/core'
3
+
4
+ module HonorCodes
5
+
6
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+ require 'honor_codes'
3
+
4
+ describe HonorCodes do
5
+ let(:license_template_dir) { Pathname.new '/tmp/spec/honor_codes' }
6
+ let(:license_template_path) { Pathname.new '/tmp/spec/honor_codes/license_template.yml' }
7
+ let(:license_template_content) do
8
+ <<-EOF
9
+ top_level:
10
+ fixnum_key: #{fixnum_key}
11
+ date_key: #{date_key}
12
+ last_key: #{last_key}
13
+ EOF
14
+ end
15
+ let(:license_filename) { 'custom.file' }
16
+ let(:generated_license_pathname) { license_template_dir.join(license_filename) }
17
+
18
+ let(:fixnum_key) { 5 }
19
+ let(:date_key) { '2014-12-31' }
20
+ let(:last_key) { 'last_value' }
21
+
22
+ before do
23
+ FileUtils.rm_rf(license_template_dir)
24
+ FileUtils.mkdir_p(license_template_dir)
25
+ license_template_path.open('w') { |file| file << license_template_content }
26
+ end
27
+
28
+ describe '.generate' do
29
+ context 'when passed a yml file and output filename' do
30
+ before do
31
+ described_class.generate license_template_path.to_s, license_filename
32
+ end
33
+
34
+ it %Q{creates a #{HonorCodes::LICENSE_NAME} file in the same directory} do
35
+ expect(generated_license_pathname).to exist
36
+ end
37
+
38
+ it 'does not write pure yaml' do
39
+ license_content = generated_license_pathname.read
40
+ expect(YAML.load license_content).not_to eq(YAML.load license_template_content)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '.interpret' do
46
+ let(:result) { described_class.interpret generated_license_pathname.to_s }
47
+
48
+ before do
49
+ described_class.generate license_template_path.to_s, license_filename
50
+ end
51
+
52
+ context 'when passed a honor_code.license file' do
53
+ it 'reads the content into a license object with ruby types' do
54
+ expect(result).to eq(YAML.load license_template_content)
55
+ top_level = result['top_level']
56
+ expect(top_level['fixnum_key']).to be_a(Fixnum)
57
+ expect(top_level['last_key']).to eq(last_key)
58
+ expect(top_level['date_key']).to eq(Date.parse date_key)
59
+ end
60
+ end
61
+ end
62
+
63
+ describe '.make_template' do
64
+ let(:props) { %w( some_key:some_value other_key:100 ) }
65
+ let(:content) { described_class.make_template props }
66
+ let(:license_hash) { {'some_key' => 'some_value', 'other_key' => '100'} }
67
+ let(:expected) do
68
+ YAML.dump({:license => license_hash})
69
+ end
70
+
71
+ it 'splits elements on : and dumps yaml of the hash nested under :license' do
72
+ expect(content).to eq(expected)
73
+ end
74
+
75
+ context 'with data types' do
76
+ let(:props) { %w( int_key:1:integer string_key_type:val:string key_default_type:13 date_key:2014-12-31:date ) }
77
+ let(:license_hash) { {'int_key' => 1, 'string_key_type' => 'val', 'key_default_type' => '13', 'date_key' => Date.parse('2014-12-31')} }
78
+
79
+ it 'casts to the correct types in the yaml' do
80
+ expect(content).to eq(expected)
81
+ end
82
+
83
+ context 'loading the produced template' do
84
+ it 'is read with the same data types' do
85
+ read_license = YAML.load(content)[:license]
86
+ expect(read_license).to eq(license_hash)
87
+ expect(read_license['key_default_type']).to be_a(String)
88
+ expect(read_license['int_key']).to be_a(Fixnum)
89
+ expect(read_license['string_key_type']).to be_a(String)
90
+ expect(read_license['date_key']).to be_a(Date)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ config.color_enabled = true
3
+ config.tty = true
4
+ config.formatter = :documentation
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: honor_codes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Robbie Gill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.18.1
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.1
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '1.5'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.5'
43
+ none: false
44
+ prerelease: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: !binary |-
53
+ MA==
54
+ none: false
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: !binary |-
60
+ MA==
61
+ none: false
62
+ prerelease: false
63
+ type: :development
64
+ - !ruby/object:Gem::Dependency
65
+ name: rspec
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: 3.0.0.beta1
71
+ none: false
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 3.0.0.beta1
77
+ none: false
78
+ prerelease: false
79
+ type: :development
80
+ description:
81
+ email:
82
+ - gill.rob@gmail.com
83
+ executables:
84
+ - honor_codes
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - ".gitignore"
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - bin/honor_codes
94
+ - honor_codes.gemspec
95
+ - lib/honor_codes.rb
96
+ - lib/honor_codes/cli.rb
97
+ - lib/honor_codes/core.rb
98
+ - lib/honor_codes/generators/template.rb
99
+ - lib/honor_codes/version.rb
100
+ - spec/honor_codes_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/robbiegill/honor_codes
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: !binary |-
114
+ MA==
115
+ none: false
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: !binary |-
121
+ MA==
122
+ none: false
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.24
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Generate honor code style licences
129
+ test_files:
130
+ - spec/honor_codes_spec.rb
131
+ - spec/spec_helper.rb