Module: Ironfan::Plugin::Base::ClassMethods

Defined in:
lib/ironfan/plugin/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cookbook_reqsObject (readonly)

Returns the value of attribute cookbook_reqs


49
50
51
# File 'lib/ironfan/plugin/base.rb', line 49

def cookbook_reqs
  @cookbook_reqs
end

#plugin_nameObject (readonly)

Returns the value of attribute plugin_name


50
51
52
# File 'lib/ironfan/plugin/base.rb', line 50

def plugin_name
  @plugin_name
end

Instance Method Details

#from_node(node = NilCheckDelegate.new(nil)) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ironfan/plugin/base.rb', line 52

def from_node(node = NilCheckDelegate.new(nil))
  new(Hash[
           fields.select{|_,x| x.node_attr}.map do |_,x|
             [x.name, (x.node_attr.split('.').inject(node) do |hsh,attr|
                         if hsh
                           (val = hsh[attr]).is_a?(Mash) ? val.to_hash : val
                         end
                       end)]
           end.reject{|_,v| v.nil?}
          ])
end

#register_with(cls, &blk) ⇒ Object


64
65
66
# File 'lib/ironfan/plugin/base.rb', line 64

def register_with cls, &blk
  (@dest_class = cls).class_eval{ extend Ironfan::Pluggable }
end

#template(plugin_name_parts, base_class = self, &blk) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ironfan/plugin/base.rb', line 68

def template plugin_name_parts, base_class=self, &blk
  plugin_name_parts = [*plugin_name_parts]
  full_name = plugin_name_parts.map(&:to_s).join('_').to_sym
  plugin_name = plugin_name_parts.first.to_sym

  Class.new(base_class, &blk).tap do |plugin_class|
    plugin_class.class_eval{ @plugin_name = plugin_name }

    self.const_set(full_name.to_s.camelize.to_sym, plugin_class)

    @dest_class.class_eval do
      add_plugin(full_name, plugin_class)
      define_method(full_name) do |*args, &blk|
        plugin_class.plugin_hook self, (args.first || {}), plugin_name, full_name, &blk
      end
    end
  end
end