Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespjh committed Mar 18, 2011
0 parents commit b072e00
Show file tree
Hide file tree
Showing 33 changed files with 814 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -0,0 +1,4 @@
= Changelog

== 0.1.0
Abstracted from DA prototype, with Calculation torn apart into three classes.
19 changes: 19 additions & 0 deletions COPYING
@@ -0,0 +1,19 @@
Copyright (c) 2011 AMEE UK Ltd (help@amee.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions README
@@ -0,0 +1 @@
== TBD
31 changes: 31 additions & 0 deletions Rakefile
@@ -0,0 +1,31 @@
require 'rake'
require 'spec'
require 'spec/rake/spectask'
require 'rake/rdoctask'
require 'rubygems/specification'
require 'rake/gempackagetask'

task :default => [:spec]

Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "spec/spec.opts"]
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec,/usr,/Library']
end

Rake::RDocTask.new { |rdoc|
rdoc.rdoc_dir = 'doc'
rdoc.title = "AMEE-Data-Abstraction - RDoc documentation"
rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
rdoc.options << '--charset' << 'utf-8'
rdoc.rdoc_files.include('README', 'COPYING')
rdoc.rdoc_files.include('lib/**/*.rb')
}

# Gem build task - load gemspec from file
gemspec = File.read('amee-data-abstraction.gemspec')
spec = eval("#{gemspec}")
Rake::GemPackageTask.new(spec) do |p|
p.gem_spec = spec
end
22 changes: 22 additions & 0 deletions amee-data-abstraction.gemspec
@@ -0,0 +1,22 @@
require './lib/amee-data-abstraction/version.rb'
require 'rake'

Gem::Specification.new do |s|
s.name = "amee"
s.version = AMEE::DataAbstraction::VERSION::STRING
s.date = "2010-11-08"
s.summary = "Calculation and form building tool hiding details of AMEE API"
s.email = "help@amee.com"
s.homepage = "http://github.com/AMEE/data-abstraction"
s.has_rdoc = true
s.authors = ["James Smith"]
s.files = ["README", "COPYING", "CHANGELOG"]
s.files += FileList.new('lib/**/*.rb')
s.files += ['init.rb', 'rails/init.rb']
s.files += [] #bin/executable
s.files += FileList.new('examples/**/*.rb')
s.files += ['init.rb', 'rails/init.rb']
s.executables = []
s.add_dependency("amee", ">= 2.6.0", "< 3.0")
s.add_dependency("amee-internal", ">= 2.6.0", "< 3.0")
end
27 changes: 27 additions & 0 deletions examples/_calculator_form.erb
@@ -0,0 +1,27 @@
<% form_id = "drill-form-#{rand(1e8)}" %>
<% form_tag({:action=>nil},{:id=>form_id}) do%>
<%= hidden_field_tag 'form_id', form_id %>
<table>
<%@calculation.drills.each_value do |input|%>
<tr>
<td><%=label :entry,input.label,input.name%></td>
<td>
<%= select_tag "entry[#{input.label}]",
options_for_select(input.options_for_select,input.set? ? input.value : nil),:disabled=>input.disabled?%>
</td>
</tr>
<%end%>
<%@calculation.profiles.each_value do |input|%>
<tr><td><%=label :entry,input.label,input.name%></td><td><%= text_field :entry, input.label, "size" => 20, :value => input.value_if_given %></td></tr>
<%end%>
<%@calculation.chosen_outputs.each_value do |result|%>
<tr><td><%=label :entry,result.label,result.name%></td><td><%= result.value_if_given %></td></tr>
<%end%>
</table>
<%end%>
<%= observe_form form_id, :url => {:action => 'result'}%>
<% javascript_tag do %>
$('#<%=form_id%>').submit(function() {
return false;
});
<%end%>
16 changes: 16 additions & 0 deletions examples/calculation_controller.rb
@@ -0,0 +1,16 @@
class CalculationController < ApplicationController
def index
@calculations=Calculations.calculations.values
end

def enter
@calculation=Calculations[params[:calculation]]
end

def result
@calculation=Calculations[params[:calculation]].clone
@calculation.choose!(params['entry'])
@calculation.calculate!
end

end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
require File.dirname(__FILE__) + "/rails/init"
18 changes: 18 additions & 0 deletions lib/amee-data-abstraction.rb
@@ -0,0 +1,18 @@
require 'amee'
require 'amee-internal'

require File.dirname(__FILE__) + '/core-extensions/class'
require File.dirname(__FILE__) + '/core-extensions/ordered_hash'
require File.dirname(__FILE__) + '/amee-data-abstraction/calculation'
require File.dirname(__FILE__) + '/amee-data-abstraction/ongoing_calculation'
require File.dirname(__FILE__) + '/amee-data-abstraction/prototype_calculation'
require File.dirname(__FILE__) + '/amee-data-abstraction/calculation_set'
require File.dirname(__FILE__) + '/amee-data-abstraction/term'
require File.dirname(__FILE__) + '/amee-data-abstraction/input'
require File.dirname(__FILE__) + '/amee-data-abstraction/drill'
require File.dirname(__FILE__) + '/amee-data-abstraction/profile'
require File.dirname(__FILE__) + '/amee-data-abstraction/output'

module AMEE::DataAbstraction
mattr_accessor :connection
end
67 changes: 67 additions & 0 deletions lib/amee-data-abstraction/calculation.rb
@@ -0,0 +1,67 @@
module AMEE
module DataAbstraction
class Calculation

public

attr_property :label,:name,:path

def [](sym)
@terms[sym.to_sym]
end

def inputs
terms AMEE::DataAbstraction::Input
end


def outputs
terms AMEE::DataAbstraction::Output
end

def inspect
"#{label} : [#{terms.values.map{|x| x.inspect}.join(',')}]"
end

def initialize_copy(source)
super
@terms=ActiveSupport::OrderedHash.new
source.terms.each do |k,v|
@terms[k]=v.clone
@terms[k].parent=self
end
end

def terms(klass=nil)
return @terms unless klass
ActiveSupport::OrderedHash[@terms.stable_select{|k,v| v.is_a? klass}]
end

protected

def initialize
@terms=ActiveSupport::OrderedHash.new
end

private

def by_path(path)
@terms.values.detect { |v| v.path==path }
end

def drill_by_path(path)
drills.values.detect { |v| v.path==path }
end

def profiles
terms AMEE::DataAbstraction::Profile
end

def drills
terms AMEE::DataAbstraction::Drill
end


end
end
end
18 changes: 18 additions & 0 deletions lib/amee-data-abstraction/calculation_set.rb
@@ -0,0 +1,18 @@
module AMEE
module DataAbstraction
class CalculationSet
def initialize(options={},&block)
@calculations={}
instance_eval(&block) if block
end
attr_accessor :calculations
def [](sym)
@calculations[sym.to_sym]
end
def calculation(options={},&block)
new_content=PrototypeCalculation.new(options,&block)
@calculations[new_content.label]=new_content
end
end
end
end
24 changes: 24 additions & 0 deletions lib/amee-data-abstraction/drill.rb
@@ -0,0 +1,24 @@
module AMEE
module DataAbstraction
class Drill < Input
public

def choices
c=calculation_with_only_earlier.send(:amee_drill).choices
c.length==1 ? [value] : c #Intention is to get autodrilled, drill will result in a UID
end

def valid_choice?
(choices.include? value) && (choices.length > 1)
end

def options_for_select
[nil]+choices
end

private

end
end
end

46 changes: 46 additions & 0 deletions lib/amee-data-abstraction/input.rb
@@ -0,0 +1,46 @@
module AMEE
module DataAbstraction
class Input < Term
def choices
raise NotImplementedError
end

def options_for_select
raise NotImplementedError
end


#DSL-----
def fixed val
value val
@fixed=true
end
#------

def fixed?
@fixed
end

def disabled?
fixed? || (!set? && !next?)
end

def next?
label==unset_siblings.values.first.label
end

protected

def valid_choice?
raise NotImplementedError
end

def calculation_with_only_earlier
res=parent.clone
res.retreat!(self.label)
return res
end

end
end
end

0 comments on commit b072e00

Please sign in to comment.