Class: McCracken::Resource
- Extended by:
- Forwardable
- Defined in:
- lib/mccracken/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Class Method Summary collapse
- .attribute(attribute_name, cast_type, **options) ⇒ Object
-
.fields(*args) ⇒ Object
Overwrite Connection#fields delegator to allow for passing an array of fields.
- .format_id(id) ⇒ Object
- .has_many(relation_name) ⇒ Object
- .has_one(relation_name) ⇒ Object
- .inherited(subclass) ⇒ Object
- .key_type(type) ⇒ Object
- .mccracken ⇒ Object
- .mccracken_initializer(document) ⇒ Object
- .schema ⇒ Object
-
.type ⇒ Object
Get the JSONAPI type.
-
.type=(type) ⇒ Object
Set the JSONAPI type.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#agent ⇒ McCracken::Agent
A new Agent instance.
- #destroy ⇒ Object
-
#errors ⇒ Array<Hash>
Array of JSON API errors.
- #errors? ⇒ Boolean
- #id ⇒ Object
-
#initialize(attrs = {}) ⇒ Resource
constructor
A new instance of Resource.
- #initialize_attrs ⇒ Object
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #serialized_attributes ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Resource
Returns a new instance of Resource.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mccracken/resource.rb', line 14 def initialize(attrs = {}) if attrs.is_a?(McCracken::Document) @document = attrs else @document = McCracken::Document.new( data: { type: self.class.type, id: attrs.delete(:id), attributes: attrs } ) end initialize_attrs end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/mccracken/resource.rb', line 4 def attributes @attributes end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
3 4 5 |
# File 'lib/mccracken/resource.rb', line 3 def document @document end |
Class Method Details
.attribute(attribute_name, cast_type, **options) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mccracken/resource.rb', line 115 def attribute(attribute_name, cast_type, **) schema[attribute_name] = McCracken::Attribute.new(attribute_name, cast_type, ) 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
178 179 180 181 182 |
# File 'lib/mccracken/resource.rb', line 178 def fields(*args) hash_fields = args.last.is_a?(Hash) ? args.pop : {} hash_fields[type] = args if args.any? mccracken.fields(hash_fields) end |
.format_id(id) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mccracken/resource.rb', line 100 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
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mccracken/resource.rb', line 142 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 = McCracken::Collection.new(documents.map{ |doc| McCracken.factory(doc) }) @_#{relation_name}_relationship = collection end RUBY end |
.has_one(relation_name) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/mccracken/resource.rb', line 132 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 = McCracken.factory(related_document) end RUBY end |
.inherited(subclass) ⇒ Object
90 91 92 93 94 |
# File 'lib/mccracken/resource.rb', line 90 def inherited(subclass) if subclass.to_s.respond_to?(:tableize) subclass.type = subclass.to_s.tableize.to_sym end end |
.key_type(type) ⇒ Object
96 97 98 |
# File 'lib/mccracken/resource.rb', line 96 def key_type(type) @key_type = type end |
.mccracken ⇒ Object
157 158 159 160 161 |
# File 'lib/mccracken/resource.rb', line 157 def mccracken return @mccracken if @mccracken @mccracken = McCracken::Client.new @mccracken end |
.mccracken_initializer(document) ⇒ Object
153 154 155 |
# File 'lib/mccracken/resource.rb', line 153 def mccracken_initializer(document) new(document) end |
.schema ⇒ Object
111 112 113 |
# File 'lib/mccracken/resource.rb', line 111 def schema @schema ||= {} end |
.type ⇒ Object
Get the JSONAPI type
170 171 172 |
# File 'lib/mccracken/resource.rb', line 170 def type mccracken.type end |
.type=(type) ⇒ Object
Set the JSONAPI type
164 165 166 167 |
# File 'lib/mccracken/resource.rb', line 164 def type=(type) McCracken.register_type(type, self) mccracken.type = type end |
Instance Method Details
#==(other) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/mccracken/resource.rb', line 79 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 |
#agent ⇒ McCracken::Agent
Returns a new Agent instance.
66 67 68 |
# File 'lib/mccracken/resource.rb', line 66 def agent self.class.mccracken.agent end |
#destroy ⇒ Object
47 48 49 |
# File 'lib/mccracken/resource.rb', line 47 def destroy document.destroy(agent) end |
#errors ⇒ Array<Hash>
Returns array of JSON API errors.
57 58 59 |
# File 'lib/mccracken/resource.rb', line 57 def errors document.errors end |
#errors? ⇒ Boolean
61 62 63 |
# File 'lib/mccracken/resource.rb', line 61 def errors? document.errors.any? end |
#id ⇒ Object
30 31 32 33 |
# File 'lib/mccracken/resource.rb', line 30 def id return nil if document.id.nil? @id ||= self.class.format_id(document.id) end |
#initialize_attrs ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/mccracken/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
43 44 45 |
# File 'lib/mccracken/resource.rb', line 43 def persisted? !id.nil? end |
#save ⇒ Object
51 52 53 54 |
# File 'lib/mccracken/resource.rb', line 51 def save @document = document.save(agent) !errors? end |
#serialized_attributes ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/mccracken/resource.rb', line 70 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 |