module Firecord::Record
Public Class Methods
included(model)
click to toggle source
Calls superclass method
# File lib/firecord/record.rb, line 3 def self.included(model) model.extend(Model) super end
new(params = {})
click to toggle source
# File lib/firecord/record.rb, line 10 def initialize(params = {}) fields.each do |field| initialize_accessor(field, params) end end
Public Instance Methods
delete()
click to toggle source
# File lib/firecord/record.rb, line 30 def delete repository.delete(id) end
fields()
click to toggle source
# File lib/firecord/record.rb, line 66 def fields model.fields end
inspect()
click to toggle source
# File lib/firecord/record.rb, line 34 def inspect attrs = fields.map { |field| value = get_value(field.name) formatted = value.is_a?(String) ? "\"#{value}\"" : value "#{field.name}=#{formatted || 'nil'}" } "#<#{model.name} #{attrs.join(' ')}>" end
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/firecord/record.rb, line 55 def method_missing(method, *args, &block) return super unless available_names.include?(method) return set_value(method, args[0]) if method.to_s.end_with?('=') get_value(method) end
new?()
click to toggle source
# File lib/firecord/record.rb, line 45 def new? persistence_state == :transient end
persist()
click to toggle source
# File lib/firecord/record.rb, line 49 def persist @persistence_state = :persisted self end
respond_to_missing?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/firecord/record.rb, line 62 def respond_to_missing?(method, include_private = false) available_names.include?(method) || super end
save()
click to toggle source
# File lib/firecord/record.rb, line 16 def save return _create if new? _update end
update(attributes = {})
click to toggle source
# File lib/firecord/record.rb, line 22 def update(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end tap { |record| repository.patch(record) } end
Private Instance Methods
_create()
click to toggle source
# File lib/firecord/record.rb, line 91 def _create tap do |record| record.created_at = DateTime.now record.id = repository.post(persist)[:name] end end
_update()
click to toggle source
# File lib/firecord/record.rb, line 98 def _update tap do |record| record.updated_at = DateTime.now repository.patch(record) end end
available_names()
click to toggle source
# File lib/firecord/record.rb, line 87 def available_names fields.map(&:name) + fields.map { |field| :"#{field.name}=" } end
field_for(name)
click to toggle source
# File lib/firecord/record.rb, line 105 def field_for(name) sanitized_name = name.to_s.end_with?('=') ? :"#{name[0..-2]}" : name fields.find { |field| field.name == sanitized_name } end
get_value(name)
click to toggle source
# File lib/firecord/record.rb, line 79 def get_value(name) value = send("_#{name}") return value if value.nil? Deserializer.new(value, field_for(name).type).value end
initialize_accessor(field, params)
click to toggle source
# File lib/firecord/record.rb, line 118 def initialize_accessor(field, params) name = field.name restrict_accessor(name) value = params[name] || nil set_value("#{name}=", value, field.type) if value end
persistence_state()
click to toggle source
# File lib/firecord/record.rb, line 114 def persistence_state @persistence_state ||= :transient end
repository()
click to toggle source
# File lib/firecord/record.rb, line 110 def repository model.repository end
restrict_accessor(name)
click to toggle source
# File lib/firecord/record.rb, line 127 def restrict_accessor(name) self.class.class_eval do attr_accessor("_#{name}") end self.class.instance_eval do private(:"_#{name}") private(:"_#{name}=") end end
set_value(name, value, type = nil)
click to toggle source
# File lib/firecord/record.rb, line 72 def set_value(name, value, type = nil) type ||= field_for(name).type sanitizer = Serializer.new(value, type) send("_#{name}", sanitizer.value) end