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) ⇒ 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
108 109 110 111 112 113 114 115 |
# File 'lib/jsduck/accessors.rb', line 108 def add_shared(hash, cfg) hash.merge!({ :owner => cfg[:owner], :files => cfg[:files], :private => cfg[:private], :meta => (cfg), }) end |
#build_lookup_table(members) ⇒ Object
40 41 42 43 44 |
# File 'lib/jsduck/accessors.rb', line 40 def build_lookup_table(members) map = {} members.each {|m| map[m[:name]] = m } 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.
124 125 126 127 128 129 130 |
# File 'lib/jsduck/accessors.rb', line 124 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][:cfg].find_all {|cfg| cfg[: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][:method] << get end # add setter if no method with same name exists set = create_setter(cfg) if !methods[set[:name]] cls[:members][:method] << 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][:event] << ev end end end end |
#create_event(cfg) ⇒ Object
80 81 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 |
# File 'lib/jsduck/accessors.rb', line 80 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jsduck/accessors.rb', line 46 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jsduck/accessors.rb', line 61 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
117 118 119 |
# File 'lib/jsduck/accessors.rb', line 117 def upcase_first(str) str[0,1].upcase + str[1..-1] end |