Module: Femto::Model

Defined in:
lib/femto/model.rb,
lib/femto/model/mongo_adapter.rb

Defined Under Namespace

Classes: ModelCreator, MongoAdapter

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



8
9
10
# File 'lib/femto/model.rb', line 8

def adapter
  @adapter
end

Class Method Details

.camelize(snake_case, first_upcase = true) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/femto/model.rb', line 133

def self.camelize(snake_case, first_upcase=true)
  if first_upcase
    snake_case.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
  else
    snake_case.first + camelize(snake_case)[1..-1]
  end
end

.create_model(name, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/femto/model.rb', line 11

def self.create_model(name, &block)
  class_name = camelize name
  begin
    Object.const_get class_name
    return # Class already exists
  rescue NameError
    # ignored
  end
  model_creator = ModelCreator.new
  block.call model_creator

  # Create class
  model_class = Class.new &model_creator.class_opts
  Object.const_set(class_name, model_class)

  # Create accessors for attributes
  model_creator.fields.each do |a|
    model_class.module_eval { attr_accessor a }
  end
  model_class.module_eval { attr_accessor :id }

  # Add custom methods
  model_creator.custom_methods.each_pair do |method_name, method_block|
    model_class.module_eval { define_method method_name, &method_block }
  end

  # Add model methods
  model_class.define_singleton_method 'find' do |query={}|
    Model.adapter.find(model_class, query)
  end
  model_class.define_singleton_method 'where' do |query={}|
    Model.adapter.find(model_class, query)
  end
  model_class.define_singleton_method 'create' do |fields={}|
    model = model_class.new fields
    model.save
    model
  end
  model_class.define_singleton_method 'all' do
    model_class.find
  end

  model_class.module_eval do
    define_method 'update' do
      Model.adapter.update self
    end

    define_method 'save' do
      Model.adapter.update self
    end

    define_method('remove') { Model.adapter.remove self }
    define_method('delete') { Model.adapter.remove self }

    define_method 'to_hash' do
      result = {}
      self.class.model_attrs[:fields].each do |f|
        val = send f
        result[f] = val if val
      end
      result
    end

    define_method 'initialize' do |fields={}|
      fields.each_pair do |key, val|
        next unless self.class.model_attrs[:fields].include? key
        send key.to_s + '=', val
      end
    end
  end

  # Model attributes (fields, storage, etc...)
  class << model_class
    attr_accessor :model_attrs
  end
  storage = model_creator.storage_name ? model_creator.storage_name.to_s : name + 's'
  model_class.model_attrs = {
      fields: model_creator.fields,
      storage_name: storage
  }

  # Create method for getting defined fields
  model_class.define_singleton_method('fields') { model_creator.fields }
end