Class: Munson::Resource

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/munson/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Resource

Returns a new instance of Resource.

Examples:

Given a Munson::Document

document = Munson::Document.new(jsonapi_hash)
Person.new(document)

Given an attributes hash

Person.new(first_name: "Chauncy", last_name: "Vünderboot")

Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/munson/resource.rb', line 14

def initialize(attrs = {})
  if attrs.is_a?(Munson::Document)
    @document = attrs
  else
    @document = Munson::Document.new(
      data: {
        type: self.class.type,
        id: attrs.delete(:id),
        attributes: attrs
      }
    )
  end

  initialize_attrs
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/munson/resource.rb', line 4

def attributes
  @attributes
end

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/munson/resource.rb', line 3

def document
  @document
end

Class Method Details

.attribute(attribute_name, cast_type, **options) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/munson/resource.rb', line 111

def attribute(attribute_name, cast_type, **options)
  schema[attribute_name] = Munson::Attribute.new(attribute_name, cast_type, options)

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{attribute_name}
      @attributes[:#{attribute_name}]
    end
  RUBY

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{attribute_name}=(val)
      document.attributes[:#{attribute_name}] = self.class.schema[:#{attribute_name}].serialize(val)
      @attributes[:#{attribute_name}] = val
    end
  RUBY
end

.fields(*args) ⇒ Object

Overwrite Connection#fields delegator to allow for passing an array of fields

Examples:

Cat.fields(:name, :favorite_toy) #=> Query(fields[cats]=name,favorite_toy)
Cat.fields(name, owner: [:name]) #=> Query(fields[cats]=name&fields[people]=name)


174
175
176
177
178
# File 'lib/munson/resource.rb', line 174

def fields(*args)
  hash_fields = args.last.is_a?(Hash) ? args.pop : {}
  hash_fields[type] = args if args.any?
  munson.fields(hash_fields)
end

.format_id(id) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/munson/resource.rb', line 96

def format_id(id)
  case @key_type
  when :integer, nil
    id.to_i
  when :string
    id.to_s
  when Proc
    @key_type.call(id)
  end
end

.has_many(relation_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/munson/resource.rb', line 138

def has_many(relation_name)
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{relation_name}
      return @_#{relation_name}_relationship if @_#{relation_name}_relationship
      documents  = document.relationship(:#{relation_name})
      collection = Munson::Collection.new(documents.map{ |doc| Munson.factory(doc) })
      @_#{relation_name}_relationship = collection
    end
  RUBY
end

.has_one(relation_name) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/munson/resource.rb', line 128

def has_one(relation_name)
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{relation_name}
      return @_#{relation_name}_relationship if @_#{relation_name}_relationship
      related_document = document.relationship(:#{relation_name})
      @_#{relation_name}_relationship = Munson.factory(related_document)
    end
  RUBY
end

.inherited(subclass) ⇒ Object



86
87
88
89
90
# File 'lib/munson/resource.rb', line 86

def inherited(subclass)
  if subclass.to_s.respond_to?(:tableize)
    subclass.type = subclass.to_s.tableize.to_sym
  end
end

.key_type(type) ⇒ Object



92
93
94
# File 'lib/munson/resource.rb', line 92

def key_type(type)
  @key_type = type
end

.munsonObject



153
154
155
156
157
# File 'lib/munson/resource.rb', line 153

def munson
  return @munson if @munson
  @munson = Munson::Client.new
  @munson
end

.munson_initializer(document) ⇒ Object



149
150
151
# File 'lib/munson/resource.rb', line 149

def munson_initializer(document)
  new(document)
end

.schemaObject



107
108
109
# File 'lib/munson/resource.rb', line 107

def schema
  @schema ||= {}
end

.typeObject

Get the JSONAPI type



166
167
168
# File 'lib/munson/resource.rb', line 166

def type
  munson.type
end

.type=(type) ⇒ Object

Set the JSONAPI type



160
161
162
163
# File 'lib/munson/resource.rb', line 160

def type=(type)
  Munson.register_type(type, self)
  munson.type = type
end

Instance Method Details

#==(other) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/munson/resource.rb', line 75

def ==(other)
  return false if !other

  if other.class.respond_to?(:type)
    self.class.type == other.class.type && self.id == other.id
  else
    false
  end
end

#agentMunson::Agent

Returns a new Agent instance.

Returns:



62
63
64
# File 'lib/munson/resource.rb', line 62

def agent
  self.class.munson.agent
end

#errorsArray<Hash>

Returns array of JSON API errors.

Returns:



53
54
55
# File 'lib/munson/resource.rb', line 53

def errors
  document.errors
end

#errors?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/munson/resource.rb', line 57

def errors?
  document.errors.any?
end

#idObject



30
31
32
33
# File 'lib/munson/resource.rb', line 30

def id
  return nil if document.id.nil?
  @id ||= self.class.format_id(document.id)
end

#initialize_attrsObject



35
36
37
38
39
40
41
# File 'lib/munson/resource.rb', line 35

def initialize_attrs
  @attributes = @document.attributes.clone
  self.class.schema.each do |name, attribute|
    casted_value = attribute.process(@attributes[name])
    @attributes[name] = casted_value
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/munson/resource.rb', line 43

def persisted?
  !id.nil?
end

#saveObject



47
48
49
50
# File 'lib/munson/resource.rb', line 47

def save
  @document = document.save(agent)
  !errors?
end

#serialized_attributesObject



66
67
68
69
70
71
72
73
# File 'lib/munson/resource.rb', line 66

def serialized_attributes
  serialized_attrs = {}
  self.class.schema.each do |name, attribute|
    serialized_value = attribute.serialize(@attributes[name])
    serialized_attrs[name] = serialized_value
  end
  serialized_attrs
end