Module: MiniModel::ClassMethods

Defined in:
lib/mini_model.rb

Instance Method Summary collapse

Instance Method Details

#[](id) ⇒ Object



49
50
51
# File 'lib/mini_model.rb', line 49

def [](id)
  first(id: id)
end

#all(&block) ⇒ Object



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

def all(&block)
  build(dataset.all(&block))
end

#attribute(key, type = nil) ⇒ Object

Defines an accessor for the attributes hash. The whole point of the attributes hash vs. instance variables is for easily passing a hash to the dataset for persistence. Maybe this is a bad idea and we should use plain ol’ attr_accessor and build the hash when needed.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mini_model.rb', line 26

def attribute(key, type = nil)
  reader = :"#{key}"
  writer = :"#{key}="

  define_method(reader) do
    self.attributes[reader]
  end

  define_method(writer) do |value|
    self.attributes[reader] = value
  end
end

#build(dataset) ⇒ Object



39
40
41
# File 'lib/mini_model.rb', line 39

def build(dataset)
  dataset.map { |attributes| new(attributes) }
end

#child(association, model_name, foreign_key = to_foreign_key) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/mini_model.rb', line 89

def child(association, model_name, foreign_key = to_foreign_key)
  define_method(association) do
    model = self.class.const_get(model_name)

    model.first(foreign_key => id)
  end
end

#children(association, model_name, foreign_key = to_foreign_key) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/mini_model.rb', line 81

def children(association, model_name, foreign_key = to_foreign_key)
  define_method(association) do
    model = self.class.const_get(model_name)

    model.where(foreign_key => id)
  end
end

#create(attributes = {}) ⇒ Object

Convenience for initializin and persisting a new model instance.



45
46
47
# File 'lib/mini_model.rb', line 45

def create(attributes = {})
  new(attributes).create
end

#datasetObject



13
14
15
# File 'lib/mini_model.rb', line 13

def dataset
  @dataset
end

#dataset=(dataset) ⇒ Object



17
18
19
# File 'lib/mini_model.rb', line 17

def dataset=(dataset)
  @dataset = dataset
end

#first(*args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/mini_model.rb', line 53

def first(*args, &block)
  attributes = dataset.first(*args, &block)

  if attributes
    new(attributes)
  else
    nil
  end
end

#parent(association, model_name, foreign_key = :"#{association}_id") ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mini_model.rb', line 97

def parent(association, model_name, foreign_key = :"#{association}_id")
  reader = foreign_key
  writer = :"#{foreign_key}="

  define_method(reader) do
    self.attributes[reader]
  end

  define_method(writer) do |value|
    self.attributes[reader] = value
  end

  define_method(association) do
    model = self.class.const_get(model_name)

    model[send(foreign_key)]
  end

  define_method(:"#{association}=") do |value|
    if value
      send(writer, value.id)
    else
      send(writer, value)
    end
  end
end

#to_foreign_keyObject



71
72
73
74
75
76
77
78
79
# File 'lib/mini_model.rb', line 71

def to_foreign_key
  name.
    to_s.
    match(/^(?:.*::)*(.*)$/)[1].
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    downcase.
    concat('_id').
    to_sym
end

#where(*args, &block) ⇒ Object



67
68
69
# File 'lib/mini_model.rb', line 67

def where(*args, &block)
  build(dataset.where(*args, &block))
end