module Frenchy::Model
Public Class Methods
included(base)
click to toggle source
# File lib/frenchy/model.rb, line 3 def self.included(base) base.extend(ClassMethods) base.class_eval do self.fields = {} self.defaults = {} end end
new(attrs={})
click to toggle source
Create a new instance of this model with the given attributes
# File lib/frenchy/model.rb, line 13 def initialize(attrs={}) attrs.stringify_keys! self.class.defaults.merge((attrs || {}).reject {|k,v| v.nil? }).each do |k,v| if self.class.fields[k] send("#{k}=", v) end end end
Public Instance Methods
attributes()
click to toggle source
Return a hash of field name as string and value pairs
# File lib/frenchy/model.rb, line 24 def attributes Hash[self.class.fields.map {|k,_| [k, send(k)]}] end
decorate(options={})
click to toggle source
Decorate the model using a decorator inferred by the class
# File lib/frenchy/model.rb, line 44 def decorate(options={}) decorator_class = "#{self.class.name}Decorator".constantize decorator_class.decorate(self, options) end
inspect()
click to toggle source
Return a string representing the value of the model instance
# File lib/frenchy/model.rb, line 39 def inspect "<#{self.class.name} #{attributes.map {|k,v| "#{k}: #{v.inspect}"}.join(", ")}>" end
persisted?()
click to toggle source
Returns that the model is persisted
# File lib/frenchy/model.rb, line 34 def persisted? true end
to_model()
click to toggle source
Returns a copy of the model
# File lib/frenchy/model.rb, line 29 def to_model self end
Protected Instance Methods
set(name, value)
click to toggle source
# File lib/frenchy/model.rb, line 51 def set(name, value) instance_variable_set("@#{name}", value) end