Sha256: 1fcb43b6cfc35e7ab6ceb52f0a240d7780bc245365d8e05249f0d38664d15e57

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# Proxy makes different kind of data accessible
# If href is present it also alows loading/reloading
class LHS::Proxy
  autoload :Accessors,
    'lhs/concerns/proxy/accessors'
  autoload :Create,
    'lhs/concerns/proxy/create'
  autoload :Problems,
    'lhs/concerns/proxy/problems'
  autoload :Link,
    'lhs/concerns/proxy/link'

  include Accessors
  include Create
  include Link
  include Problems

  # prevent clashing with attributes of underlying data
  attr_accessor :_data, :_loaded
  delegate :_record, to: :_data, allow_nil: true

  def initialize(data)
    self._data = data
    self._loaded = false
  end

  def record
    _data.class
  end

  def load!(options = nil)
    return self if _loaded
    reload!(options)
  end

  def reload!(options = nil)
    options = {} if options.blank?
    data = _data.class.request(
      options.merge(method: :get).merge(reload_options)
    )
    _data.merge_raw!(data.unwrap(:item_key))
    self._loaded = true
    return becomes(_record) if _record
    self
  end

  private

  def as_record
    @as_record ||= becomes(_record)
  end

  def reload_options
    return { url: _data.href } if _data.href
    return { params: { id: as_record.id } } if as_record.id
    {}
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lhs-14.5.0 lib/lhs/proxy.rb
lhs-14.4.0 lib/lhs/proxy.rb
lhs-14.3.4 lib/lhs/proxy.rb
lhs-14.3.3 lib/lhs/proxy.rb
lhs-14.3.2 lib/lhs/proxy.rb