Class: Buezli::Api::Client::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/buezli/api/client/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes:, model_name:, schema:, api_client: nil, client: nil) ⇒ Record

Returns a new instance of Record.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/buezli/api/client/record.rb', line 7

def initialize(attributes:, model_name:, schema:, api_client: nil, client: nil)
  @raw_attributes = (attributes || {}).with_indifferent_access
  @model_name = model_name
  @api_client = api_client || client
  @schema = schema
  raise ArgumentError, "api_client is required" unless @api_client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



31
32
33
34
35
# File 'lib/buezli/api/client/record.rb', line 31

def method_missing(method, *args, &block)
  return super unless args.empty? && block.nil? && schema.declared_field?(model_name, method)

  read_value(method.to_s)
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



5
6
7
# File 'lib/buezli/api/client/record.rb', line 5

def api_client
  @api_client
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



5
6
7
# File 'lib/buezli/api/client/record.rb', line 5

def model_name
  @model_name
end

#raw_attributesObject (readonly)

Returns the value of attribute raw_attributes.



5
6
7
# File 'lib/buezli/api/client/record.rb', line 5

def raw_attributes
  @raw_attributes
end

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/buezli/api/client/record.rb', line 5

def schema
  @schema
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
# File 'lib/buezli/api/client/record.rb', line 23

def [](key)
  raw_attributes[key]
end

#deleted?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/buezli/api/client/record.rb', line 19

def deleted?
  raw_attributes[:_deleted] == true
end

#idObject



15
16
17
# File 'lib/buezli/api/client/record.rb', line 15

def id
  raw_attributes[:id]
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/buezli/api/client/record.rb', line 37

def respond_to_missing?(method, include_private = false)
  schema.declared_field?(model_name, method) || super
end

#to_hObject



27
28
29
# File 'lib/buezli/api/client/record.rb', line 27

def to_h
  raw_attributes.to_h
end