flatten_record 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3f760e4ba28e47a8cb7e46621300db8c99d36c9
4
- data.tar.gz: 4129c16a709afbd09567ee9ce888257651f20a19
3
+ metadata.gz: ca6514cd5417800256fc9d02679ebfc487b9434d
4
+ data.tar.gz: dd8e7587673dd14b19d7692844c5af44617ef090
5
5
  SHA512:
6
- metadata.gz: 5713c428677377b05b1feb93bc50fa821c173e6ccc33a52c6c571e1731c495d372b4f9006920d4ef811686b8d990ee6ef2f513f3efa3a9861e1d88fc71f851ba
7
- data.tar.gz: 3ea39337ec9243801d6883bf1831267e3bbb28f70f6bf6af543dd8684e19ea7a4456e9c3c4029e2c507901d43900ba149525f9c94fa2c72e27148c8951a5cf2f
6
+ metadata.gz: 92f296a7b05159983e97ec7c8ed736eec0533aeabd9a0051bba644bf780cebeeed140d208d7d3f24352d26034e15f3fa0cd465a2606fcbc3c8157ea4d8ac1e1d
7
+ data.tar.gz: 178e1bb147d516a3adcbf46508badc083642ace332338a6bb7942debc3e8646352a8b94646654177f851fa6fb72849a1c1a3440b207f984b02ad3d0a096b8430
data/README.md CHANGED
@@ -1,8 +1,35 @@
1
1
  # FlattenRecord [![Code Climate](https://codeclimate.com/github/alvinsj/flatten_record.png)](https://codeclimate.com/github/alvinsj/flatten_record) [![Build Status](https://travis-ci.org/alvinsj/flatten_record.png?branch=master)](https://travis-ci.org/alvinsj/flatten_record)
2
2
 
3
- An ActiveRecord plugin that denormalizes your existing ActiveRecord models.
3
+ An ActiveRecord plugin that helps to denormalize your existing ActiveRecord models.
4
4
 
5
- It provides an easier way to create denormalized records to be used for reporting, includes generation of migration file.
5
+ It provides an easier way to denormalize/flatten normalized records, and generation the table schema.
6
+
7
+ ## Example
8
+
9
+ Existing normalized tables
10
+
11
+ | orders | customers | line_items |
12
+ |:-------|:---------|:-----------|
13
+ | id | id | id |
14
+ | customer_id | name | description |
15
+ | discount | | total |
16
+ | total | | order_id |
17
+
18
+ Denormalized table, generated by flatten_record
19
+
20
+ |denormalized_orders|
21
+ |:-------------------|
22
+ | id |
23
+ | order_id |
24
+ | discount |
25
+ | total |
26
+ | customer_id |
27
+ | customer_name |
28
+ | line_item_id |
29
+ | line_item_description |
30
+ | line_item_total |
31
+ | line_items_sum (custom column) |
32
+ | total_in_usd (custom column) |
6
33
 
7
34
  ## Usage
8
35
 
@@ -19,19 +46,24 @@ Include module in your newly defined model
19
46
  include FlattenRecord::Flattener
20
47
 
21
48
  denormalize :order, {
49
+
50
+ # specifying association
22
51
  include: {
23
- # :belongs_to association
52
+ # :belongs_to association
24
53
  customer: {}
25
54
 
26
55
  # :has_many association, create multiple denormalized records
27
56
  line_items: {}
28
- },
29
- methods: {
30
- # save methods defined in Normalized model
57
+
58
+ },
59
+
60
+ # save results of methods defined in Normalized model
61
+ methods: {
31
62
  total_in_usd: :decimal
32
63
  },
64
+
65
+ # compute results of methods defined in Denormalized model
33
66
  compute: {
34
- # compute methods defined in Denormalized model
35
67
  line_items_sum: { type: :decimal, default: 0 }
36
68
  }
37
69
  }
@@ -40,9 +72,9 @@ Include module in your newly defined model
40
72
  def compute_line_items_sum(order)
41
73
  order.line_items.collect(&:total).inject(:+)
42
74
  end
43
- end
75
+ end
44
76
 
45
- class Order < ActiveRecord::Base
77
+ class Order < ActiveRecord::Base
46
78
  def total_in_usd
47
79
  # calculation
48
80
  end
@@ -58,8 +90,8 @@ Update definition and generate new migration file
58
90
  $ rails generate flatten_record:migration denormalized_order
59
91
  Warning. Table already exists: denormalized_orders
60
92
  Generating migration based on the difference..
61
- Add columns: d_line_items_description
62
- create db/migrate/20140313034736_add_d_line_items_description_to_denormalized_orders.rb
93
+ Add columns: line_items_description
94
+ create db/migrate/20140313034736_add_line_items_description_to_denormalized_orders.rb
63
95
 
64
96
  ### Use denormalizer methods
65
97
  Create record
@@ -74,7 +106,7 @@ Update record(s)
74
106
 
75
107
  irb(main)> DenormalizedOrder.update_with(order)
76
108
 
77
- ## Design & Documentation
109
+ ## API Documentation/Development/Discussion
78
110
 
79
111
  Refer to the [wiki](https://github.com/alvinsj/flatten_record/wiki).
80
112
 
@@ -10,16 +10,9 @@ module FlattenRecord
10
10
  end
11
11
 
12
12
  def denormalize(instance, to_record)
13
- first_record = to_record.respond_to?(:each) ? to_record.flatten.first : to_record
14
-
15
- begin
16
- to_record = assign_value(to_record, name) do |record|
17
- record.send("compute_#{@column.name}".to_sym, instance)
18
- end
19
- rescue
20
- raise "compute_#{@column.name} is not found in #{to_record.inspect}"
13
+ assign_value(to_record, name) do |record|
14
+ record.send("compute_#{@column.name}".to_sym, instance)
21
15
  end
22
- to_record
23
16
  end
24
17
  end
25
18
  end
@@ -1,3 +1,3 @@
1
1
  module FlattenRecord
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end