Skip to content

Commit

Permalink
Release 0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloh committed Jun 30, 2017
1 parent e5547e5 commit 6250185
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 382 deletions.
78 changes: 33 additions & 45 deletions lib/pathway.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'forwardable'
require 'inflecto'
require 'contextualizer'
require 'pathway/version'
require 'pathway/initializer'
require 'pathway/result'

module Pathway
Expand All @@ -13,10 +13,15 @@ def self.plugin(name)

self.extend plugin::ClassMethods if plugin.const_defined? :ClassMethods
self.include plugin::InstanceMethods if plugin.const_defined? :InstanceMethods
# TODO: Separate DSL per operation hierarchy
DSL.include plugin::DSLMethods if plugin.const_defined? :DSLMethods
plugin.apply(self) if plugin.respond_to?(:apply)
end
end

class DSL
end

class Error < StandardError
attr_reader :type, :message, :details
singleton_class.send :attr_accessor, :default_messages
Expand All @@ -40,25 +45,34 @@ def default_message_for(type)
end
end

module Plugins
module Scope
module ClassMethods
def scope(*attrs)
include Initializer[*attrs]
end
end
class State
extend Forwardable

module InstanceMethods
def initialize(*)
end
def initialize(operation, values = {})
@hash = operation.context.merge(values)
@result_key = operation.result_key
end

def context
@context || {}
end
end
delegate %i([] []= fetch store include?) => :@hash

def update(kargs)
@hash.update(kargs)
self
end

def result
@hash[@result_key]
end

def to_hash
@hash
end

module Flow
alias :to_h :to_hash
end

module Plugins
module Base
module ClassMethods
attr_accessor :result_key

Expand Down Expand Up @@ -102,10 +116,11 @@ def wrap_if_present(value, type: :not_found, message: nil, details: [])
end

def self.apply(klass)
klass.extend Contextualizer
klass.result_key = :value
end

class DSL
module DSLMethods
def initialize(operation, input)
@result = wrap(State.new(operation, input: input))
@operation = operation
Expand Down Expand Up @@ -170,35 +185,8 @@ def _callable(callable)
end
end
end

class State
extend Forwardable

def initialize(operation, values = {})
@hash = operation.context.merge(values)
@result_key = operation.result_key
end

delegate %i([] []= fetch store include?) => :@hash

def update(kargs)
@hash.update(kargs)
self
end

def result
@hash[@result_key]
end

def to_hash
@hash
end

alias :to_h :to_hash
end
end
end

Operation.plugin Plugins::Scope
Operation.plugin Plugins::Flow
Operation.plugin Plugins::Base
end
28 changes: 0 additions & 28 deletions lib/pathway/initializer.rb

This file was deleted.

4 changes: 0 additions & 4 deletions lib/pathway/plugins/sequel_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def transaction(&bl)
end
end

def self.apply(*)
Flow::DSL.include(DSLMethods)
end

module InstanceMethods
module Finder
def self.[](model_class, by: :id)
Expand Down
2 changes: 1 addition & 1 deletion lib/pathway/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pathway
VERSION = '0.0.7'
VERSION = '0.0.8'
end
1 change: 1 addition & 0 deletions pathway.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.4.0"

spec.add_dependency "inflecto"
spec.add_dependency "contextualizer", "~> 0.0.3"

spec.add_development_dependency "dry-validation", "~> 0.10.7"
spec.add_development_dependency "bundler", ">= 1.14.0"
Expand Down
68 changes: 0 additions & 68 deletions spec/initializer_spec.rb

This file was deleted.

18 changes: 3 additions & 15 deletions spec/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SimpleOperation < Operation
plugin :authorization
plugin :responder

scope :user, :repository
context :user, :repository

authorization { user.role == :root }

Expand Down Expand Up @@ -149,7 +149,8 @@ def create_model(params:, profile:,**)
Class.new(Operation) do
plugin :authorization

scope :role
context :role

authorization { role == :admin }
end
end
Expand All @@ -160,19 +161,6 @@ def create_model(params:, profile:,**)
end
end

describe ".scope" do
subject(:operation_class) do
Class.new(Operation) { scope :foo, :bar }
end

it "includes an Scope module defining the scope dependencies" do
operation = operation_class.new(foo: "XXX", bar: "YYY")

expect(operation.foo).to eq("XXX")
expect(operation.bar).to eq("YYY")
end
end

describe ".call" do
let(:ctx) { { user: double("User", role: :root), repository: double("Repo") } }
let(:params) { { name: "Paul Smith", email: "psmith@email.com" } }
Expand Down
Loading

0 comments on commit 6250185

Please sign in to comment.