Skip to content

Commit

Permalink
Merge pull request #77 from tomgi/deep_merge
Browse files Browse the repository at this point in the history
it should allow to build options step by step without overriding previously set values
  • Loading branch information
michelson committed Oct 21, 2012
2 parents 18990d0 + a8e142e commit edf35ce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ Overriding entire option:
#.....
````

If you want to use this syntax and still be able to build option step-by-step without overriding:

````
@h = LazyHighCharts::HighChart.new('graph') do |f|
#.....
f.xAxis!(:categories => @days.reverse! , :labels=>{:rotation=>-45 , :align => 'right'})
f.chart!({:defaultSeriesType=>"spline" , :renderTo => "myRenderArea" , :inverted => true})
#.....
````

Using the datetime axis type:

````
Expand Down
1 change: 1 addition & 0 deletions lazy_high_charts.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = "~> 1.3"

s.add_dependency "bundler", "~> 1.0"
s.add_dependency "hash-deep-merge"

s.description = <<-DESC
lazy_high_charts is a Rails 3.x gem for displaying Highcharts graphs.
Expand Down
10 changes: 9 additions & 1 deletion lib/lazy_high_charts/high_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def defaults_options
#
# For instance: <tt>high_chart.grid(:color => "#699")</tt>
def method_missing(meth, opts = {})
merge_options meth, opts
if meth.to_s.end_with? '!'
deep_merge_options meth[0..-2].to_sym, opts
else
merge_options meth, opts
end
end

# Add a simple series to the graph:
Expand All @@ -62,6 +66,10 @@ def merge_options(name, opts)
@options.merge! name => opts
end

def deep_merge_options(name, opts)
@options.deep_merge! name => opts
end

def arguments_to_options(args)
if args.blank?
{:show => true}
Expand Down
18 changes: 18 additions & 0 deletions spec/high_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@
end
chart.options[:subtitle][:text].should == "Bar"
end

it 'should override entire option by default when resetting it again' do
chart = LazyHighCharts::HighChart.new('graph') do |f|
f.xAxis(categories: [3, 5, 7])
f.xAxis(title: {text: 'x title'})
end
chart.options[:xAxis][:categories].should == nil
chart.options[:xAxis][:title][:text].should == 'x title'
end

it 'should allow to build options step by step without overriding previously set values' do
chart = LazyHighCharts::HighChart.new('graph') do |f|
f.xAxis!(categories: [3, 5, 7])
f.xAxis!(title: {text: 'x title'})
end
chart.options[:xAxis][:categories].should == [3, 5, 7]
chart.options[:xAxis][:title][:text].should == 'x title'
end

end

Expand Down

0 comments on commit edf35ce

Please sign in to comment.