class Strapi::ContentType
The class for defining a Ruby class that represents a Strapi
Content-Type
Attributes
fields[R]
attributes[R]
deleted[R]
id[R]
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/strapi/content_type.rb, line 8 def initialize(attributes = {}) @attributes = attributes.symbolize_keys end
Private Class Methods
_plural_id()
click to toggle source
# File lib/strapi/content_type.rb, line 129 def _plural_id @_plural_id ||= to_s.demodulize.tableize.dasherize end
all(params = {})
click to toggle source
# File lib/strapi/content_type.rb, line 101 def all(params = {}) get_list(params) end
create(attributes, params = {})
click to toggle source
# File lib/strapi/content_type.rb, line 109 def create(attributes, params = {}) new(attributes).save(params) end
field(attr, options = {})
click to toggle source
# File lib/strapi/content_type.rb, line 84 def field(attr, options = {}) @fields = [] if fields.nil? fields << attr define_method attr do strapi_attr_value(attr, options) end define_method "#{attr}=" do |value| attributes[attr] = value end end
find(id, params = {})
click to toggle source
# File lib/strapi/content_type.rb, line 97 def find(id, params = {}) new_from_response Request.get("#{_plural_id}/#{id}", params).data end
get_list(params)
click to toggle source
# File lib/strapi/content_type.rb, line 123 def get_list(params) Request.get(_plural_id, params).data.map do |result| new_from_response result end end
new_from_response(response)
click to toggle source
# File lib/strapi/content_type.rb, line 115 def new_from_response(response) return unless response new(response['attributes'].transform_keys(&:underscore)).tap do |entry| entry.instance_variable_set('@id', response['id']) end end
plural_id(name)
click to toggle source
# File lib/strapi/content_type.rb, line 80 def plural_id(name) @_plural_id = name end
where(params)
click to toggle source
# File lib/strapi/content_type.rb, line 105 def where(params) get_list(params) end
Public Instance Methods
==(other)
click to toggle source
# File lib/strapi/content_type.rb, line 18 def ==(other) other.is_a?(self.class) && id == other.id end
delete(params = {})
click to toggle source
# File lib/strapi/content_type.rb, line 33 def delete(params = {}) Request.delete("#{self.class.send(:_plural_id)}/#{id}", params) @attributes = {} @id = nil @deleted = true nil end
save(params = {})
click to toggle source
# File lib/strapi/content_type.rb, line 22 def save(params = {}) return if deleted response = id ? update_request(params) : create_request(params) entry = self.class.send(:new_from_response, response) tap do @attributes = attributes.deep_merge entry.attributes @id = entry.id end end
Private Instance Methods
create_request(params)
click to toggle source
# File lib/strapi/content_type.rb, line 43 def create_request(params) Request.post( "#{self.class.send(:_plural_id)}?#{params.to_query}", data: attributes.slice(*self.class.fields) ).data end
datetime_from_timestamp(key)
click to toggle source
# File lib/strapi/content_type.rb, line 57 def datetime_from_timestamp(key) return unless (timestamp = @attributes[key]) DateTime.parse timestamp end
strapi_attr_value(attr, options)
click to toggle source
# File lib/strapi/content_type.rb, line 63 def strapi_attr_value(attr, options) return unless (value = @attributes[attr]) return value unless (content_type = options[:content_type]) content_type_class = content_type.is_a?(String) ? content_type.constantize : content_type if (data = value['data']).is_a?(Array) data.map do |entry| content_type_class.send(:new_from_response, entry) end else content_type_class.send(:new_from_response, data) end end
update_request(params)
click to toggle source
# File lib/strapi/content_type.rb, line 50 def update_request(params) Request.put( "#{self.class.send(:_plural_id)}/#{id}?#{params.to_query}", data: attributes.slice(*self.class.fields) ).data end