Module: MinceModel::ClassMethods

Defined in:
lib/mince_model.rb

Instance Method Summary collapse

Instance Method Details

#add_assignable_field(field_name) ⇒ Object



47
48
49
50
51
# File 'lib/mince_model.rb', line 47

def add_assignable_field(field_name)
  fields << field_name
  assignable_fields << field_name
  attr_accessor field_name
end

#add_readonly_field(field_name) ⇒ Object



41
42
43
44
45
# File 'lib/mince_model.rb', line 41

def add_readonly_field(field_name)
  fields << field_name
  readonly_fields << field_name
  attr_reader field_name
end

#allObject



24
25
26
# File 'lib/mince_model.rb', line 24

def all
  data_model.all.map{|a| new a }
end

#assignable_fieldsObject



59
60
61
# File 'lib/mince_model.rb', line 59

def assignable_fields
  @assignable_fields ||= []
end

#data_model(model = nil) ⇒ Object



19
20
21
22
# File 'lib/mince_model.rb', line 19

def data_model(model=nil)
  @data_model = model if model
  @data_model
end

#field(field_name, options = {}) ⇒ Object



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

def field(field_name, options={})
  if options[:assignable]
    add_assignable_field(field_name)
  else
    add_readonly_field(field_name)
  end
end

#fields(*field_names) ⇒ Object



53
54
55
56
57
# File 'lib/mince_model.rb', line 53

def fields(*field_names)
  @fields ||= []
  field_names.each {|field_name| field(field_name) }
  @fields
end

#find(id) ⇒ Object



28
29
30
31
# File 'lib/mince_model.rb', line 28

def find(id)
  a = data_model.find(id)
  new a if a
end

#readonly_fieldsObject



63
64
65
# File 'lib/mince_model.rb', line 63

def readonly_fields
  @readonly_fields ||= []
end