Sha256: 57832b3e8d8ccd7494bef1334f8fe62fe813775bdbfd629f6c2322b7df5f5e9e

Contents?: true

Size: 1.21 KB

Versions: 41

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, :becomes, 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

41 entries across 41 versions & 1 rubygems

Version Path
lhs-16.1.5 lib/lhs/proxy.rb
lhs-16.1.4 lib/lhs/proxy.rb
lhs-16.1.3 lib/lhs/proxy.rb
lhs-16.1.2 lib/lhs/proxy.rb
lhs-16.1.1 lib/lhs/proxy.rb
lhs-16.1.0 lib/lhs/proxy.rb
lhs-16.0.1 lib/lhs/proxy.rb
lhs-16.0.0 lib/lhs/proxy.rb
lhs-15.7.0 lib/lhs/proxy.rb
lhs-15.6.1 lib/lhs/proxy.rb
lhs-15.6.0 lib/lhs/proxy.rb
lhs-15.5.1 lib/lhs/proxy.rb
lhs-15.5.0 lib/lhs/proxy.rb
lhs-15.4.1 lib/lhs/proxy.rb
lhs-15.4.0 lib/lhs/proxy.rb
lhs-15.4.0.pre.hasone.1 lib/lhs/proxy.rb
lhs-15.3.3 lib/lhs/proxy.rb
lhs-15.3.3.pre.fixoptions.1 lib/lhs/proxy.rb
lhs-15.3.2 lib/lhs/proxy.rb
lhs-15.3.1 lib/lhs/proxy.rb