Module: Frenchy::Model

Defined in:
lib/frenchy/model.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject

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(options={})
  decorator_class = "#{self.class.name}Decorator".constantize
  decorator_class.decorate(self, options)
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

#inspectObject

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/frenchy/model.rb', line 34

def persisted?
  true
end

#to_modelObject

Returns a copy of the model



29
30
31
# File 'lib/frenchy/model.rb', line 29

def to_model
  self
end