Class: LHS::Record

Inherits:
Object
  • Object
show all
Includes:
Inspect, IsHref, Pagination, AttributeAssignment, Batch, Chainable, Configuration, Create, CustomSetters, Destroy, Endpoints, Equality, Find, FindBy, First, HrefFor, Last, Mapping, Merge, Model, Provider, Relations, Request, Scope, Tracing, Update
Defined in:
lib/lhs/record.rb,
lib/lhs/concerns/record/find.rb,
lib/lhs/concerns/record/last.rb,
lib/lhs/concerns/record/batch.rb,
lib/lhs/concerns/record/first.rb,
lib/lhs/concerns/record/merge.rb,
lib/lhs/concerns/record/model.rb,
lib/lhs/concerns/record/scope.rb,
lib/lhs/concerns/record/create.rb,
lib/lhs/concerns/record/update.rb,
lib/lhs/test/stubbable_records.rb,
lib/lhs/concerns/record/destroy.rb,
lib/lhs/concerns/record/find_by.rb,
lib/lhs/concerns/record/mapping.rb,
lib/lhs/concerns/record/request.rb,
lib/lhs/concerns/record/tracing.rb,
lib/lhs/concerns/record/equality.rb,
lib/lhs/concerns/record/href_for.rb,
lib/lhs/concerns/record/provider.rb,
lib/lhs/concerns/record/chainable.rb,
lib/lhs/concerns/record/endpoints.rb,
lib/lhs/concerns/record/relations.rb,
lib/lhs/concerns/record/pagination.rb,
lib/lhs/concerns/record/configuration.rb,
lib/lhs/concerns/record/custom_setters.rb,
lib/lhs/concerns/record/attribute_assignment.rb

Defined Under Namespace

Modules: AttributeAssignment, Batch, Chainable, Configuration, Create, CustomSetters, Destroy, Endpoints, Equality, Find, FindBy, First, HrefFor, Last, Mapping, Merge, Model, Pagination, Provider, Relations, Request, Scope, Tracing, Update

Constant Summary collapse

DEFAULT_LIMIT =
LHS::Pagination::Base::DEFAULT_LIMIT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#persisted?, #to_model

Methods included from Merge

#deep_merge, #deep_merge!, #merge, #merge!

Methods included from Inspect

#inspect

Methods included from Equality

#==

Methods included from Chainable

#options

Methods included from AttributeAssignment

#assign_attributes

Constructor Details

#initialize(data = nil, apply_customer_setters = true) ⇒ Record

Returns a new instance of Record.



82
83
84
85
86
87
# File 'lib/lhs/record.rb', line 82

def initialize(data = nil, apply_customer_setters = true)
  data ||= LHS::Data.new({}, nil, self.class)
  data = LHS::Data.new(data, nil, self.class) unless data.is_a?(LHS::Data)
  define_singleton_method(:_data) { data }
  apply_custom_setters! if apply_customer_setters
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



105
106
107
# File 'lib/lhs/record.rb', line 105

def method_missing(name, *args, &block)
  _data.send(name, *args, &block)
end

Class Method Details

.build(data = nil) ⇒ Object



93
94
95
# File 'lib/lhs/record.rb', line 93

def self.build(data = nil)
  new(data)
end

.stub_all(url, items, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lhs/test/stubbable_records.rb', line 9

def self.stub_all(url, items, options = {})
  extend WebMock::API

  items.each_slice(DEFAULT_LIMIT).with_index do |(*batch), index|
    uri = LocalUri::URI.new(url)
    uri.query.merge!(
      limit_key(:parameter) => DEFAULT_LIMIT
    )
    offset = pagination_class.page_to_offset(index + 1, DEFAULT_LIMIT)
    unless index.zero?
      uri.query.merge!(
        pagination_key(:parameter) => offset
      )
    end
    request_stub = stub_request(:get, uri.to_s)
    request_stub.with(options) if options.present?
    request_stub.to_return(
      body: {
        items: batch,
        offset: index.zero? ? 0 : offset,
        total: items.length
      }.to_json
    )
  end
end

Instance Method Details

#as_json(options = nil) ⇒ Object



89
90
91
# File 'lib/lhs/record.rb', line 89

def as_json(options = nil)
  _data.as_json(options)
end

#dupObject

Override Object#dup because it doesn’t support copying any singleton methods, which leads to missing ‘_data` method when you execute `dup`.



99
100
101
# File 'lib/lhs/record.rb', line 99

def dup
  clone
end