faire 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2bb2a13cbd53ef37d79d7eb3b446af1f9891e8be
4
+ data.tar.gz: 8da189e4f4c2db7f23a9e6e0f8dc538e31db2b5d
5
+ SHA512:
6
+ metadata.gz: 84d58898a6fa8eec92fa28826277a25f178df523608b61619a958f4a7ace7c05ec5f3d48ece582e697c0078c59b60eba07c66e5d3993d3c558d6ea958236e07e
7
+ data.tar.gz: 7009651f627620ade4429679e6b9d5d819983d1f6b7120655d26784689987fa0563a293e58554e1d5cef5377b53eec97703a3d0abbc0b7b808a6712806904a4a
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faire.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Michal Krzyzanowski
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.
@@ -0,0 +1,29 @@
1
+ # Faire
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'faire'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install faire
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << "lib" << "test"
6
+ test.pattern = "test/**/*_test.rb"
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -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 'faire/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "faire"
8
+ spec.version = Faire::VERSION
9
+ spec.authors = ["Michal Krzyzanowski"]
10
+ spec.email = ["michal.krzyzanowski+github@gmail.com"]
11
+ spec.description = %q{Manage application logic with Interactors}
12
+ spec.summary = %q{}
13
+ spec.homepage = "https://github.com/chaps-io/faire"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "virtus", "~> 1.0.5"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "minitest", "~> 5.8.1"
25
+ end
@@ -0,0 +1,18 @@
1
+ require "virtus"
2
+ require "faire/version"
3
+ require "faire/input"
4
+ require "faire/interactor"
5
+ require "faire/runner"
6
+ require "faire/attributes"
7
+ require "faire/error"
8
+ require "faire/validator"
9
+
10
+ module Faire
11
+ def self.included(base)
12
+ base.include(Virtus.model)
13
+ base.include(Validator)
14
+
15
+ base.extend(Attributes)
16
+ base.extend(Runner)
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ module Faire
2
+ module Attributes
3
+ def required_inputs
4
+ inputs.select(&:required?)
5
+ end
6
+
7
+ def inputs
8
+ @inputs ||= []
9
+ end
10
+
11
+ def defaults
12
+ { nullify_blank: true, required: true }.freeze
13
+ end
14
+
15
+ def string(name, attributes = {})
16
+ input(name, String, attributes)
17
+ end
18
+
19
+ def object(name, klass, attributes = {})
20
+ klass.include(Virtus.model)
21
+
22
+ input(name, klass, attributes)
23
+ end
24
+
25
+ def input(name, klass, attributes)
26
+ attrs = defaults.merge(attributes)
27
+
28
+ self.inputs << Input.new(name, String, attrs)
29
+ self.attribute(name, klass, attrs)
30
+ end
31
+
32
+ def integer(name, attributes = {})
33
+ input(name, Integer, attributes)
34
+ end
35
+
36
+ def float(name, attributes = {})
37
+ input(name, Float, attributes)
38
+ end
39
+
40
+ def date(name, attributes = {})
41
+ input(name, Date, attributes)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ module Faire
2
+ module Error
3
+ RequiredParametersMissing = Class.new(StandardError)
4
+ CoercionError = Class.new(StandardError)
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module Faire
2
+ class Input
3
+ attr_reader :name
4
+
5
+ def initialize(name, klass, **attributes)
6
+ @name = name
7
+ @attributes = attributes
8
+ @required = attributes.fetch(:required, false)
9
+ end
10
+
11
+ def required?
12
+ @required == true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ module Faire
2
+ class Interactor
3
+ extend Forwardable
4
+ attr_reader :object
5
+
6
+ def_delegators :object, :validate, :execute, :valid?
7
+
8
+ def initialize(object)
9
+ @object = object
10
+ end
11
+
12
+ def validate_and_execute
13
+ validate
14
+ result
15
+ end
16
+
17
+ def result
18
+ @result ||= execute if valid?
19
+ end
20
+
21
+ def errors
22
+ @errors ||= object.errors
23
+ end
24
+
25
+ def success?
26
+ errors.empty?
27
+ end
28
+
29
+ def fail?
30
+ errors.any?
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ module Faire
2
+ module Runner
3
+ def run(input = {})
4
+ object = new(input)
5
+ Interactor.new(object).tap(&:validate_and_execute)
6
+ end
7
+
8
+ def run!(input = {})
9
+ interactor = new(input)
10
+ interactor.validate!
11
+ interactor.execute
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module Faire
2
+ module Validator
3
+ def validate!
4
+ validate || (raise Error::RequiredParametersMissing, "required parameters missing")
5
+ end
6
+
7
+ def validate
8
+ @valid = self.class.required_inputs.all?{|object| self.public_send(object.name) }
9
+ end
10
+
11
+ def valid?
12
+ @valid == true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Faire
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ require "test_helper"
2
+
3
+ class FaireTest < MiniTest::Test
4
+ def test_run_returns_output_if_input_is_correct
5
+ interactor = SampleInteraction.run!(name: "asd", bill: { name: "ZOMG" }, other: 123)
6
+ assert_equal Bill, interactor.class
7
+ end
8
+
9
+ def test_run_raises_error_if_input_is_incorrect
10
+ assert_raises Faire::Error::RequiredParametersMissing do
11
+ SampleInteraction.run!(bill: { name: "ZOMG" }, other: 123)
12
+ end
13
+ end
14
+
15
+ def test_run_returns_interactor_class_if_output_is_correct
16
+ interactor = SampleInteraction.run(name: "asd", bill: { name: "ZOMG" }, other: 123)
17
+ assert_equal Faire::Interactor, interactor.class
18
+ assert_equal "ZOMG", interactor.result.name
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ class Bill
2
+ include Virtus.model
3
+
4
+ attribute :name
5
+ end
6
+
7
+ class SampleInteraction
8
+ include Faire
9
+
10
+ integer :name, nullify_blank: true
11
+ string :other, required: false
12
+ object :bill, Bill
13
+
14
+ def execute
15
+ bill
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ require "faire"
2
+ require "minitest/autorun"
3
+ require_relative "support/sample_interaction"
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faire
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michal Krzyzanowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.8.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.8.1
69
+ description: Manage application logic with Interactors
70
+ email:
71
+ - michal.krzyzanowski+github@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - faire.gemspec
82
+ - lib/faire.rb
83
+ - lib/faire/attributes.rb
84
+ - lib/faire/error.rb
85
+ - lib/faire/input.rb
86
+ - lib/faire/interactor.rb
87
+ - lib/faire/runner.rb
88
+ - lib/faire/validator.rb
89
+ - lib/faire/version.rb
90
+ - test/faire_test.rb
91
+ - test/support/sample_interaction.rb
92
+ - test/test_helper.rb
93
+ homepage: https://github.com/chaps-io/faire
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.8
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: ''
117
+ test_files:
118
+ - test/faire_test.rb
119
+ - test/support/sample_interaction.rb
120
+ - test/test_helper.rb