Module: Musa::Series::Serie

Defined in:
lib/musa-dsl/series/base-series.rb

Defined Under Namespace

Modules: Prototyping

Class Method Summary collapse

Class Method Details

.baseObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/musa-dsl/series/base-series.rb', line 12

def self.base
  Module.new do
    include SerieImplementation

    def has_source; false; end
    private def mandatory_source; false; end

    def has_sources; false; end
    private def mandatory_sources; false; end
  end
end

.with(source: false, source_as: nil, private_source: nil, mandatory_source: nil, sources: false, sources_as: nil, private_sources: nil, mandatory_sources: nil, smart_block: false, block: false, block_as: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/musa-dsl/series/base-series.rb', line 24

def self.with(source: false,
              source_as: nil,
              private_source: nil,
              mandatory_source: nil,
              sources: false,
              sources_as: nil,
              private_sources: nil,
              mandatory_sources: nil,
              smart_block: false,
              block: false,
              block_as: nil)

  source_as ||= :source
  source_setter = (source_as.to_s + '=').to_sym
  _mandatory_source = source if mandatory_source.nil?

  sources_as ||= :sources
  sources_setter = (sources_as.to_s + '=').to_sym
  _mandatory_sources = sources if mandatory_sources.nil?

  block_as ||= :proc
  block_setter = (block_as.to_s + '=').to_sym

  Module.new do
    include SerieImplementation

    if source
      private def has_source; true; end
      define_method(:mandatory_source) { _mandatory_source }
      private :mandatory_source

      define_method source_as do
        @source
      end

      define_method source_setter do |serie|
        unless @source.nil? || @source.undefined? || serie.state == @source.state
          raise ArgumentError, "New serie for #{source_as} should be a #{@state} instead of a #{serie.state}"
        end

        @source = serie
        mark_regarding! @source
      end

      if private_source
        private source_as
        private source_setter
      end
    else
      private def has_source; false; end
      private def mandatory_source; false; end
    end

    if sources
      private def has_sources; true; end
      define_method(:mandatory_sources) { _mandatory_sources }
      private :mandatory_source

      define_method sources_as do
        @sources
      end

      define_method sources_setter do |series|
        unless series.is_a?(Hash) || series.is_a?(Array)
          raise ArgumentError, "New series for #{sources_as} should be a Hash or an Array instead of a #{series.class.name}"
        end

        @sources = series
        try_to_resolve_undefined_state_if_needed
      end

      if private_sources
        private sources_as
        private sources_setter
      end
    else
      private def has_sources; false; end
      private def mandatory_sources; false; end
    end

    if smart_block
      define_method block_as do |&block|
        if block
          @block = Extension::SmartProcBinder::SmartProcBinder.new(block)
        else
          @block.proc
        end
      end

      define_method block_setter do |block|
        @block = Extension::SmartProcBinder::SmartProcBinder.new(block)
      end

    elsif block
      define_method block_as do |&block|
        if block
          @block = block
        else
          @block
        end
      end

      define_method block_setter do |block|
        @block = block
      end
    end
  end
end