class Ucb::Hcm::DataFetcher

Attributes

current_value[R]

Public Class Methods

new(raw_data) click to toggle source
# File lib/ucb/hcm/data_fetcher.rb, line 34
def initialize(raw_data)
  @original_value = raw_data
  reset_current_value(raw_data)
end

Public Instance Methods

method_missing(method, *args) click to toggle source

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.

# 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
value(default = nil) click to toggle source
# 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

Private Instance Methods

camelize(str) click to toggle source
# File lib/ucb/hcm/data_fetcher.rb, line 62
def camelize(str)
  str.to_s.gsub(/_([a-z])/) { "#{$1.upcase}" }
end
reset_current_value(hash) click to toggle source
# File lib/ucb/hcm/data_fetcher.rb, line 66
def reset_current_value(hash)
  @current_value = Hashie::Mash.new(hash)
end