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

Class Attribute Details

.associationsObject

Returns the value of attribute associations.



60
61
62
# File 'lib/object_json_mapper/base.rb', line 60

def associations
  @associations
end

.relationObject

Returns the value of attribute relation.



60
61
62
# File 'lib/object_json_mapper/base.rb', line 60

def relation
  @relation
end

.root_urlObject

Returns the value of attribute root_url.



60
61
62
# File 'lib/object_json_mapper/base.rb', line 60

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)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/object_json_mapper/base.rb', line 90

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



68
69
70
71
72
73
# File 'lib/object_json_mapper/base.rb', line 68

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:



75
76
77
# File 'lib/object_json_mapper/base.rb', line 75

def configure
  yield self
end

.find(id) ⇒ ObjectJSONMapper::Base

Returns current model instance.

Parameters:

  • id (Integer)

Returns:



140
141
142
143
144
145
146
147
148
# File 'lib/object_json_mapper/base.rb', line 140

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:



154
155
156
# File 'lib/object_json_mapper/base.rb', line 154

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

.inherited(base) ⇒ Object



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

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

.nameObject



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

def name
  @name || super
end

.name=(value) ⇒ Object



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

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

.noneObject



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

def none
  NullRelation.new(klass: self)
end

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

Same as ‘new` but for persisted records

Parameters:

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

Returns:



125
126
127
128
129
# File 'lib/object_json_mapper/base.rb', line 125

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

.root(value) ⇒ Object



115
116
117
118
119
120
# File 'lib/object_json_mapper/base.rb', line 115

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)


105
106
107
108
109
110
111
112
113
# File 'lib/object_json_mapper/base.rb', line 105

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:



133
134
135
# File 'lib/object_json_mapper/base.rb', line 133

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

Instance Method Details

#==(other) ⇒ Object



51
52
53
# File 'lib/object_json_mapper/base.rb', line 51

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

#clientObject



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

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