class Faraday::Hypermedia::History

Constants

State

Public Class Methods

new() click to toggle source
# File lib/faraday/hypermedia/history.rb, line 9
def initialize
  @stored_states = [build_state]
  @current_index = 0
  reset_current_links
end

Public Instance Methods

back() click to toggle source
# File lib/faraday/hypermedia/history.rb, line 87
def back
  if @current_index >= 1
    @current_index -= 1
    reset_current_links
  end
end
current_state() click to toggle source
# File lib/faraday/hypermedia/history.rb, line 15
def current_state
  @stored_states[@current_index]
end
fill_in_template_params(template_params) click to toggle source
# File lib/faraday/hypermedia/history.rb, line 51
def fill_in_template_params(template_params)
  template_urls = current_links.keys.select { |k| k =~ RE_URI_TEMPLATE }
  template_urls.each do |template_url|
    expanded_url = URITemplate.new(template_url).expand(template_params) # Addressable のほうが良いかも
    link_params = current_links.delete(template_url)
    current_links.store(expanded_url, link_params)
  end
end
forward() click to toggle source
# File lib/faraday/hypermedia/history.rb, line 94
def forward
  if @current_index < @stored_states.length - 1
    @current_index += 1
    reset_current_links
  end
end
push(response_env) click to toggle source
# File lib/faraday/hypermedia/history.rb, line 82
def push(response_env)
  url = response_env[:url]
  push_state(response_env, '', url)
end
push_state(data, title, url) click to toggle source
# File lib/faraday/hypermedia/history.rb, line 64
def push_state(data, title, url)
  if url == current_state.url
    replace_state(data, title, url)
  else
    @stored_states.slice!((@current_index+1)..-1) # current_index の先から最後まで削除
    state = build_state(data: data, title: title, url: url)
    @stored_states.push(state)
    @current_index += 1
    reset_current_links
    state
  end
end
replace_state(data, title, url) click to toggle source
# File lib/faraday/hypermedia/history.rb, line 77
def replace_state(data, title, url)
  reset_current_links
  @stored_states[@current_index] = build_state(data: data, title: title, url: url)
end

Private Instance Methods

build_state(source = {}) click to toggle source
# File lib/faraday/hypermedia/history.rb, line 103
def build_state(source = {})
  State.new(
    source[:data]  || {},
    source[:title] || '',
    source[:url]   || URI('navigation:blank')
  )
end