Module: Frenchy::Model
- Defined in:
- lib/frenchy/model.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#attributes ⇒ Object
Return a hash of field name as string and value pairs.
-
#decorate(options = {}) ⇒ Object
Decorate the model using a decorator inferred by the class.
-
#initialize(attrs = {}) ⇒ Object
Create a new instance of this model with the given attributes.
-
#inspect ⇒ Object
Return a string representing the value of the model instance.
-
#persisted? ⇒ Boolean
Returns that the model is persisted.
-
#to_model ⇒ Object
Returns a copy of the model.
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/frenchy/model.rb', line 3 def self.included(base) base.extend(ClassMethods) base.class_eval do self.fields = {} self.defaults = {} end end |
Instance Method Details
#attributes ⇒ Object
Return a hash of field name as string and value pairs
24 25 26 |
# File 'lib/frenchy/model.rb', line 24 def attributes Hash[self.class.fields.map {|k,_| [k, send(k)]}] end |
#decorate(options = {}) ⇒ Object
Decorate the model using a decorator inferred by the class
44 45 46 47 |
# File 'lib/frenchy/model.rb', line 44 def decorate(={}) decorator_class = "#{self.class.name}Decorator".constantize decorator_class.decorate(self, ) end |
#initialize(attrs = {}) ⇒ Object
Create a new instance of this model with the given attributes
13 14 15 16 17 18 19 20 21 |
# 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 |
#inspect ⇒ Object
Return a string representing the value of the model instance
39 40 41 |
# File 'lib/frenchy/model.rb', line 39 def inspect "<#{self.class.name} #{attributes.map {|k,v| "#{k}: #{v.inspect}"}.join(", ")}>" end |
#persisted? ⇒ Boolean
Returns that the model is persisted
34 35 36 |
# File 'lib/frenchy/model.rb', line 34 def persisted? true end |
#to_model ⇒ Object
Returns a copy of the model
29 30 31 |
# File 'lib/frenchy/model.rb', line 29 def to_model self end |