Module: Intermodal::DSL::PresentationHelpers

Included in:
API, Mapping::Mapper
Defined in:
lib/intermodal/dsl/presentation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#attribute(field) ⇒ Object

Use this to call a getter method on the model

Example:

using Intermodal::Functional

presentation_for :item do
  presents :price, with: attribute(:price) | helper(:number_to_currency)
end


47
48
49
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 47

def attribute(field)
  ->(resource) { resource.send(field) }
end

#helper(_method) ⇒ Object

Use this to access Rails ActionView helpers, most useful in presenters in conjunction with attribute Example:

using Intermodal::Functional

presentation_for :item do
  presents :price, with: attribute(:price) | helper(:number_to_currency)
end


71
72
73
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 71

def helper(_method)
  ActionController::Base.helpers.method(_method)
end

#map_to(hash) ⇒ Object

Use this to look up a value in a hash or anything that will accept a [] message (including ActiveModel objects). Useful when you want to convert a value into a human-friendly form, or for translations.

Example:

using Intermodal::Functional

presentation_for :ledger do
  presents :t_code
  presents :t_code_description, with: attribute(:t_code) | map_to(Ledger::Description)


85
86
87
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 85

def map_to(hash)
  ->(x) { hash[x] }
end

#maybe(f) ⇒ Object

Use this to handle nil cases

Example:

using Intermodal::Functional

acceptance_for :article do
  accepts :published_at, with: params(:published_at) | maybe(parse_datetime)
end


35
36
37
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 35

def maybe(f)
  ->(value) { value.nil? ? nil : f.call(value) }
end

#params(field) ⇒ Object

Use this to get a value from the params hash, most useful for acceptors

Example:

using Intermodal::Functional

acceptance_for :article do
  accepts :published_at, with: params(:published_at) | maybe(parse_datetime)
end


59
60
61
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 59

def params(field)
  ->(p) { p[field] }
end

#presenter(name, opt = {}) ⇒ Object

Use this to map to a presenter

Example:

presentation_for :account do
  presents :contact, with: presenter(:contact, scope: :details)
end


17
18
19
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 17

def presenter(name, opt={})
  attribute(name) | maybe(to_presentation(name, opt))
end

#to_datetimeObject

Use this to convert a string to a datetime. ActiveRecord .create() and .update_attributes() is not very robust when we try to put in different datatypes.

Assume that the date format that matters is in ISO 8601 format.

This will parse something to a datetime, and if it fails, we throw a BadRequest exception. Intermodal will rescue from that and respond with HTTP 400 Bad Request error.



96
97
98
99
100
101
102
103
104
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 96

def to_datetime
  @_to_datetime = proc do |dt|
    begin
      DateTime.iso8601(dt.to_s)
    rescue ArgumentError
      raise Intermodal::BadRequest
    end
  end
end

#to_presentation(name, opt = {}) ⇒ Object

Use this to map a value to a presenter Unlike #presenter() helper, this will not check if the attribute is ni



23
24
25
# File 'lib/intermodal/dsl/presentation_helpers.rb', line 23

def to_presentation(name, opt={})
  ->(value) { presenters[name].call(value, opt) }
end