Module: Representable::Binding::Factories

Included in:
Representable::Binding
Defined in:
lib/representable/pipeline_factories.rb

Instance Method Summary collapse

Instance Method Details

#collect_for(item_functions) ⇒ Object

i decided not to use polymorphism here for the sake of clarity.



12
13
14
15
16
17
# File 'lib/representable/pipeline_factories.rb', line 12

def collect_for(item_functions)
  return [Collect[*item_functions]] if array?
  return [Collect::Hash[*item_functions]] if self[:hash]

  item_functions
end

#default_parse_fragment_functionsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/representable/pipeline_factories.rb', line 68

def default_parse_fragment_functions
  functions = [AssignFragment]
  functions << SkipParse if self[:skip_parse]

  if self[:class] or self[:extend] or self[:instance] or self[:populator]
    if self[:populator]
      functions << CreateObject::Populator
    elsif self[:parse_strategy]
      functions << CreateObject::Instance # TODO: remove in 2.5.
    else
      functions << (self[:class] ? CreateObject::Class : CreateObject::Instance)
    end

    functions << Prepare     if self[:prepare]
    functions << Decorate    if self[:extend]
    if representable?
      functions << (self[:deserialize] ? Deserializer : Deserialize)
    end
  end

  functions
end

#default_parse_init_functionsObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/representable/pipeline_factories.rb', line 57

def default_parse_init_functions
  functions = []
  functions << Stop if self[:writeable]==false
  functions << StopOnExcluded
  functions << If if self[:if]
  functions << (self[:as] ? AssignAs : AssignName)
  functions << (self[:reader] ? Reader : ReadFragment)
  functions << (has_default? ? Default : StopOnNotFound)
  functions << OverwriteOnNil # include StopOnNil if you don't want to erase things.
end

#default_post_functionsObject



91
92
93
94
95
# File 'lib/representable/pipeline_factories.rb', line 91

def default_post_functions
  funcs = []
  funcs << ParseFilter if self[:parse_filter].any?
  funcs << (self[:setter] ? Setter : SetValue)
end

#default_render_fragment_functionsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/representable/pipeline_factories.rb', line 28

def default_render_fragment_functions
  functions = []
  functions << SkipRender if self[:skip_render]
  if typed? # TODO: allow prepare regardless of :extend, which makes it independent of typed?
    if self[:prepare]
      functions << Prepare
    end
    # functions << (self[:prepare] ? Prepare : Decorate)
  end
  functions << Decorate if self[:extend] and !self[:prepare]
  if representable?
    functions << (self[:serialize] ? Serializer : Serialize)
  end
  functions
end

#default_render_init_functionsObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/representable/pipeline_factories.rb', line 44

def default_render_init_functions
  functions = []
  functions << Stop if self[:readable]==false
  functions << StopOnExcluded
  functions << If if self[:if]
  functions << (self[:getter] ? Getter : GetValue)
  functions << Writer if self[:writer]
  functions << RenderFilter if self[:render_filter].any?
  functions << RenderDefault if has_default?
  functions << StopOnSkipable
  functions << (self[:as] ? AssignAs : AssignName)
end

#parse_functionsObject



19
20
21
# File 'lib/representable/pipeline_factories.rb', line 19

def parse_functions
  [*default_parse_init_functions, *collect_for(default_parse_fragment_functions), *default_post_functions]
end

#pipeline_for(name, input, options) ⇒ Object



4
5
6
7
8
9
# File 'lib/representable/pipeline_factories.rb', line 4

def pipeline_for(name, input, options)
  return yield unless (proc = @definition[name])

  # proc.(self, options)
  instance_exec(input, options, &proc)
end

#render_functionsObject

DISCUSS: StopOnNil, before collect



24
25
26
# File 'lib/representable/pipeline_factories.rb', line 24

def render_functions
  [*default_render_init_functions, *collect_for(default_render_fragment_functions), WriteFragment]
end