Class: Jsapi::Model::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Validations
Defined in:
lib/jsapi/model/base.rb

Overview

The base API model.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nested) ⇒ Base

Returns a new instance of Base.



32
33
34
# File 'lib/jsapi/model/base.rb', line 32

def initialize(nested)
  @nested = nested
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)

:nodoc:



67
68
69
70
# File 'lib/jsapi/model/base.rb', line 67

def method_missing(*args) # :nodoc:
  name = args.first.to_s
  _attr_readers.key?(name) ? _attr_readers[name] : super
end

Class Method Details

.model_nameObject

Overrides ActiveModel::Naming#model_name to support anonymous model classes.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jsapi/model/base.rb', line 11

def self.model_name
  @_model_name ||= begin
    # Prevent that ActiveModel::Name::new raises an error if this is an anonymous class
    klass = self
    klass = klass.superclass while klass.name.nil?

    # Adapted from ActiveModel::Naming#model_name
    namespace = klass.module_parents.detect do |n|
      n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
    end
    ActiveModel::Name.new(klass, namespace)
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



36
37
38
39
40
41
# File 'lib/jsapi/model/base.rb', line 36

def ==(other) # :nodoc:
  super || (
    self.class == other.class &&
    attributes == other.attributes
  )
end

#errorsObject

Overrides ActiveModel::Validations#errors to use Errors as error store.



44
45
46
# File 'lib/jsapi/model/base.rb', line 44

def errors
  @errors ||= Errors.new(self)
end

#inspectObject

:nodoc:



48
49
50
51
# File 'lib/jsapi/model/base.rb', line 48

def inspect # :nodoc:
  "#<#{self.class.name}#{' ' if attributes.any?}" \
  "#{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
end

#respond_to_missing?(param1, _param2) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


53
54
55
# File 'lib/jsapi/model/base.rb', line 53

def respond_to_missing?(param1, _param2) # :nodoc:
  _attr_readers.key?(param1.to_s) ? true : super
end