Class: MagicReport::Report
- Inherits:
-
Object
- Object
- MagicReport::Report
- Includes:
- ClassHelpers
- Defined in:
- lib/magic_report/report.rb,
lib/magic_report/report/csv.rb,
lib/magic_report/report/row.rb,
lib/magic_report/report/process.rb,
lib/magic_report/report/class_helpers.rb,
lib/magic_report/report/configuration.rb
Defined Under Namespace
Modules: ClassHelpers, Configuration Classes: Csv, Process, Row
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#has_many ⇒ Object
readonly
Returns the value of attribute has_many.
-
#has_one ⇒ Object
readonly
Returns the value of attribute has_one.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nested_field ⇒ Object
readonly
Returns the value of attribute nested_field.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
- .field(*attrs) ⇒ Object
- .fields(*attrs) ⇒ Object
- .has_many(attribute, opts = {}, &block) ⇒ Object
- .has_one(attribute, opts = {}, &block) ⇒ Object
- .t(key) ⇒ Object
Instance Method Summary collapse
- #as_attachment ⇒ Object
- #as_csv ⇒ Object
- #headings ⇒ Object
-
#initialize(fields: nil, has_one: nil, has_many: nil, name: nil, prefix: nil, nested_field: nil) ⇒ Report
constructor
A new instance of Report.
- #process(input) ⇒ Object
- #resolve_path(key) ⇒ Object
Methods included from ClassHelpers
#fields_from_class, #has_many_from_class, #has_one_from_class, #name_from_class
Constructor Details
#initialize(fields: nil, has_one: nil, has_many: nil, name: nil, prefix: nil, nested_field: nil) ⇒ Report
Returns a new instance of Report.
9 10 11 12 13 14 15 16 17 |
# File 'lib/magic_report/report.rb', line 9 def initialize(fields: nil, has_one: nil, has_many: nil, name: nil, prefix: nil, nested_field: nil) @fields = fields || fields_from_class @has_one = has_one || has_one_from_class @has_many = has_many || has_many_from_class @name = name || name_from_class @prefix = prefix @nested_field = nested_field end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def fields @fields end |
#has_many ⇒ Object (readonly)
Returns the value of attribute has_many.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def has_many @has_many end |
#has_one ⇒ Object (readonly)
Returns the value of attribute has_one.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def has_one @has_one end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def name @name end |
#nested_field ⇒ Object (readonly)
Returns the value of attribute nested_field.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def nested_field @nested_field end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def prefix @prefix end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/magic_report/report.rb', line 7 def result @result end |
Class Method Details
.field(*attrs) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/magic_report/report.rb', line 56 def field(*attrs) key, processor = attrs @fields ||= [] @fields << Configuration::Field.new(key: key, processor: processor) end |
.fields(*attrs) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/magic_report/report.rb', line 48 def fields(*attrs) @fields ||= [] Types::SymbolArray[attrs].each do |key| @fields << Configuration::Field.new(key: key) end end |
.has_many(attribute, opts = {}, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/magic_report/report.rb', line 79 def has_many(attribute, opts = {}, &block) @has_many ||= [] coerced_attribute = Types::Coercible::Symbol[attribute] klass = ::MagicReport::Utils.derive_class(opts, &block) opts[:name] = ::MagicReport::Utils.underscore(opts[:name].to_s) if opts[:name] if (prefix = opts[:prefix]) opts[:prefix] = new(name: ::MagicReport::Utils.underscore(name.to_s)).instance_exec(&prefix) end @has_many << Configuration::HasMany.new(klass: klass, opts: opts, key: coerced_attribute) end |
.has_one(attribute, opts = {}, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/magic_report/report.rb', line 63 def has_one(attribute, opts = {}, &block) @has_one ||= [] coerced_attribute = Types::Coercible::Symbol[attribute] klass = ::MagicReport::Utils.derive_class(opts, &block) opts[:name] = ::MagicReport::Utils.underscore(opts[:name].to_s) if opts[:name] if (prefix = opts[:prefix]) opts[:prefix] = new(name: ::MagicReport::Utils.underscore(name.to_s)).instance_exec(&prefix) end @has_one << Configuration::HasOne.new(klass: klass, opts: opts, key: coerced_attribute) end |
.t(key) ⇒ Object
44 45 46 |
# File 'lib/magic_report/report.rb', line 44 def t(key) ::MagicReport::Utils.t(name: name, key: key) end |
Instance Method Details
#as_attachment ⇒ Object
32 33 34 35 36 37 |
# File 'lib/magic_report/report.rb', line 32 def { mime_type: "text/csv", content: as_csv.io } end |
#as_csv ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/magic_report/report.rb', line 23 def as_csv @as_csv ||= begin csv = ::MagicReport::Report::Csv.new(self) csv.generate csv end end |
#headings ⇒ Object
39 40 41 |
# File 'lib/magic_report/report.rb', line 39 def headings @headings ||= (fields.map { |field| t(field.key) } + has_one.map { |association| association.report.headings } + has_many.map { |association| association.report.headings }).flatten end |
#process(input) ⇒ Object
19 20 21 |
# File 'lib/magic_report/report.rb', line 19 def process(input) @result = ::MagicReport::Report::Process.new(self).call(input) end |
#resolve_path(key) ⇒ Object
96 97 98 |
# File 'lib/magic_report/report.rb', line 96 def resolve_path(key) nested_field ? "#{nested_field}.#{key}".to_sym : key end |