Class: XDry::Generators::ConstructorFromField

Inherits:
Generator
  • Object
show all
Defined in:
lib/xdry/generators/ctor_from_field.rb

Instance Attribute Summary

Attributes inherited from Generator

#patcher

Instance Method Summary collapse

Methods inherited from Generator

id, inherited, #initialize, #process_attribute, #verbose?

Constructor Details

This class inherits a constructor from XDry::Generators::Generator

Instance Method Details

#impl_selector_def_for(attributes) ⇒ Object



70
71
72
# File 'lib/xdry/generators/ctor_from_field.rb', line 70

def impl_selector_def_for attributes
  CompoundSelectorDef.new(attributes.each_with_index.collect { |a, i| selector_component_for(a, i, true) })
end

#intf_selector_def_for(attributes) ⇒ Object



74
75
76
# File 'lib/xdry/generators/ctor_from_field.rb', line 74

def intf_selector_def_for attributes
  CompoundSelectorDef.new(attributes.each_with_index.collect { |a, i| selector_component_for(a, i, false) })
end

#patch_implementation!(oclass, attributes) ⇒ Object



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
# File 'lib/xdry/generators/ctor_from_field.rb', line 16

def patch_implementation! oclass, attributes
  impl_selector_def = impl_selector_def_for(attributes)

  init_code = Emitter.capture do |o|
    o.method "(id)#{impl_selector_def}" do
      o.if "self = [super init]" do
      end
      o << "return self;"
    end
  end

  MethodPatcher.new(patcher, oclass, impl_selector_def.selector, ImplementationStartIP.new(oclass), init_code) do |omethod|
    impl = omethod.impl
    ip = InsideConstructorIfSuperIP.new(impl)

    lines = Emitter.capture do |o|
      attributes.zip(impl_selector_def.components).each do |oattr, selector_component|
        name, type = oattr.name, oattr.type
        capitalized_name = name.capitalized_identifier

        retain_policy = Boxing.retain_policy_of type

        unless impl.children.any? { |n| NLine === n && n.line =~ /^(?:self\s*.\s*#{oattr.name}|#{oattr.field_name})\s*=/ }

          var_name = impl.start_node.selector_def.var_name_after_keyword(selector_component.keyword)

          field_ref = oattr.field_name
          field_ref = "self->#{field_ref}" if field_ref == var_name

          retained = retain_policy.retain(var_name)
          o << "#{field_ref} = #{retained};"
        end
      end
    end

    ip.insert @patcher, lines unless lines.empty?
  end
end

#patch_interface!(oclass, attributes) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xdry/generators/ctor_from_field.rb', line 55

def patch_interface! oclass, attributes
  intf_selector_def = intf_selector_def_for(attributes)

  omethod = oclass.find_method(intf_selector_def.selector)
  unless omethod && omethod.has_header?

    ip = BeforeInterfaceEndIP.new(oclass)
    lines = Emitter.capture do |o|
      o << "- (id)#{intf_selector_def};"
    end
    ip.insert patcher, [""] + lines + [""]

  end
end

#process_class(oclass) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/xdry/generators/ctor_from_field.rb', line 8

def process_class oclass
  attributes = oclass.attributes.select { |a| a.wants_constructor? }
  return if attributes.empty?

  patch_implementation! oclass, attributes
  patch_interface! oclass, attributes
end

#selector_component_for(attribute, index, is_impl) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/xdry/generators/ctor_from_field.rb', line 78

def selector_component_for attribute, index, is_impl
  keyword = attribute.name
  keyword = "initWith#{keyword.capitalized_identifier}" if index == 0

  arg_name = attribute.name
  arg_name = arg_name.prefixed_as_arg_name if is_impl && arg_name == attribute.field_name

  SelectorComponent.new("#{keyword}:", arg_name, attribute.type)
end