-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
287 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Datacaster | ||
module Runtimes | ||
class Base | ||
def self.call(r, proc, *args) | ||
r.instance_exec(*args, &proc) | ||
end | ||
|
||
def self.send_to_parent(r, m, *args, &block) | ||
parent = r.instance_variable_get(:@parent) | ||
not_found!(m) if parent.nil? | ||
call(parent, -> { public_send(m, *args, &block) }) | ||
end | ||
|
||
def self.not_found!(m) | ||
raise NoMethodError.new("Method #{m.inspect} is not available in current runtime context") | ||
end | ||
|
||
def initialize(parent = nil) | ||
@parent = parent | ||
end | ||
|
||
def method_missing(m, *args, &block) | ||
self.class.send_to_parent(self, m, *args, &block) | ||
end | ||
|
||
def respond_to_missing?(m, include_private = false) | ||
!@parent.nil? && @parent.respond_to?(m, include_private) | ||
end | ||
|
||
def inspect | ||
"#<#{self.class.name} parent: #{@parent.inspect}>" | ||
end | ||
|
||
def to_s | ||
inspect | ||
end | ||
|
||
def Success(v) | ||
Datacaster.ValidResult(v) | ||
end | ||
|
||
def Failure(v) | ||
Datacaster.ErrorResult(v) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'yaml' | ||
|
||
module Datacaster | ||
module SubstituteI18n | ||
@load_path = [] | ||
|
||
def self.exists?(key) | ||
!fetch(key).nil? | ||
end | ||
|
||
def self.fetch(key) | ||
keys = [locale] + key.split('.') | ||
|
||
@translations.each do |hash| | ||
result = hash.dig(*keys) | ||
return result unless result.nil? | ||
end | ||
nil | ||
end | ||
|
||
def self.load_path | ||
@load_path | ||
end | ||
|
||
def self.load_path=(array) | ||
@load_path = array | ||
@translations = array.map { |x| YAML.load_file(x) } | ||
end | ||
|
||
def self.locale | ||
'en' | ||
end | ||
|
||
def self.locale=(*) | ||
raise NotImplementedError.new("Setting locale is not supported, use ruby-i18n instead of datacaster's built-in") | ||
end | ||
|
||
def self.t(key, **args) | ||
string = fetch(key) | ||
return "Translation missing #{key}" unless string | ||
|
||
args.each do |from, to| | ||
string = string.gsub("%{#{from}}", to.to_s) | ||
end | ||
string | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
module Datacaster | ||
class Transformer < Base | ||
def initialize(name, &block) | ||
def initialize(&block) | ||
raise "Expected block" unless block_given? | ||
|
||
@name = name | ||
@transform = block | ||
end | ||
|
||
def cast(object, runtime:) | ||
Datacaster.ValidResult(Runtime.(runtime, @transform, object)) | ||
Datacaster.ValidResult(Runtimes::Base.(runtime, @transform, object)) | ||
end | ||
|
||
def inspect | ||
"#<Datacaster::#{@name}Transformer>" | ||
"#<Datacaster::Transformer>" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
module Datacaster | ||
class Trier < Base | ||
def initialize(name, error, catched_exception, &block) | ||
def initialize(catched_exception, &block) | ||
raise "Expected block" unless block_given? | ||
|
||
@name = name | ||
@error = error | ||
@catched_exception = Array(catched_exception) | ||
@transform = block | ||
end | ||
|
||
def cast(object, runtime:) | ||
begin | ||
Datacaster.ValidResult(Runtime.(runtime, @transform, object)) | ||
Datacaster.ValidResult(Runtimes::Base.(runtime, @transform, object)) | ||
rescue *@catched_exception | ||
Datacaster.ErrorResult([@error]) | ||
Datacaster.ErrorResult(I18nValues::DefaultKeys.new(['.try', 'datacaster.errors.try'], value: object)) | ||
end | ||
end | ||
|
||
def inspect | ||
"#<Datacaster::#{@name}Trier>" | ||
"#<Datacaster::Trier>" | ||
end | ||
end | ||
end |
Oops, something went wrong.