Class: Trax::Core::Transformer

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/trax/core/transformer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = {}, parent = nil) ⇒ Transformer

Returns a new instance of Transformer.



79
80
81
82
83
84
85
86
87
88
# File 'lib/trax/core/transformer.rb', line 79

def initialize(obj={}, parent=nil)
  @input = obj.dup
  @output = {}.with_indifferent_access
  @parent = parent if parent

  initialize_output_properties
  initialize_default_values
  run_after_initialize_callbacks if run_after_initialize_callbacks?
  run_after_transform_callbacks if run_after_transform_callbacks?
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



4
5
6
# File 'lib/trax/core/transformer.rb', line 4

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/trax/core/transformer.rb', line 4

def output
  @output
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/trax/core/transformer.rb', line 4

def parent
  @parent
end

Class Method Details

.after_initialize(&block) ⇒ Object



15
16
17
# File 'lib/trax/core/transformer.rb', line 15

def self.after_initialize(&block)
  after_initialize_callbacks << block
end

.after_transform(&block) ⇒ Object



19
20
21
# File 'lib/trax/core/transformer.rb', line 19

def self.after_transform(&block)
  after_transform_callbacks << block
end

.fetch_property_from_object(_property, obj) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/trax/core/transformer.rb', line 70

def self.fetch_property_from_object(_property, obj)
  if _property.include?('/')
    property_chain = _property.split('/')
    obj.dig(*property_chain)
  else
    obj[_property]
  end
end

.from_parent?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/trax/core/transformer.rb', line 43

def self.from_parent?
  false
end

.inherited(subklass) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/trax/core/transformer.rb', line 6

def self.inherited(subklass)
  subklass.class_attribute :properties
  subklass.properties = {}.with_indifferent_access
  subklass.class_attribute :after_initialize_callbacks
  subklass.after_initialize_callbacks = ::Set.new
  subklass.class_attribute :after_transform_callbacks
  subklass.after_transform_callbacks = ::Set.new
end

.is_nested?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/trax/core/transformer.rb', line 39

def self.is_nested?
  !!self.try(:parent_definition)
end

.nested(*args, **options, &block) ⇒ Object



66
67
68
# File 'lib/trax/core/transformer.rb', line 66

def self.nested(*args, **options, &block)
  transformer(*args, **options, &block)
end

.nested_propertiesObject



27
28
29
# File 'lib/trax/core/transformer.rb', line 27

def self.nested_properties
  @nested_properties ||= properties.values.select{|prop| prop.is_nested? }
end

.properties_with_default_valuesObject



23
24
25
# File 'lib/trax/core/transformer.rb', line 23

def self.properties_with_default_values
  @properties_with_default_values ||= properties.values.select{ |prop| prop.try(:default) }
end

.property(_property_name, **options, &block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/trax/core/transformer.rb', line 47

def self.property(_property_name, **options, &block)
  options[:parent_definition] = self
  options[:property_name] = _property_name
  options[:with] = block if block_given?
  transformer_klass_name = "#{name}::#{_property_name.camelize}"
  transformer_klass = ::Trax::Core::NamedClass.new(transformer_klass_name, Property, **options)
  self.properties[_property_name] = transformer_klass
end

.transformer(_property_name, **options, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/trax/core/transformer.rb', line 56

def self.transformer(_property_name, **options, &block)
  options[:parent_definition] = self
  options[:property_name] = _property_name
  options[:default] = ->(){ {}.with_indifferent_access } unless options.key?(:default)
  options[:with] = block if block_given?
  transformer_klass_name = "#{name}::#{_property_name.camelize}"
  transformer_klass = ::Trax::Core::NamedClass.new(transformer_klass_name, Transformer, **options, &block)
  self.properties[_property_name] = transformer_klass
end

.transformer_propertiesObject



31
32
33
# File 'lib/trax/core/transformer.rb', line 31

def self.transformer_properties
  @transformer_properties ||= properties.values.select{|prop| prop.ancestors.include?(::Trax::Core::Transformer) }
end

.transformer_properties_with_after_transform_callbacksObject



35
36
37
# File 'lib/trax/core/transformer.rb', line 35

def self.transformer_properties_with_after_transform_callbacks
  @transformer_properties_with_after_transform_callbacks ||= transformer_properties.select{|prop| prop.after_transform_callbacks.any? }
end

Instance Method Details

#[](_property) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/trax/core/transformer.rb', line 90

def [](_property)
  if _property.include?('/')
    property_chain = _property.split('/')
    self.dig(*property_chain)
  else
    super(_property)
  end
end

#__getobj__Object



118
119
120
# File 'lib/trax/core/transformer.rb', line 118

def __getobj__
  @output
end

#has_parent?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/trax/core/transformer.rb', line 108

def has_parent?
  !!parent
end

#key?(_property) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/trax/core/transformer.rb', line 99

def key?(_property)
  if _property.include?('/')
    property_chain = _property.split('/')
    !!self.dig(*property_chain)
  else
    super(_property)
  end
end

#parent_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
# File 'lib/trax/core/transformer.rb', line 112

def parent_key?(k)
  return false unless has_parent?

  parent.key?(k)
end

#to_hashObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/trax/core/transformer.rb', line 122

def to_hash
  @to_hash ||= begin
    duplicate_hash = self.__getobj__.dup

    duplicate_hash.each_pair do |k, v|
      if v.is_a?(::Trax::Core::Transformer)
        duplicate_hash[k] = v.__getobj__
      elsif v.is_a?(Property)
        duplicate_hash[k] = v.__getobj__
      end
    end

    duplicate_hash
  end
end

#to_recursive_hashObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/trax/core/transformer.rb', line 138

def to_recursive_hash
  @to_recursive_hash ||= begin
    duplicate_hash = self.__getobj__.dup

    self.each_pair do |k, v|
      if v.is_a?(::Trax::Core::Transformer)
        duplicate_hash[k] = v.to_hash
      elsif v.is_a?(Property)
        duplicate_hash[k] = v.__getobj__
      end
    end

    duplicate_hash
  end
end