Class: ObjectJSONMapper::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Validations, ActiveModel::Validations::Callbacks, Associations, Conversion, Errors, Local, Persistence, Serialization
Defined in:
lib/object_json_mapper/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#destroy, included, #reload, #save, #update

Methods included from Associations

included

Methods included from Errors

#load_errors, #valid?

Methods included from Serialization

#serializable_hash

Methods included from Conversion

#to_key, #to_model, #to_param, #to_partial_path

Methods included from Local

#find_by_local, included, #local

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



21
22
23
24
# File 'lib/object_json_mapper/base.rb', line 21

def initialize(attributes = {})
  self.attributes = attributes
  @persisted = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



40
41
42
43
# File 'lib/object_json_mapper/base.rb', line 40

def method_missing(method_name, *args, &block)
  return attributes.fetch(method_name) if respond_to_missing?(method_name)
  super
end

Class Attribute Details

.associationsObject

Returns the value of attribute associations.



63
64
65
# File 'lib/object_json_mapper/base.rb', line 63

def associations
  @associations
end

.relationObject

Returns the value of attribute relation.



63
64
65
# File 'lib/object_json_mapper/base.rb', line 63

def relation
  @relation
end

.root_urlObject

Returns the value of attribute root_url.



63
64
65
# File 'lib/object_json_mapper/base.rb', line 63

def root_url
  @root_url
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



15
16
17
# File 'lib/object_json_mapper/base.rb', line 15

def attributes
  @attributes
end

#persistedObject Also known as: persisted?

Returns the value of attribute persisted.



15
16
17
# File 'lib/object_json_mapper/base.rb', line 15

def persisted
  @persisted
end

Class Method Details

.attribute(name, type: nil, default: nil) ⇒ Object

Parameters:

  • name (Symbol)
  • type (Dry::Types::Constructor) (defaults to: nil)
  • default (Proc) (defaults to: nil)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/object_json_mapper/base.rb', line 93

def attribute(name, type: nil, default: nil)
  define_method(name) do
    return default.call if attributes.exclude?(name) && default
    return type.call(attributes[name]) if type

    attributes[name]
  end

  define_method("#{name}=") do |value|
    attributes[name] = value
  end
end

.clientObject



71
72
73
74
75
76
# File 'lib/object_json_mapper/base.rb', line 71

def client
  RestClient::Resource.new(
    URI.join(ObjectJSONMapper.base_url, root_url).to_s,
    headers: ObjectJSONMapper.headers
  )
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



78
79
80
# File 'lib/object_json_mapper/base.rb', line 78

def configure
  yield self
end

.find(id) ⇒ ObjectJSONMapper::Base

Returns current model instance.

Parameters:

  • id (Integer)

Returns:



143
144
145
146
147
148
149
150
151
# File 'lib/object_json_mapper/base.rb', line 143

def find(id)
  raise ActiveRecord::RecordNotFound if id.nil?

  result = HTTP.parse_json(client[id].get.body)

  persist(result)
rescue RestClient::ExceptionWithResponse
  raise ActiveRecord::RecordNotFound
end

.find_by(conditions = {}) ⇒ ObjectJSONMapper::Base

rubocop:disable Rails/FindBy

Parameters:

  • conditions (Hash) (defaults to: {})

Returns:



157
158
159
# File 'lib/object_json_mapper/base.rb', line 157

def find_by(conditions = {})
  where(conditions).first
end

.inherited(base) ⇒ Object



65
66
67
68
69
# File 'lib/object_json_mapper/base.rb', line 65

def inherited(base)
  base.root_url     = base.name.underscore.pluralize
  base.associations = Associations::Registry.new
  base.relation     = Relation.new(klass: base)
end

.nameObject



86
87
88
# File 'lib/object_json_mapper/base.rb', line 86

def name
  @name || super
end

.name=(value) ⇒ Object



82
83
84
# File 'lib/object_json_mapper/base.rb', line 82

def name=(value)
  @name = value.to_s
end

.noneObject



161
162
163
# File 'lib/object_json_mapper/base.rb', line 161

def none
  NullRelation.new(klass: self)
end

.persist(attributes = {}) ⇒ ObjectJSONMapper::Base

Same as ‘new` but for persisted records

Parameters:

  • attributes (Hash) (defaults to: {})

Returns:



128
129
130
131
132
# File 'lib/object_json_mapper/base.rb', line 128

def persist(attributes = {})
  new(attributes).tap do |base|
    base.persisted = true
  end
end

.root(value) ⇒ Object



118
119
120
121
122
123
# File 'lib/object_json_mapper/base.rb', line 118

def root(value)
  clone.tap do |base|
    base.name     = name
    base.root_url = value.to_s
  end
end

.scope(name, block) ⇒ Object

Parameters:

  • name (Symbol)
  • block (Proc)


108
109
110
111
112
113
114
115
116
# File 'lib/object_json_mapper/base.rb', line 108

def scope(name, block)
  define_singleton_method(name) do
    relation.deep_clone.instance_exec(&block)
  end

  relation.define_singleton_method(name) do
    instance_exec(&block)
  end
end

.where(conditions = {}) ⇒ ObjectJSONMapper::Relation<ObjectJSONMapper::Base> Also known as: all

Returns collection of model instances.

Parameters:

  • conditions (Hash) (defaults to: {})

Returns:



136
137
138
# File 'lib/object_json_mapper/base.rb', line 136

def where(conditions = {})
  relation.tap { |relation| relation.klass = self }.where(conditions)
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
# File 'lib/object_json_mapper/base.rb', line 54

def ==(other)
  attributes == other.attributes && persisted == other.persisted
end

#clientObject



58
59
60
# File 'lib/object_json_mapper/base.rb', line 58

def client
  self.class.client[id]
end

#new_record?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/object_json_mapper/base.rb', line 28

def new_record?
  !persisted?
end

#persistObject



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

def persist
  @persisted = true
end

#reloadable?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/object_json_mapper/base.rb', line 36

def reloadable?
  to_key.any?
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/object_json_mapper/base.rb', line 45

def respond_to_missing?(method_name, *)
  attributes.key?(method_name)
end