Class: JsDuck::Accessors
- Inherits:
-
Object
- Object
- JsDuck::Accessors
- Defined in:
- lib/jsduck/accessors.rb
Instance Method Summary collapse
- #add_shared(hash, cfg) ⇒ Object
- #build_lookup_table(members, tagname) ⇒ Object
-
#clone_meta(cfg) ⇒ Object
Create copy of all meta attributes of config, except the :required which only applies to configs and must not be propagated to methods or events.
-
#create(cls) ⇒ Object
Given a class, generates accessor methods to configs with When class already contains a getter or setter, the method is not added.
- #create_event(cfg) ⇒ Object
- #create_getter(cfg) ⇒ Object
- #create_setter(cfg) ⇒ Object
- #upcase_first(str) ⇒ Object
Instance Method Details
#add_shared(hash, cfg) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/jsduck/accessors.rb', line 110 def add_shared(hash, cfg) hash.merge!({ :owner => cfg[:owner], :files => cfg[:files], :private => cfg[:private], :autodetected => cfg[:autodetected], :meta => (cfg), }) end |
#build_lookup_table(members, tagname) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/jsduck/accessors.rb', line 40 def build_lookup_table(members, tagname) map = {} members.each do |m| map[m[:name]] = m if m[:tagname] == tagname end map end |
#clone_meta(cfg) ⇒ Object
Create copy of all meta attributes of config, except the :required which only applies to configs and must not be propagated to methods or events.
127 128 129 130 131 132 133 |
# File 'lib/jsduck/accessors.rb', line 127 def (cfg) h = {} cfg[:meta].each_pair do |key, value| h[key] = value unless key == :required end h end |
#create(cls) ⇒ Object
Given a class, generates accessor methods to configs with When class already contains a getter or setter, the method is not added.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jsduck/accessors.rb', line 10 def create(cls) # Grab all configs tagged as @accessor accessors = cls[:members].find_all {|m| m[:tagname] == :cfg && m[:accessor] } # Build lookup tables of method and event names methods = build_lookup_table(cls[:members], :method) events = build_lookup_table(cls[:members], :event) accessors.each do |cfg| # add getter if no method with same name exists get = create_getter(cfg) if !methods[get[:name]] cls[:members] << get end # add setter if no method with same name exists set = create_setter(cfg) if !methods[set[:name]] cls[:members] << set end # for evented accessors if cfg[:evented] # add event if no event with same name exists ev = create_event(cfg) if !events[ev[:name]] cls[:members] << ev end end end end |
#create_event(cfg) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/jsduck/accessors.rb', line 82 def create_event(cfg) name = cfg[:name].downcase + "change" setter_name = "set" + upcase_first(cfg[:name]); return add_shared({ :tagname => :event, :name => name, :doc => "Fires when the {@link ##{cfg[:id]}} configuration is changed by {@link #method-#{setter_name}}.", :params => [ { :name => "this", :type => cfg[:owner], :doc => "The #{cfg[:owner]} instance." }, { :name => "value", :type => cfg[:type], :doc => "The new value being set." }, { :name => "oldValue", :type => cfg[:type], :doc => "The existing value." }, ], :id => "event-" + name, }, cfg) end |
#create_getter(cfg) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jsduck/accessors.rb', line 48 def create_getter(cfg) name = "get" + upcase_first(cfg[:name]) return add_shared({ :tagname => :method, :name => name, :doc => "Returns the value of {@link #cfg-#{cfg[:name]}}.", :params => [], :return => { :type => cfg[:type], :doc => "", }, :id => "method-" + name, }, cfg) end |
#create_setter(cfg) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jsduck/accessors.rb', line 63 def create_setter(cfg) name = "set" + upcase_first(cfg[:name]); return add_shared({ :tagname => :method, :name => name, :doc => "Sets the value of {@link #cfg-#{cfg[:name]}}.", :params => [{ :type => cfg[:type], :name => cfg[:name], :doc => "", }], :return => { :type => "undefined", :doc => "", }, :id => "method-" + name, }, cfg) end |
#upcase_first(str) ⇒ Object
120 121 122 |
# File 'lib/jsduck/accessors.rb', line 120 def upcase_first(str) str[0,1].upcase + str[1..-1] end |