Class: Roast::Workflow::DotAccessHash

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/workflow/dot_access_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ DotAccessHash

Returns a new instance of DotAccessHash.



6
7
8
# File 'lib/roast/workflow/dot_access_hash.rb', line 6

def initialize(hash)
  @hash = hash || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/roast/workflow/dot_access_hash.rb', line 19

def method_missing(method_name, *args, &block)
  method_str = method_name.to_s

  # Handle boolean predicate methods (ending with ?)
  if method_str.end_with?("?")
    key = method_str.chomp("?")
    # Always return false for non-existent keys with ? methods
    return false unless has_key?(key) # rubocop:disable Style/PreferredHashMethods

    !!self[key]
  # Handle setter methods (ending with =)
  elsif method_str.end_with?("=")
    key = method_str.chomp("=")
    self[key] = args.first
  # Handle bang methods (ending with !) - should raise
  elsif method_str.end_with?("!")
    super
  # Handle regular getter methods
  elsif args.empty? && block.nil?
    # Return nil for non-existent keys (like a hash would)
    self[method_str]
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/roast/workflow/dot_access_hash.rb', line 180

def ==(other)
  case other
  when DotAccessHash
    @hash == other.instance_variable_get(:@hash)
  when Hash
    @hash == other
  else
    false
  end
end

#[](key) ⇒ Object



10
11
12
13
# File 'lib/roast/workflow/dot_access_hash.rb', line 10

def [](key)
  value = @hash[key.to_sym] || @hash[key.to_s]
  wrap_value(value)
end

#[]=(key, value) ⇒ Object



15
16
17
# File 'lib/roast/workflow/dot_access_hash.rb', line 15

def []=(key, value)
  @hash[key.to_sym] = value
end

#clearObject



175
176
177
178
# File 'lib/roast/workflow/dot_access_hash.rb', line 175

def clear
  @hash.clear
  self
end

#compactObject



148
149
150
# File 'lib/roast/workflow/dot_access_hash.rb', line 148

def compact
  DotAccessHash.new(@hash.compact)
end

#delete(key) ⇒ Object



171
172
173
# File 'lib/roast/workflow/dot_access_hash.rb', line 171

def delete(key)
  @hash.delete(key.to_sym) || @hash.delete(key.to_s)
end

#dig(*keys) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/roast/workflow/dot_access_hash.rb', line 118

def dig(*keys)
  keys.inject(self) do |obj, key|
    break nil unless obj.is_a?(DotAccessHash) || obj.is_a?(Hash)

    if obj.is_a?(DotAccessHash)
      obj[key]
    else
      obj[key.to_sym] || obj[key.to_s]
    end
  end
end

#each(&block) ⇒ Object



71
72
73
# File 'lib/roast/workflow/dot_access_hash.rb', line 71

def each(&block)
  @hash.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/roast/workflow/dot_access_hash.rb', line 67

def empty?
  @hash.empty?
end

#except(*keys) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/roast/workflow/dot_access_hash.rb', line 162

def except(*keys)
  excluded = @hash.dup
  keys.each do |key|
    excluded.delete(key.to_sym)
    excluded.delete(key.to_s)
  end
  DotAccessHash.new(excluded)
end

#fetch(key, *args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roast/workflow/dot_access_hash.rb', line 106

def fetch(key, *args)
  if has_key?(key) # rubocop:disable Style/PreferredHashMethods
    self[key]
  elsif block_given?
    yield(key)
  elsif !args.empty?
    args[0]
  else
    raise KeyError, "key not found: #{key.inspect}"
  end
end

#has_key?(key_name) ⇒ Boolean Also known as: member?

Returns:

  • (Boolean)


191
192
193
# File 'lib/roast/workflow/dot_access_hash.rb', line 191

def has_key?(key_name)
  @hash.key?(key_name.to_sym) || @hash.key?(key_name.to_s)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/roast/workflow/dot_access_hash.rb', line 102

def include?(key)
  has_key?(key) # rubocop:disable Style/PreferredHashMethods
end

#inspectObject



79
80
81
# File 'lib/roast/workflow/dot_access_hash.rb', line 79

def inspect
  @hash.inspect
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/roast/workflow/dot_access_hash.rb', line 98

def key?(key)
  has_key?(key) # rubocop:disable Style/PreferredHashMethods
end

#keysObject



63
64
65
# File 'lib/roast/workflow/dot_access_hash.rb', line 63

def keys
  @hash.keys
end

#map(&block) ⇒ Object



136
137
138
# File 'lib/roast/workflow/dot_access_hash.rb', line 136

def map(&block)
  @hash.map(&block)
end

#merge(other) ⇒ Object



87
88
89
90
91
92
# File 'lib/roast/workflow/dot_access_hash.rb', line 87

def merge(other)
  merged_hash = @hash.dup
  other_hash = other.is_a?(DotAccessHash) ? other.to_h : other
  merged_hash.merge!(other_hash)
  DotAccessHash.new(merged_hash)
end

#reject(&block) ⇒ Object



144
145
146
# File 'lib/roast/workflow/dot_access_hash.rb', line 144

def reject(&block)
  DotAccessHash.new(@hash.reject(&block))
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/roast/workflow/dot_access_hash.rb', line 45

def respond_to_missing?(method_name, include_private = false)
  method_str = method_name.to_s

  if method_str.end_with?("!")
    false # Don't respond to bang methods
  elsif method_str.end_with?("?")
    true  # Always respond to predicate methods
  elsif method_str.end_with?("=")
    true  # Always respond to setter methods
  else
    true  # Always respond to getter methods (they return nil if missing)
  end
end

#select(&block) ⇒ Object



140
141
142
# File 'lib/roast/workflow/dot_access_hash.rb', line 140

def select(&block)
  DotAccessHash.new(@hash.select(&block))
end

#sizeObject Also known as: length



130
131
132
# File 'lib/roast/workflow/dot_access_hash.rb', line 130

def size
  @hash.size
end

#slice(*keys) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/roast/workflow/dot_access_hash.rb', line 152

def slice(*keys)
  sliced = {}
  keys.each do |key|
    if has_key?(key) # rubocop:disable Style/PreferredHashMethods
      sliced[key.to_sym] = @hash[key.to_sym] || @hash[key.to_s]
    end
  end
  DotAccessHash.new(sliced)
end

#to_hObject



59
60
61
# File 'lib/roast/workflow/dot_access_hash.rb', line 59

def to_h
  @hash
end

#to_json(*args) ⇒ Object



83
84
85
# File 'lib/roast/workflow/dot_access_hash.rb', line 83

def to_json(*args)
  @hash.to_json(*args)
end

#to_sObject



75
76
77
# File 'lib/roast/workflow/dot_access_hash.rb', line 75

def to_s
  @hash.to_s
end

#valuesObject



94
95
96
# File 'lib/roast/workflow/dot_access_hash.rb', line 94

def values
  @hash.values
end