Class: TypedOperation::Context
- Inherits:
-
Object
- Object
- TypedOperation::Context
- Defined in:
- lib/typed_operation/context.rb
Overview
Context objects are for passing data through pipelines and chains.
This particular implementation wraps a Hash but provides dot access for cleaner syntax. Its underlying store is mutable.
Users can subclass or provide their own context that implements the same interface.
Required interface for custom contexts:
- #[](key) - read a value
- #[]=(key, value) - write a value (if mutable)
- #key?(key) - check if key exists
- #to_h - convert to Hash (for kwargs extraction)
- #merge(other) - combine with another context/hash, returns new context
Dot access is not required. It’s up to you if you prefer to have it in your operation usage.
Instance Method Summary collapse
-
#==(other) ⇒ Object
: (untyped) -> bool.
-
#[](key) ⇒ Object
: (Symbol | String) -> untyped.
-
#[]=(key, value) ⇒ Object
: (Symbol | String, untyped) -> untyped.
-
#each(&block) ⇒ Object
: () -> Enumerator[[Symbol, untyped], self] : () { ([Symbol, untyped]) -> void } -> self.
-
#empty? ⇒ Boolean
: () -> bool.
-
#except(*keys) ⇒ Object
: (*(Symbol | String)) -> Context.
-
#fetch(key, *args, &block) ⇒ Object
: (Symbol | String) -> untyped : [T] (Symbol | String, T) -> (untyped | T) : [T] (Symbol | String) { (Symbol) -> T } -> (untyped | T).
-
#initialize(data = {}) ⇒ Context
constructor
: (?Hash[Symbol | String, untyped] | Context | _ToH) -> void.
-
#inspect ⇒ Object
(also: #to_s)
: () -> String.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
: (Symbol | String) -> bool.
-
#keys ⇒ Object
: () -> Array.
-
#merge(other) ⇒ Object
: (Hash[Symbol | String, untyped] | Context | _ToH) -> Context.
-
#method_missing(name, *args, &block) ⇒ Object
Provide dot access for reading and writing values.
-
#reject(&block) ⇒ Object
: () { (Symbol, untyped) -> boolish } -> Context.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
: (Symbol, ?bool) -> bool.
-
#select(&block) ⇒ Object
: () { (Symbol, untyped) -> boolish } -> Context.
-
#size ⇒ Object
(also: #length)
: () -> Integer.
-
#slice(*keys) ⇒ Object
: (*(Symbol | String)) -> Context.
-
#to_h ⇒ Object
(also: #to_hash)
: () -> Hash[Symbol, untyped].
-
#transform_values(&block) ⇒ Object
: () { (untyped) -> untyped } -> Context.
-
#values ⇒ Object
: () -> Array.
Constructor Details
#initialize(data = {}) ⇒ Context
: (?Hash[Symbol | String, untyped] | Context | _ToH) -> void
25 26 27 |
# File 'lib/typed_operation/context.rb', line 25 def initialize(data = {}) @data = coerce_to_hash(data).transform_keys(&:to_sym) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Provide dot access for reading and writing values. : (Symbol, *untyped) ?{ (*untyped) -> untyped } -> untyped
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/typed_operation/context.rb', line 131 def method_missing(name, *args, &block) name_str = name.to_s if name_str.end_with?("=") key = name_str.chomp("=").to_sym write_value(key, args.first) elsif name_str.end_with?("?") key = name_str.chomp("?").to_sym !!read_value(key) elsif args.empty? && !block read_value(name) else super end end |
Instance Method Details
#==(other) ⇒ Object
: (untyped) -> bool
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/typed_operation/context.rb', line 118 def ==(other) case other when Context @data == other.to_h when Hash @data == other.transform_keys(&:to_sym) else false end end |
#[](key) ⇒ Object
: (Symbol | String) -> untyped
30 31 32 |
# File 'lib/typed_operation/context.rb', line 30 def [](key) read_value(key.to_sym) end |
#[]=(key, value) ⇒ Object
: (Symbol | String, untyped) -> untyped
35 36 37 |
# File 'lib/typed_operation/context.rb', line 35 def []=(key, value) write_value(key.to_sym, value) end |
#each(&block) ⇒ Object
: () -> Enumerator[[Symbol, untyped], self] : () { ([Symbol, untyped]) -> void } -> self
75 76 77 78 79 |
# File 'lib/typed_operation/context.rb', line 75 def each(&block) return enum_for(:each) unless block_given? @data.each(&block) self end |
#empty? ⇒ Boolean
: () -> bool
107 108 109 |
# File 'lib/typed_operation/context.rb', line 107 def empty? @data.empty? end |
#except(*keys) ⇒ Object
: (*(Symbol | String)) -> Context
102 103 104 |
# File 'lib/typed_operation/context.rb', line 102 def except(*keys) self.class.new(@data.except(*keys.map(&:to_sym))) end |
#fetch(key, *args, &block) ⇒ Object
: (Symbol | String) -> untyped : [T] (Symbol | String, T) -> (untyped | T) : [T] (Symbol | String) { (Symbol) -> T } -> (untyped | T)
59 60 61 |
# File 'lib/typed_operation/context.rb', line 59 def fetch(key, *args, &block) @data.fetch(key.to_sym, *args, &block) end |
#inspect ⇒ Object Also known as: to_s
: () -> String
156 157 158 |
# File 'lib/typed_operation/context.rb', line 156 def inspect "#<#{self.class.name} #{@data.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?
: (Symbol | String) -> bool
40 41 42 |
# File 'lib/typed_operation/context.rb', line 40 def key?(key) @data.key?(key.to_sym) end |
#keys ⇒ Object
: () -> Array
64 65 66 |
# File 'lib/typed_operation/context.rb', line 64 def keys @data.keys end |
#merge(other) ⇒ Object
: (Hash[Symbol | String, untyped] | Context | _ToH) -> Context
52 53 54 |
# File 'lib/typed_operation/context.rb', line 52 def merge(other) self.class.new(@data.merge(coerce_to_hash(other))) end |
#reject(&block) ⇒ Object
: () { (Symbol, untyped) -> boolish } -> Context
87 88 89 |
# File 'lib/typed_operation/context.rb', line 87 def reject(&block) self.class.new(@data.reject(&block)) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
: (Symbol, ?bool) -> bool
148 149 150 151 152 153 |
# File 'lib/typed_operation/context.rb', line 148 def respond_to_missing?(name, include_private = false) name_str = name.to_s name_str.end_with?("=", "?") || key?(name) || super end |
#select(&block) ⇒ Object
: () { (Symbol, untyped) -> boolish } -> Context
82 83 84 |
# File 'lib/typed_operation/context.rb', line 82 def select(&block) self.class.new(@data.select(&block)) end |
#size ⇒ Object Also known as: length
: () -> Integer
112 113 114 |
# File 'lib/typed_operation/context.rb', line 112 def size @data.size end |
#slice(*keys) ⇒ Object
: (*(Symbol | String)) -> Context
97 98 99 |
# File 'lib/typed_operation/context.rb', line 97 def slice(*keys) self.class.new(@data.slice(*keys.map(&:to_sym))) end |
#to_h ⇒ Object Also known as: to_hash
: () -> Hash[Symbol, untyped]
46 47 48 |
# File 'lib/typed_operation/context.rb', line 46 def to_h @data.dup end |
#transform_values(&block) ⇒ Object
: () { (untyped) -> untyped } -> Context
92 93 94 |
# File 'lib/typed_operation/context.rb', line 92 def transform_values(&block) self.class.new(@data.transform_values(&block)) end |
#values ⇒ Object
: () -> Array
69 70 71 |
# File 'lib/typed_operation/context.rb', line 69 def values @data.values end |