Skip to content

Commit

Permalink
No more access options object as Hash.
Browse files Browse the repository at this point in the history
Always expect it to be an object with accessor methods.
As a default (and in tests) use OpenStruct.
  • Loading branch information
nene committed Nov 26, 2013
1 parent f65e13a commit 0507abe
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 42 deletions.
3 changes: 2 additions & 1 deletion lib/jsduck/exporter/full.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'jsduck/class'
require 'jsduck/member_registry'
require 'ostruct'

module JsDuck
module Exporter

# Exporter for all the class docs.
class Full
def initialize(relations, opts={})
def initialize(relations, opts=OpenStruct.new)
# parameters are just for compatibility with other exporters
end

Expand Down
5 changes: 3 additions & 2 deletions lib/jsduck/format/doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'jsduck/inline/img'
require 'jsduck/inline/video'
require 'jsduck/inline/example'
require 'ostruct'

module JsDuck
module Format
Expand All @@ -18,7 +19,7 @@ class Doc
# Creates a formatter configured with options originating from
# command line. For the actual effect of the options see
# Inline::* classes.
def initialize(relations={}, opts={})
def initialize(relations={}, opts=OpenStruct.new)
@relations = relations
@opts = opts
@subproperties = Format::Subproperties.new(self)
Expand Down Expand Up @@ -104,7 +105,7 @@ def replace(input)
# Keep track of open HTML tags. We're not auto-detecting class
# names when inside <a>. Also we want to close down the unclosed
# tags.
tags = Format::HtmlStack.new(@opts[:ignore_html] || {}, @doc_context)
tags = Format::HtmlStack.new(@opts.ignore_html || {}, @doc_context)

while !s.eos? do
if substitute = @inline_link.replace(s)
Expand Down
4 changes: 3 additions & 1 deletion lib/jsduck/inline/example.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'ostruct'

module JsDuck
module Inline

Expand All @@ -14,7 +16,7 @@ module Inline
class Example
# Constructor takes opts parameter for consistency with other
# JsDuck::Inline::* classes.
def initialize(opts={})
def initialize(opts=OpenStruct.new)
@re = /<pre><code>\s*@example( +[^\n]*)?\s+/m
end

Expand Down
6 changes: 3 additions & 3 deletions lib/jsduck/inline/img.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'jsduck/util/html'
require 'jsduck/logger'
require 'pp'
require 'ostruct'

module JsDuck
module Inline
Expand All @@ -15,8 +15,8 @@ class Img
# Used for error reporting.
attr_accessor :doc_context

def initialize(opts={})
@tpl = opts[:img] || '<img src="%u" alt="%a" width="%w" height="%h"/>'
def initialize(opts=OpenStruct.new)
@tpl = opts.img || '<img src="%u" alt="%a" width="%w" height="%h"/>'

@re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m
end
Expand Down
5 changes: 3 additions & 2 deletions lib/jsduck/inline/link_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'jsduck/util/html'
require 'ostruct'

module JsDuck
module Inline
Expand All @@ -9,7 +10,7 @@ class LinkRenderer
# Inline::AutoLink.
attr_reader :relations

def initialize(relations={}, opts={})
def initialize(relations={}, opts=OpenStruct.new)
@relations = relations

# Template HTML that replaces {@link Class#member anchor text}.
Expand All @@ -20,7 +21,7 @@ def initialize(relations={}, opts={})
# %# - inserts "#" if member name present
# %- - inserts "-" if member name present
# %a - anchor text for link
@tpl = opts[:link] || '<a href="%c%#%m">%a</a>'
@tpl = opts.link || '<a href="%c%#%m">%a</a>'
end

# Generates HTML link to class or member applying the link
Expand Down
3 changes: 2 additions & 1 deletion lib/jsduck/inline/video.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'jsduck/util/html'
require 'jsduck/logger'
require 'ostruct'

module JsDuck
module Inline
Expand All @@ -10,7 +11,7 @@ class Video
# Used for error reporting.
attr_accessor :doc_context

def initialize(opts={})
def initialize(opts=OpenStruct.new)
@doc_context = {}

@templates = {
Expand Down
2 changes: 1 addition & 1 deletion lib/jsduck/news.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class News
# Creates News object from relations data when --import option
# specified.
def self.create(relations, doc_formatter, opts)
if opts[:import].length > 0
if opts.import.length > 0
News.new(relations, doc_formatter)
else
Util::NullObject.new(:to_html => "")
Expand Down
2 changes: 1 addition & 1 deletion lib/jsduck/options/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ def validator(name, &block)
# class every time.
def init_defaults
@defaults.each_pair do |name, value|
@opts[name] = clone(value)
@opts.send(:"#{name}=", clone(value))
end
end

Expand Down
12 changes: 0 additions & 12 deletions lib/jsduck/options/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ module Options

# Stores values of command line options.
#
# Options can be accessed using normal accessor methods or with
# Hash-like :[] and :[]= interface.
#
# All options are initially defined with an #attribute method, which
# ensures that accessing an unexisting option will result in an
# error.
Expand All @@ -24,15 +21,6 @@ def attribute(name, default=nil)
self.class.send(:attr_accessor, name)
end

# Make options object behave like hash.
# This allows us to substitute it with hash in unit tests.
def [](key)
instance_variable_get("@#{key}")
end
def []=(key, value)
instance_variable_set("@#{key}", value)
end

# Defines a validator function that gets run after all the
# options have been parsed. When validation fails, the function
# should return an error message string (or an array of string
Expand Down
6 changes: 4 additions & 2 deletions lib/jsduck/process/ext4_events.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'ostruct'

module JsDuck
module Process

Expand All @@ -7,13 +9,13 @@ module Process
# But only does so when :ext4_events option is set to true or the
# code itself is detected as being writted in Ext4 style.
class Ext4Events
def initialize(classes, opts={})
def initialize(classes, opts=OpenStruct.new)
@classes = classes
@opts = opts
end

def process_all!
if @opts[:ext4_events] == true || (@opts[:ext4_events] == nil && ext4_style_code?)
if @opts.ext4_events == true || (@opts.ext4_events == nil && ext4_style_code?)
@classes.each_value {|cls| process(cls) }
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/jsduck/process/overrides.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'jsduck/logger'
require 'ostruct'

module JsDuck
module Process

class Overrides
def initialize(classes_hash, opts = {:external_classes => []})
def initialize(classes_hash, opts = OpenStruct.new(:external_classes => []))
@classes_hash = classes_hash
@opts = opts
end
Expand All @@ -28,7 +29,7 @@ def process_all!
@classes_hash.delete(cls[:name])
end

@opts[:external_classes] += overrides.map {|c| c[:name] }
@opts.external_classes += overrides.map {|c| c[:name] }
end

private
Expand Down
11 changes: 6 additions & 5 deletions lib/jsduck/process/versions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'jsduck/process/importer'
require 'ostruct'

module JsDuck
module Process
Expand All @@ -7,16 +8,16 @@ module Process
# older versions of the same project and looking in which version
# a class or method first appeared.
class Versions
def initialize(relations, opts={}, importer=Process::Importer.new)
def initialize(relations, opts=OpenStruct.new, importer=Process::Importer.new)
@relations = relations
@opts = opts
@importer = importer
end

# Loads in exported docs and generates @since and @new tags.
def process_all!
if @opts[:import].length > 0
@versions = @importer.import(@opts[:import])
if @opts.import.length > 0
@versions = @importer.import(@opts.import)
add_since_tags
end
end
Expand Down Expand Up @@ -117,9 +118,9 @@ def is_new?(version_nr)
def new_versions_map
new_versions = {}

if @opts[:new_since]
if @opts.new_since
@versions.map {|v| v[:version] }.each do |v|
if v == @opts[:new_since] || !new_versions.empty?
if v == @opts.new_since || !new_versions.empty?
new_versions[v] = true
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/jsduck/tag/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def initialize
#
# NOTE: This method is explicitly called from JsDuck::Options class.
def init_tooltip!(opts)
if opts[:new_since]
@signature[:tooltip] = "New since #{opts[:new_since]}"
elsif opts[:import].length > 0
@signature[:tooltip] = "New since #{opts[:import].last[:version]}"
if opts.new_since
@signature[:tooltip] = "New since #{opts.new_since}"
elsif opts.import.length > 0
@signature[:tooltip] = "New since #{opts.import.last[:version]}"
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/format_doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "jsduck/format/doc"
require "jsduck/relations"
require "jsduck/class"
require "ostruct"

describe JsDuck::Format::Doc do

Expand Down Expand Up @@ -37,7 +38,7 @@ def get(filename)
:alternateClassNames => ["FooBar"]
}),
])
@formatter = JsDuck::Format::Doc.new(relations, :img => '<img src="%u" alt="%a"/>')
@formatter = JsDuck::Format::Doc.new(relations, OpenStruct.new(:img => '<img src="%u" alt="%a"/>'))
@formatter.class_context = "Context"
@formatter.images = ImageDirMock.new
end
Expand Down Expand Up @@ -187,7 +188,7 @@ def get(filename)
]
}),
])
@formatter = JsDuck::Format::Doc.new(relations, :img => '<img src="%u" alt="%a"/>')
@formatter = JsDuck::Format::Doc.new(relations, OpenStruct.new(:img => '<img src="%u" alt="%a"/>'))
@formatter.class_context = "Context"
@formatter.images = ImageDirMock.new
end
Expand Down
9 changes: 6 additions & 3 deletions spec/versions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "jsduck/process/versions"
require "jsduck/util/null_object"
require "jsduck/class"
require "ostruct"

describe JsDuck::Process::Versions do

Expand Down Expand Up @@ -49,7 +50,8 @@ def current_version
{:name => "ExplicitNewClass", :new => true, :alternateClassNames => []},
].map {|cfg| JsDuck::Class.new(cfg) }

JsDuck::Process::Versions.new(@relations, {:import => @versions}, importer).process_all!
opts = OpenStruct.new(:import => @versions)
JsDuck::Process::Versions.new(@relations, opts, importer).process_all!

# build className/member index for easy lookup in specs
@stuff = {}
Expand Down Expand Up @@ -175,7 +177,8 @@ def current_version
{:name => "NewClass", :alternateClassNames => []},
].map {|cfg| JsDuck::Class.new(cfg) }

JsDuck::Process::Versions.new(@relations, {:import => @versions, :new_since => "2.0"}, importer).process_all!
opts = OpenStruct.new(:import => @versions, :new_since => "2.0")
JsDuck::Process::Versions.new(@relations, opts, importer).process_all!
end

# @since
Expand Down Expand Up @@ -225,7 +228,7 @@ def current_version
]},
].map {|cfg| JsDuck::Class.new(cfg) }

opts = {:import => versions, :new_since => "3.0"}
opts = OpenStruct.new(:import => versions, :new_since => "3.0")
JsDuck::Process::Versions.new(relations, opts, importer).process_all!

relations
Expand Down

0 comments on commit 0507abe

Please sign in to comment.