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.2.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.



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

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



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

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

.id_keyObject



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

def self.id_key
  @id_key
end

.id_key=(id_key) ⇒ Object



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

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

.keysObject



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

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

.not_allowed_namesObject



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

def self.not_allowed_names
  %w(resource_id resource)
end

.relationsObject



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

def self.relations
  @keys.find_all {|k| k.kind_of?(Relation)}
end

.resource_name(custom_name = nil) ⇒ Object



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

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

.resource_name=(resource_name) ⇒ Object



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

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

.summarized_keysObject



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

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

Instance Method Details

#resource_idObject



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

def resource_id
  __send__(id_key.name)
end

#update_attributes(attrs = {}) ⇒ Object



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

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