Class: RestModel

Inherits:
Object
  • Object
show all
Extended by:
Key::Builder, Source::Retriever
Includes:
Response, Serialization, Source::Sender
Defined in:
lib/rest_model.rb,
lib/rest_model/key.rb,
lib/rest_model/errors.rb,
lib/rest_model/version.rb,
lib/rest_model/key/href.rb,
lib/rest_model/response.rb,
lib/rest_model/key/builder.rb,
lib/rest_model/source/path.rb,
lib/rest_model/key/property.rb,
lib/rest_model/key/relation.rb,
lib/rest_model/configuration.rb,
lib/rest_model/source/sender.rb,
lib/rest_model/key/embeddable.rb,
lib/rest_model/key/association.rb,
lib/rest_model/source/retriever.rb,
lib/rest_model/key/href/response.rb,
lib/rest_model/serialization/date.rb,
lib/rest_model/source/translation.rb,
lib/rest_model/key/property/sender.rb,
lib/rest_model/serialization/float.rb,
lib/rest_model/key/property/builder.rb,
lib/rest_model/key/relation/builder.rb,
lib/rest_model/serialization/string.rb,
lib/rest_model/serialization/symbol.rb,
lib/rest_model/key/embeddable/sender.rb,
lib/rest_model/key/property/response.rb,
lib/rest_model/key/relation/response.rb,
lib/rest_model/serialization/boolean.rb,
lib/rest_model/serialization/integer.rb,
lib/rest_model/key/embeddable/builder.rb,
lib/rest_model/key/property/retriever.rb,
lib/rest_model/key/embeddable/response.rb,
lib/rest_model/serialization/date_time.rb,
lib/rest_model/key/embeddable/retriever.rb,
lib/rest_model/serialization/enumerable.rb

Defined Under Namespace

Modules: Configuration, Response, Serialization, Source Classes: Association, Embeddable, Href, Key, Property, Relation, SerializationError, SourceError, TranslationError

Constant Summary collapse

VERSION =
"0.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Key::Builder

key, summarizes

Methods included from Property::Builder

#id, #properties, #property

Methods included from Embeddable::Builder

#embeds, #embeds_many, #embeds_one

Methods included from Relation::Builder

#belongs_to, #has_many, #has_one, #relation

Methods included from Source::Retriever

from_source, from_source!

Methods included from Response

#build_links, #resource, #resource_keys, #summarize?

Methods included from Source::Sender

#to_source, #to_source!

Constructor Details

#initialize(attrs = {}) ⇒ RestModel

Returns a new instance of RestModel.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rest_model.rb', line 46

def initialize(attrs = {})
  return if attrs.nil? or attrs.empty?

  attrs = attrs.with_indifferent_access

  assign_non_keys_attrs(attrs)

  self.class.keys.each do |key|
    __send__("#{key.name}=", key.from_hash(attrs[key.name])) if key.present?(self)
  end
end

Class Method Details

.convert_input_keys(converter = nil) ⇒ Object



107
108
109
110
# File 'lib/rest_model.rb', line 107

def self.convert_input_keys(converter = nil)
  @convert_input_keys = converter if converter
  @convert_input_keys
end

.id_keyObject



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

def self.id_key
  @id_key
end

.id_key=(id_key) ⇒ Object



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

def self.id_key=(id_key)
  @id_key = id_key
end

.keysObject



91
92
93
# File 'lib/rest_model.rb', line 91

def self.keys
  @keys ||= []
end

.not_allowed_namesObject



112
113
114
# File 'lib/rest_model.rb', line 112

def self.not_allowed_names
  %w(resource_id resource)
end

.relationsObject



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

def self.relations
  keys.find_all(&:relation?)
end

.resource_name(custom_name = nil) ⇒ Object



103
104
105
# File 'lib/rest_model.rb', line 103

def self.resource_name(custom_name = nil)
  @resource_name ||= custom_name or to_s.demodulize.tableize
end

.resource_name=(resource_name) ⇒ Object



87
88
89
# File 'lib/rest_model.rb', line 87

def self.resource_name=(resource_name)
  @resource_name = resource_name
end

.summarized_keysObject



95
96
97
# File 'lib/rest_model.rb', line 95

def self.summarized_keys
  @summarized_keys ||= []
end

Instance Method Details

#resource_idObject



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

def resource_id
  __send__(id_key.name)
end

#update_attributes(attrs = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rest_model.rb', line 58

def update_attributes(attrs = {})
  return if attrs.nil? or attrs.empty?

  attrs = attrs.with_indifferent_access

  assign_non_keys_attrs(attrs)

  self.class.keys.each do |key|
    if key.present?(self) and attrs.has_key?(key.name)
      value = attrs[key.name]
      __send__("#{key.name}=", key.from_hash(value, __send__(key.name)))
    end
  end

  self
end