The primary
concept in bottom-up programming is building abstractions from the lowest level. By
writing the lowest-level constructs first, you are essentially building your program on
top of those abstractions. In a sense, you are writing a domain-specific language in
which you build your programs.
4 | Chapter 1: Foundational Techniques
This concept is extremely useful in ActiveRecord. After creating your basic schema
and model objects, you can begin to build abstractions on top of those objects. Many
Rails projects start out by building abstractions on the model like this, before writing
a single line of controller code or even designing the web interface:
class Order < ActiveRecord::Base
has_many :line_items
def total
subtotal + shipping + tax
end
def subtotal
line_items.sum(:price)
end
def shipping
shipping_base_price + line_items.sum(:shipping)
end
def tax
subtotal * TAX_RATE
end
end
Ruby Foundations
This book relies heavily on a firm understanding of Ruby. This section will
explain some aspects of Ruby that are often confusing or misunderstood. Some of
this may be familiar, but these are important concepts that form the basis for the
metaprogramming techniques covered later in this chapter.
Classes and Modules
Classes and modules are the foundation of object-oriented programming in Ruby.
Classes facilitate encapsulation and separation of concerns.
Pages:
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29