Class: Rubanok::Plane

Inherits:
Object
  • Object
show all
Extended by:
DSL::Mapping, DSL::Matching
Defined in:
lib/rubanok/plane.rb

Overview

Base class for transformers (planes)

Define transformation rules via ‘map` and `match` methods and apply them by calling the plane:

class MyPlane < Rubanok::Plane
  map :type do
    raw.where(type: type)
  end
end

MyPlane.call(MyModel.all, {type: "public"})

NOTE: the second argument (‘params`) MUST be a Hash. Keys could be either Symbols or Strings (we automatically transform strings to symbols while matching rules).

All transformation methods are called within the context of the instance of a plane class.

You can access the input data via ‘raw` method.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Matching

match

Methods included from DSL::Mapping

map

Constructor Details

#initialize(input) ⇒ Plane

Returns a new instance of Plane.



57
58
59
# File 'lib/rubanok/plane.rb', line 57

def initialize(input)
  @input = input
end

Class Method Details

.add_rule(rule) ⇒ Object



41
42
43
# File 'lib/rubanok/plane.rb', line 41

def add_rule(rule)
  rules << rule
end

.call(input, params) ⇒ Object



37
38
39
# File 'lib/rubanok/plane.rb', line 37

def call(input, params)
  new(input).call(params)
end

.rulesObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/rubanok/plane.rb', line 45

def rules
  return @rules if instance_variable_defined?(:@rules)

  @rules =
    if superclass <= Plane
      superclass.rules.dup
    else
      []
    end
end

Instance Method Details

#call(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubanok/plane.rb', line 61

def call(params)
  params = params.symbolize_keys

  rules.each do |rule|
    next unless rule.applicable?(params)

    apply_rule! rule.to_method_name, rule.project(params)
  end

  input
end