Module: Datacaster::Mixin
- Included in:
- Base
- Defined in:
- lib/datacaster/mixin.rb
Instance Method Summary collapse
- #&(other) ⇒ Object
- #*(other) ⇒ Object
- #call(object) ⇒ Object
- #call_with_runtime(object, runtime) ⇒ Object
- #cast_errors(error_caster) ⇒ Object
- #i18n_key(*keys, **args) ⇒ Object
- #i18n_map_keys(mapping) ⇒ Object
- #i18n_scope(scope, **args) ⇒ Object
- #i18n_vars(vars) ⇒ Object
- #inspect ⇒ Object
- #then(other) ⇒ Object
- #with_context(context = {}) ⇒ Object
- #with_object_context(object) ⇒ Object
- #with_runtime(runtime) ⇒ Object
- #|(other) ⇒ Object
Instance Method Details
#&(other) ⇒ Object
3 4 5 |
# File 'lib/datacaster/mixin.rb', line 3 def &(other) AndNode.new(self, other) end |
#*(other) ⇒ Object
11 12 13 |
# File 'lib/datacaster/mixin.rb', line 11 def *(other) AndWithErrorAggregationNode.new(self, other) end |
#call(object) ⇒ Object
34 35 36 |
# File 'lib/datacaster/mixin.rb', line 34 def call(object) call_with_runtime(object, Runtimes::Base.new) end |
#call_with_runtime(object, runtime) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/datacaster/mixin.rb', line 38 def call_with_runtime(object, runtime) result = cast(object, runtime: runtime) unless result.is_a?(Result) raise RuntimeError.new("Caster should've returned Datacaster::Result, but returned #{result.inspect} instead") end result end |
#cast_errors(error_caster) ⇒ Object
15 16 17 |
# File 'lib/datacaster/mixin.rb', line 15 def cast_errors(error_caster) ContextNodes::ErrorsCaster.new(self, error_caster) end |
#i18n_key(*keys, **args) ⇒ Object
65 66 67 |
# File 'lib/datacaster/mixin.rb', line 65 def i18n_key(*keys, **args) ContextNodes::I18n.new(self, I18nValues::Key.new(keys, args)) end |
#i18n_map_keys(mapping) ⇒ Object
69 70 71 |
# File 'lib/datacaster/mixin.rb', line 69 def i18n_map_keys(mapping) ContextNodes::I18nKeysMapper.new(self, mapping) end |
#i18n_scope(scope, **args) ⇒ Object
73 74 75 |
# File 'lib/datacaster/mixin.rb', line 73 def i18n_scope(scope, **args) ContextNodes::I18n.new(self, I18nValues::Scope.new(scope, args)) end |
#i18n_vars(vars) ⇒ Object
77 78 79 |
# File 'lib/datacaster/mixin.rb', line 77 def i18n_vars(vars) ContextNodes::I18n.new(self, I18nValues::Scope.new(nil, vars)) end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/datacaster/mixin.rb', line 81 def inspect "#<Datacaster::Base>" end |
#then(other) ⇒ Object
19 20 21 |
# File 'lib/datacaster/mixin.rb', line 19 def then(other) ThenNode.new(self, DefinitionDSL.(other)) end |
#with_context(context = {}) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/datacaster/mixin.rb', line 23 def with_context(context = {}) unless context.respond_to?(:[]) raise "with_context expected enumerable as argument, got #{context.inspect} instead" end ContextNodes::UserContext.new(self, context) end |
#with_object_context(object) ⇒ Object
30 31 32 |
# File 'lib/datacaster/mixin.rb', line 30 def with_object_context(object) ContextNodes::ObjectContext.new(self, object) end |
#with_runtime(runtime) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/datacaster/mixin.rb', line 46 def with_runtime(runtime) result = ->(object) do call_with_runtime(object, runtime) end this = self result.singleton_class.define_method(:with_runtime) do |new_runtime| this.with_runtime(new_runtime) end result.singleton_class.define_method(:without_runtime) do |new_runtime| this end result end |