Class: Procedo::Procedure::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/procedo/procedure/field.rb

Overview

An Attribute defines an information to complete

Direct Known Subclasses

Computation, Handler, Setter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter, name, options = {}) ⇒ Field

Returns a new instance of Field.


133
134
135
136
137
# File 'lib/procedo/procedure/field.rb', line 133

def initialize(parameter, name, options = {})
  @parameter = parameter
  self.name = name
  @options = options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


6
7
8
# File 'lib/procedo/procedure/field.rb', line 6

def name
  @name
end

#parameterObject (readonly)

Returns the value of attribute parameter


6
7
8
# File 'lib/procedo/procedure/field.rb', line 6

def parameter
  @parameter
end

Class Method Details

.code_trees(*snippets) ⇒ Object


15
16
17
18
19
20
21
22
23
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
# File 'lib/procedo/procedure/field.rb', line 15

def code_trees(*snippets)
  options = snippets.extract_options!
  base_parse_options = {}
  base_parse_options[:root] = options[:root] if options[:root]
  snippets.each do |snippet|
    tree_name = snippet.to_s + '_tree'
    instance_var = '@' + tree_name
    attr_reader tree_name

    define_method "#{snippet}=" do |expr|
      parse_options = base_parse_options.merge(
        message: "Syntax error on #{self.class.name} (#{procedure_name}/#{parameter_name}##{@name}) #{snippet}."
      )
      instance_variable_set(instance_var, expr.blank? ? nil : parse!(expr.to_s, parse_options))
    end

    # Check if tree exist
    define_method "#{snippet}?" do
      instance_variable_get(instance_var).present?
    end

    # Check if given env variable is used
    define_method "#{snippet}_with_environment_variable?" do |*names|
      tree = instance_variable_get(instance_var)
      return false unless tree.present?
      names.each do |name|
        return true if detect_environment_variable(tree, name.to_s.upcase)
      end
    end
    alias_method "#{snippet}_env?", "#{snippet}_with_environment_variable?"

    # Check if given parameter is used
    define_method "#{snippet}_with_parameter?" do |parameter|
      tree = instance_variable_get(instance_var)
      return false unless tree.present?
      detect_parameter(tree, parameter)
    end

    # Check if given parameter is used
    define_method "#{snippet}_dependent_parameters" do
      tree = instance_variable_get(instance_var)
      return false unless tree.present?
      dependent_parameters(tree)
    end
  end
end

.count_variables(node, name) ⇒ Object

Count variables


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/procedo/procedure/field.rb', line 120

def count_variables(node, name)
  if (node.is_a?(Procedo::Formula::Language::Self) && name == :self) ||
     (node.is_a?(Procedo::Formula::Language::Variable) && name.to_s == node.text_value)
    return 1
  end
  return 0 unless node.elements
  node.elements.each_with_object(0) do |child, count|
    count += count_variables(child, name)
    count
  end
end

.dependent_parameters(root) ⇒ Object


87
88
89
90
91
92
93
94
95
96
# File 'lib/procedo/procedure/field.rb', line 87

def dependent_parameters(root)
  parameters = []
  each(root) do |node|
    if node.is_a?(Procedo::Formula::Language::Variable)
      name = node.text_value.to_sym
      parameters << name unless parameters.include?(name)
    end
  end
  parameters
end

.detect(root, &block) ⇒ Object


98
99
100
101
102
103
104
105
106
107
# File 'lib/procedo/procedure/field.rb', line 98

def detect(root, &block)
  return root if yield(root)
  if root.elements
    root.elements.each do |node|
      child = detect(node, &block)
      return child unless child.nil?
    end
  end
  nil
end

.detect_environment_variable(root, name) ⇒ Object

Detects environment variables for the given name


70
71
72
73
74
75
76
# File 'lib/procedo/procedure/field.rb', line 70

def detect_environment_variable(root, name)
  variable_name = name.to_s.upcase
  detect(root) do |node|
    node.is_a?(Procedo::Formula::Language::EnvironmentVariable) &&
      variable_name == node.text_value
  end
end

.detect_parameter(root, parameter) ⇒ Object

Detects environment variables for the given name


79
80
81
82
83
84
85
# File 'lib/procedo/procedure/field.rb', line 79

def detect_parameter(root, parameter)
  parameter_name = (parameter.is_a?(Procedo::Procedure::Parameter) ? parameter.name.to_s : parameter.to_s)
  detect(root) do |node|
    node.is_a?(Procedo::Formula::Language::Variable) &&
      parameter_name == node.text_value
  end
end

.each(root) {|root| ... } ⇒ Object

Yields:

  • (root)

109
110
111
112
113
114
115
116
117
# File 'lib/procedo/procedure/field.rb', line 109

def each(root, &block)
  yield(root)
  if root.elements
    root.elements.each do |node|
      each(node, &block)
    end
  end
  root
end

.parse!(code, options = {}) ⇒ Object


62
63
64
65
66
67
# File 'lib/procedo/procedure/field.rb', line 62

def parse!(code, options = {})
  return Procedo::Formula.parse(code.to_s, options)
rescue Procedo::Formula::SyntaxError => e
  raise (options[:message] || "Syntax error in #{code.inspect}.") + ' ' + e.message + "\n" +
        code + "\n" + ('' * e.failure_index) + ''
end