Module: Exegesis::Model::ClassMethods

Defined in:
lib/exegesis/model.rb

Instance Method Summary collapse

Instance Method Details

#default(hash = nil) ⇒ Object

sets a default hash object for the attributes of the instances of the class if an argument given, else retrieves the default



33
34
35
36
37
38
39
40
# File 'lib/exegesis/model.rb', line 33

def default hash=nil
  if hash
    @default = {}
    hash.each {|key, value| @default[key.to_s] = value }
  else
    @default ||= {}
  end
end

#expose(*attrs) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/exegesis/model.rb', line 12

def expose *attrs
  opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
  [attrs].flatten.each do |attrib|
    attrib = attrib.to_s
    if opts[:writer]
      define_writer(attrib) {|val| @attributes[attrib] = opts[:writer].call(val) }
    elsif !opts.has_key?(:writer)
      define_writer(attrib) {|val| @attributes[attrib] = val }
    end
    if opts[:as] == :reference
      define_reference attrib
    elsif opts[:as]
      define_caster attrib, opts[:as]
    else
      define_method(attrib) { @attributes[attrib] }
    end
  end
end