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.
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# 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
25 26 27 |
# File 'lib/frenchy/model.rb', line 25 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
35 36 37 38 |
# File 'lib/frenchy/model.rb', line 35 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
14 15 16 17 18 19 20 21 22 |
# File 'lib/frenchy/model.rb', line 14 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
30 31 32 |
# File 'lib/frenchy/model.rb', line 30 def inspect "<#{self.class.name} #{attributes.map {|k,v| "#{k}: #{v.inspect}"}.join(", ")}>" end |