traitorous 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0597d522d23bd2c71b7f3c32c4ba7051354855e9
4
+ data.tar.gz: 74bafa4cbd011ab8ab2e337a12d7f5c2294288b0
5
+ SHA512:
6
+ metadata.gz: 4fd180776568fd37efb6663e4f2dc0635e58973dcd55d7803c7cec0680de77b50b811802897b1ae2e26a855727cfc7d513f764c2aeefd42b35f08f5ad864c685
7
+ data.tar.gz: c891627cf1985ec7be19f239c81d9e78bb99c5648678fffe063c9c1f26599d5aa9315f8f29766107a4a1bf6aba305f2a5434eafe74ec1b065663894b5c30cf96
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ rvm:
5
+ - 2.1
6
+ - 2.2
7
+ addons:
8
+ code_climate:
9
+ repo_token:
10
+ secure: kulnvpdGe7vQOyWXYJdCKesoAwhSGSPZUChh8mzxhOk6tedPtQ0CTC/FehWKdd/gyktgBVTn3OT/rqqsqVrOhOea7KQ5QpfvhZi8Yx7nlrw+zF/Z1eTPjCS+MNOpkJMLuXaC7PhmnJVzE2htjZ1Wca1lmOA+wl4ZiM5HtPlXKDagiEZjNGvTeRy0JqSQIXO991SbdV5WBXvNgPk3pUMNHRJjhD2rrmWkBI7ucsEpZfhKtMVbxYku52jOTnzm3PUe6hDXJ+SnOjc5wTgnYl9UdLbdUQAInUpyTRR5toRkSCqZ88wfYMqcWdITZatifZ8qrF91eX5I1HpExzRrui6r7xSYMNJ6Y/GGOhk1NOHwHx8SpxDk+lOciWWV0vlfrFCVF8C5hy8K8yE/5wPL5m7GgQguqhpTTfMhuRPw2ozcqczLxJKOKo5TqkeBm9frpFlKBOAydYB96rq9uRliEqchfPhDB4C8vbtpjpA9Z8GJdQZoJd4V4urZhxKwC8ItqB7acZrtZ/fgBiSM9+u2e/C7IBcWk9l7jKRW0jbsI1BFZy1MrbkbvS8xmIc+FxYU1CdNu0KXH5tCKrXoJxVTsLCUh/doOrSWfx30prkGVDyJQs6E0NIUPfywXjqQUlPtUgAd3PxWfCA9ZKY/Mug6qS6fdqowUBeUKk7fux9x5dAMAgk=
data/.travis.yml.orig ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ rvm:
5
+ - 2.1
6
+ - 2.2
7
+ addons:
8
+ code_climate:
9
+ repo_token: secret_key_redacted
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in traitorous.gemspec
4
+ gemspec
5
+
6
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/Guardfile ADDED
@@ -0,0 +1,23 @@
1
+ # Note: The cmd option is now required due to the increasing number of ways
2
+ # rspec may be run, below are examples of the most common uses.
3
+ # * bundler: 'bundle exec rspec'
4
+ # * bundler binstubs: 'bin/rspec'
5
+ # * spring: 'bin/rspec' (This will use spring if running and you have
6
+ # installed the spring binstubs per the docs)
7
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
8
+ # * 'just' rspec: 'rspec'
9
+
10
+ guard :rspec, cmd: "bundle exec rspec", all_after_pass: true, all_on_start: true do
11
+ require "guard/rspec/dsl"
12
+ dsl = Guard::RSpec::Dsl.new(self)
13
+
14
+ # Feel free to open issues for suggestions and improvements
15
+
16
+ # RSpec files
17
+ rspec = dsl.rspec
18
+ watch(rspec.spec_helper) { rspec.spec_dir }
19
+ watch(rspec.spec_support) { rspec.spec_dir }
20
+ watch(rspec.spec_files)
21
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
22
+
23
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 scott m parrish
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.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Traitorous
2
+
3
+ [![Build Status](https://travis-ci.org/anithri/traitorous.svg)](https://travis-ci.org/anithri/traitorous)
4
+ [![Code Climate](https://codeclimate.com/github/anithri/traitorous/badges/gpa.svg)](https://codeclimate.com/github/anithri/traitorous)
5
+
6
+ This is a simple trait based system that emphasizes reading in and out data
7
+ structures, using an plugin Converter system. Each trait defined has a
8
+ name and a Converter obj that responds to :export, and :import.
9
+
10
+ This process came out of a need to have a flexible config system that could be
11
+ read from files easily (yaml, json, f*$k xml), populate a nested set of objects,
12
+ and able to export the while thing ready to be encoded and saved back to disk.
13
+
14
+ The converters can be used to help (de)serialize, set default values, do
15
+ validations and translations. The converter's job is to be able to import a
16
+ value, instantiating into classes and assigning values.
17
+
18
+ I took a lot of inspiration from [virtus](https://github.com/solnic/virtus).
19
+
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'traitorous'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install traitorous
36
+
37
+ ## Usage
38
+
39
+ ```ruby
40
+
41
+ # see spec/
42
+ require 'traitorous'
43
+
44
+ class Ruin
45
+ include Traitorous
46
+ include Traitorous::Equality
47
+ trait :name
48
+ trait :danger
49
+ end
50
+
51
+ r = Ruin.new(name: 'Skull Mountain', danger: 'The Devil of')
52
+ #
53
+ puts r.name
54
+ # Skull Mountain
55
+
56
+ puts r.danger
57
+ # The Devil of
58
+
59
+ puts r.export
60
+ # {'name' => 'Skull Mountain, 'danger' => 'The Devil of'}
61
+
62
+ puts Ruin.new(r.export) == r
63
+ # true
64
+
65
+ class Area
66
+ include Traitorous
67
+ include Traitorous::Equality
68
+ trait :name
69
+ trait :size, Converter::DefaultValueStatic.new('sub-continent')
70
+ trait :ruins, Converter::UniformArray.new(Ruin)
71
+ end
72
+
73
+ area = Area.new(
74
+ name: 'Western Marches',
75
+ size: 'Region',
76
+ ruins: [{name: 'Skull Mountain', danger: 'The Devil of'},
77
+ {name: 'Dire Swamp', danger: 'The Devil of'}
78
+ ]
79
+ )
80
+ puts area.ruins.length
81
+ # 2
82
+ puts area.export
83
+ # {:name=>"Western Slope", :size=>"region", :ruins=>[{:name=>"Dire Swamp", :danger=>"The Creature of"}, {:name=>"Skull Mountain", :danger=>"The Devil of"}]}
84
+ puts Area.new(area.export) == area
85
+ # true
86
+ ```
87
+
88
+ ## Development
89
+
90
+ I use Guard to automate testing. It won't affect anything execpt the disk space
91
+ needed to store the gems. If you do want to use it, from a shell in the home
92
+ directory of the gem and type `guard`.
93
+
94
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
95
+
96
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
97
+
98
+ ## Contributing
99
+
100
+ 1. Fork it ( https://github.com/[my-github-username]/traitorous/fork )
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ #require 'rake/version_task'
3
+ require 'rspec/core/rake_task'
4
+
5
+ #Rake::VersionTask.new
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
10
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "traitorous"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ module Traitorous
2
+ module Converter
3
+ class DefaultValueStatic
4
+ def initialize(default_value)
5
+ @default_value = default_value
6
+ end
7
+ def do_import(opts)
8
+ opts || @default_value
9
+ end
10
+ def do_export(data)
11
+ data
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module Traitorous
2
+ module Converter
3
+ class Identity
4
+ def do_export(data)
5
+ data
6
+ end
7
+
8
+ def do_import(opts)
9
+ opts
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Traitorous
2
+ module Converter
3
+ class Model
4
+ def initialize(model_klass)
5
+ @model_klass = model_klass
6
+ end
7
+ def do_import(opts)
8
+ @model_klass.new(opts)
9
+ end
10
+ def do_export(data)
11
+ data.export
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Traitorous
2
+ module Converter
3
+ class UniformArray
4
+ def initialize(uniform_class)
5
+ @uniform_class ||= uniform_class
6
+ end
7
+ def do_export(data_arr)
8
+ data_arr.map(&:export)
9
+ end
10
+
11
+ def do_import(opts_arr)
12
+ opts_arr.map{|d| @uniform_class.new(d)}
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'traitorous'
2
+ module Traitorous
3
+ module Equality
4
+ def ==(other_val)
5
+ traits.keys.all? do |trait_name|
6
+ orig_trait = self.send(trait_name.intern)
7
+ other_trait = other_val.send(trait_name.intern)
8
+ array_equality(orig_trait, other_trait) || (orig_trait == other_trait)
9
+ end
10
+ end
11
+
12
+ def array_equality(alpha, beta)
13
+ return false unless (alpha.is_a?(Array) && beta.is_a?(Array))
14
+ alpha.all?{|a| beta.any?{|b| a == b}}
15
+ end
16
+ end
17
+ end
data/lib/traitorous.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+
3
+ module Traitorous
4
+ HASH = HashWithIndifferentAccess
5
+
6
+ VERSION = File.read('VERSION')
7
+
8
+ module Converter
9
+ require 'traitorous/converter/identity'
10
+ require 'traitorous/converter/default_value_static'
11
+ require 'traitorous/converter/model'
12
+ require 'traitorous/converter/uniform_array'
13
+ DEFAULT_CONVERTER = Converter::Identity.new
14
+ end
15
+ require 'traitorous/equality'
16
+ module ClassMethods
17
+ attr_accessor :traits
18
+
19
+ def trait(attr_name, converter = Traitorous::Converter::DEFAULT_CONVERTER)
20
+ self.traits ||= HASH.new
21
+ # sub hash used here, {} chosen for expansion possibility, but only
22
+ # :converter is in use
23
+ self.traits[attr_name] = {converter: converter}
24
+ attr_accessor attr_name
25
+ end
26
+ end
27
+
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
31
+
32
+ def initialize(opts = {})
33
+ @opts = HASH.new(opts)
34
+ traits.each_pair do |new_trait, trait_opts|
35
+ converter = trait_opts[:converter]
36
+ val = converter.do_import(@opts.fetch(new_trait, nil))
37
+ self.send("#{new_trait}=".intern, val)
38
+ end
39
+ end
40
+
41
+ def export
42
+ exported = HASH.new
43
+ traits.each_pair do |trait_name, trait_opts|
44
+ attr_value = self.public_send(trait_name.intern)
45
+ exported[trait_name] = trait_opts[:converter].do_export(attr_value)
46
+ end
47
+ exported
48
+ end
49
+
50
+ def traits
51
+ self.class.traits
52
+ end
53
+
54
+ def inspect
55
+ string = "#<#{self.class.name}:#{self.object_id} "
56
+ fields = self.class.traits.keys.map{|field| "#{field}: #{self.send(field).inspect}"}
57
+ string << fields.join(", ") << ">"
58
+ end
59
+
60
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'traitorous'
7
+ spec.version = File.read('VERSION')
8
+ spec.authors = ['scott m parrish']
9
+ spec.email = ['anithri@gmail.com']
10
+
11
+ spec.summary = %q{A simple attribute, import, export system for PORO, }
12
+ spec.description = %q{This is a simple trait based system that emphasizes the ability to import and export from storage formats}
13
+ spec.homepage = 'https://github.com/anithri/traitorous'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activesupport'
30
+ spec.add_development_dependency 'version'
31
+ spec.add_development_dependency 'bundler', '~> 1.9'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'guard-rspec'
34
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traitorous
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - scott m parrish
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: version
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This is a simple trait based system that emphasizes the ability to import
84
+ and export from storage formats
85
+ email:
86
+ - anithri@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - ".travis.yml.orig"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Guardfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - VERSION
102
+ - bin/console
103
+ - bin/setup
104
+ - lib/traitorous.rb
105
+ - lib/traitorous/converter/default_value_static.rb
106
+ - lib/traitorous/converter/identity.rb
107
+ - lib/traitorous/converter/model.rb
108
+ - lib/traitorous/converter/uniform_array.rb
109
+ - lib/traitorous/equality.rb
110
+ - traitorius.gemspec
111
+ homepage: https://github.com/anithri/traitorous
112
+ licenses:
113
+ - MIT
114
+ metadata:
115
+ allowed_push_host: https://rubygems.org
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: A simple attribute, import, export system for PORO,
136
+ test_files: []