Class: Ucb::Hcm::DataFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ucb/hcm/data_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data) ⇒ DataFetcher

Returns a new instance of DataFetcher.



34
35
36
37
# File 'lib/ucb/hcm/data_fetcher.rb', line 34

def initialize(raw_data)
  @original_value = raw_data
  reset_current_value(raw_data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

This is where the fetching logic happens - each time we’re given a value to find, we store that value as @current_value and wait for the next call. If we ever get nil, then none of the subsequent calls matter, so we short-circuit them.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ucb/hcm/data_fetcher.rb', line 48

def method_missing(method, *args)
  return self if @current_value.nil?

  normalized_method = camelize(method)
  if @current_value.respond_to?(normalized_method)
    @current_value = @current_value.send(normalized_method, *args)
  else
    @current_value = nil
  end
  self
end

Instance Attribute Details

#current_valueObject (readonly)

Returns the value of attribute current_value.



32
33
34
# File 'lib/ucb/hcm/data_fetcher.rb', line 32

def current_value
  @current_value
end

Instance Method Details

#value(default = nil) ⇒ Object



39
40
41
42
43
# File 'lib/ucb/hcm/data_fetcher.rb', line 39

def value(default = nil)
  (@current_value || default).tap do
    reset_current_value(@original_value)
  end
end