Module: Parametric::Struct::ClassMethods

Defined in:
lib/parametric/struct.rb

Constant Summary collapse

PLURAL_END =
/s$/.freeze

Instance Method Summary collapse

Instance Method Details

#__class_name(key) ⇒ Object



133
134
135
# File 'lib/parametric/struct.rb', line 133

def __class_name(key)
  key.to_s.split('_').map(&:capitalize).join.sub(PLURAL_END, '')
end

#build(attrs) ⇒ Object



102
103
104
105
106
# File 'lib/parametric/struct.rb', line 102

def build(attrs)
  attrs.each_with_object({}) do |(k, v), obj|
    obj[k] = wrap(k, v)
  end
end

#new!(attrs = {}) ⇒ Object

Raises:



57
58
59
60
61
# File 'lib/parametric/struct.rb', line 57

def new!(attrs = {})
  st = new(attrs)
  raise InvalidStructError.new(st) unless st.valid?
  st
end

#parametric_after_define_schema(schema) ⇒ Object

this hook is called after schema definition in DSL module



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
# File 'lib/parametric/struct.rb', line 64

def parametric_after_define_schema(schema)
  schema.fields.values.each do |field|
    if field.[:schema]
      case field.[:schema]
      when Parametric::Schema
        klass = Class.new do
          include Struct
        end
        klass.schema = field.[:schema]
        self.const_set(__class_name(field.key), klass)
        klass.parametric_after_define_schema(field.[:schema])
      when Array
        # Handle one_of fields: create multiple struct classes, one for each possible schema
        # This allows the field to instantiate the appropriate nested struct based on which schema validates
        classes = field.[:schema].map.with_index(1) do |sc, idx|
          klass = Class.new do
            include Struct
          end
          klass.schema = sc
          class_name = "#{__class_name(field.key)}#{idx}"
          self.const_set(__class_name(class_name), klass)
          klass.parametric_after_define_schema(sc)
          klass
        end
        self.const_set(__class_name(field.key), classes)
      else
        self.const_set(__class_name(field.key), field.[:schema])
      end
    end

    self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{field.key}
        _graph[:#{field.key}]
      end
    RUBY
  end
end

#wrap(key, value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/parametric/struct.rb', line 108

def wrap(key, value)
  case value
  when Hash
    # find constructor for field
    cons = self.const_get(__class_name(key))
    case cons
    when Class # Single nested struct
      cons.new(value)
    when Array # Array of possible nested structs (one_of)
      # For one_of fields: instantiate all possible struct classes and return the first valid one
      # This allows the struct to automatically choose the correct nested structure based on the data
      structs = cons.map{|c| c.new(value) }
      structs.find(&:valid?) || structs.first
    else
      value.freeze
    end
  when Array
    value.map{|v| wrap(key, v) }.freeze
  else
    value.freeze
  end
end