Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started working on a operation syntax builder.
- Loading branch information
Doug Youch
committed
May 24, 2010
1 parent
5bca639
commit 51eae04
Showing
8 changed files
with
158 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
class UserSegment::OperationBuilder < HashModel | ||
attributes :operator => nil, :field => 'created', :operation => 'since', :arguments => [] | ||
|
||
def operator_options | ||
[['Not', 'not'], ['', nil]] | ||
end | ||
|
||
def build(opts={}) | ||
self.operator = opts[:operator] | ||
self.field = opts[:field] | ||
self.operation = opts[:operation] | ||
unless self.operation_options.rassoc(self.operation) | ||
self.operation = self.operation_options[0][1] | ||
@user_segment_field = nil | ||
end | ||
|
||
self.operation_arguments.each_with_index do |type, idx| | ||
arg = "argument#{idx}" | ||
self.send("#{arg}=", opts[arg.to_sym]) if opts[arg.to_sym] | ||
end | ||
end | ||
|
||
def field_options | ||
return @field_options if @field_options | ||
@field_options = [] | ||
UserSegment::FieldHandler.handlers.each do |handler| | ||
handler[:class].user_segment_fields.each do |field, values| | ||
@field_options << [values[:name], field.to_s] unless @field_options.rassoc(field.to_s) | ||
end | ||
end | ||
@field_options.sort! { |a, b| a[0] <=> b[0] } | ||
end | ||
|
||
def user_segment_field | ||
return @user_segment_field if @user_segment_field | ||
@user_segment_field = UserSegment::Field.new :field => self.field, :operation => self.operation, :arguments => self.arguments | ||
@user_segment_field | ||
end | ||
|
||
def operation_options | ||
return @operation_options if @operation_options | ||
@operation_options = [] | ||
|
||
if self.user_segment_field.type_class | ||
@operation_options = self.user_segment_field.type_class.user_segment_field_type_operations.collect do |operation, values| | ||
[values[:name], operation.to_s] | ||
end | ||
end | ||
|
||
@operation_options.sort! { |a, b| a[0] <=> b[0] } | ||
end | ||
|
||
def operation_arguments | ||
self.user_segment_field.operation_arguments || [] | ||
end | ||
|
||
def operation_argument_names | ||
self.user_segment_field.operation_argument_names | ||
end | ||
|
||
def operation_argument_options | ||
self.user_segment_field.operation_argument_options | ||
end | ||
|
||
def convert_to(value, idx) | ||
UserSegment::FieldType.convert_to(value, self.operation_arguments[idx], self.operation_argument_options[idx]) | ||
end | ||
|
||
def method_missing(arg, *args) | ||
arg = arg.to_s | ||
if arg =~ /^argument(\d+)$/ | ||
self.arguments[$1.to_i] | ||
elsif arg =~ /^argument(\d+)=$/ | ||
self.arguments[$1.to_i] = self.convert_to(args[0], $1.to_i) if $1.to_i < self.operation_arguments.length | ||
else | ||
super | ||
end | ||
end | ||
|
||
def to_expr | ||
return '' unless self.user_segment_field.valid? | ||
output = self.operator == 'not' ? 'not ' : '' | ||
output += self.user_segment_field.to_expr | ||
output | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
<div id="operation_builder"> | ||
<% admin_form_for :builder, @builder do |f| -%> | ||
<%= f.check_boxes :operator, [['not', 'not']], :single => true, :label => 'I am' %> | ||
<%= f.select :field, @builder.field_options, :label => 'looking for' %> | ||
<%= f.select :operation, @builder.operation_options %> | ||
<% @builder.operation_arguments.each_with_index do |arg, idx| -%> | ||
<% arg_options = @builder.operation_argument_options[idx] | ||
label = @builder.operation_argument_names[idx] | ||
if arg_options[:options] -%> | ||
<%= f.select "argument#{idx}", arg_options[:options], :label => label %> | ||
<% else -%> | ||
<%= f.text_field "argument#{idx}", :label => label %> | ||
<% end -%> | ||
<% end -%> | ||
<%= f.submit 'Build' %> | ||
<% end -%> | ||
|
||
<div class="operation"> | ||
<span>Expression:</span><br/> | ||
<textarea style="width:400px; height: 50px;" readonly> | ||
<%= @builder.to_expr %> | ||
</textarea> | ||
</div> | ||
|
||
</div> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
<%= f.spacer %> | ||
<%= f.cancel_submit_buttons 'Cancel', (@segment.id ? 'Save' : 'Create') %> | ||
<% end -%> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= render :partial => 'operation_form' %> | ||
|