Class: ForestAdminAgent::Utils::ConditionTreeParser
- Inherits:
-
Object
- Object
- ForestAdminAgent::Utils::ConditionTreeParser
- Includes:
- Http::Exceptions, ForestAdminDatasourceToolkit::Components::Query::ConditionTree, ForestAdminDatasourceToolkit::Components::Query::ConditionTree::Nodes, ForestAdminDatasourceToolkit::Utils
- Defined in:
- lib/forest_admin_agent/utils/condition_tree_parser.rb
Class Method Summary collapse
- .branch?(filters) ⇒ Boolean
- .cast_to_type(value, expected_type) ⇒ Object
- .from_plain_object(collection, filters) ⇒ Object
- .get_expected_type_for_condition(leaf, schema) ⇒ Object
- .leaf?(filters) ⇒ Boolean
- .parse_value(collection, leaf) ⇒ Object
Class Method Details
.branch?(filters) ⇒ Boolean
86 87 88 |
# File 'lib/forest_admin_agent/utils/condition_tree_parser.rb', line 86 def self.branch?(filters) filters.key?(:aggregator) && filters.key?(:conditions) end |
.cast_to_type(value, expected_type) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/forest_admin_agent/utils/condition_tree_parser.rb', line 58 def self.cast_to_type(value, expected_type) return value if value.nil? if expected_type.is_a?(Array) items = value.is_a?(String) ? value.split(',').map(&:strip) : value filter_fn = expected_type[0] == 'Number' ? ->(item) { item.is_a?(Numeric) } : ->(_) { true } return value unless items.is_a?(Array) return items.map { |item| cast_to_type(item, expected_type[0]) }.select(&filter_fn) end case expected_type when 'String', 'Dateonly', 'Date' value.to_s when 'Number' value.to_f when 'Boolean' !%w[false 0 no].include?(value.to_s) else value end end |
.from_plain_object(collection, filters) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/forest_admin_agent/utils/condition_tree_parser.rb', line 12 def self.from_plain_object(collection, filters) if leaf?(filters) operator = filters[:operator].titleize.tr(' ', '_').downcase value = parse_value(collection, filters.merge(operator: operator)) return ConditionTreeLeaf.new(filters[:field], operator, value) end if branch?(filters) aggregator = filters[:aggregator].capitalize conditions = filters[:conditions].map do |sub_tree| from_plain_object(collection, sub_tree) end return conditions.size == 1 ? conditions[0] : ConditionTreeBranch.new(aggregator, conditions) end raise BadRequestError, 'Failed to instantiate condition tree' end |
.get_expected_type_for_condition(leaf, schema) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/forest_admin_agent/utils/condition_tree_parser.rb', line 39 def self.get_expected_type_for_condition(leaf, schema) operators_expecting_number = [ Operators::SHORTER_THAN, Operators::LONGER_THAN, Operators::AFTER_X_HOURS_AGO, Operators::BEFORE_X_HOURS_AGO, Operators::PREVIOUS_X_DAYS, Operators::PREVIOUS_X_DAYS_TO_DATE ] return 'Number' if operators_expecting_number.include?(leaf[:operator]) if [Operators::IN, Operators::NOT_IN, Operators::INCLUDES_ALL].include?(leaf[:operator]) return [schema.column_type] end schema.column_type end |
.leaf?(filters) ⇒ Boolean
82 83 84 |
# File 'lib/forest_admin_agent/utils/condition_tree_parser.rb', line 82 def self.leaf?(filters) filters.key?(:field) && filters.key?(:operator) end |
.parse_value(collection, leaf) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/forest_admin_agent/utils/condition_tree_parser.rb', line 32 def self.parse_value(collection, leaf) schema = Collection.get_field_schema(collection, leaf[:field]) expected_type = get_expected_type_for_condition(leaf, schema) cast_to_type(leaf[:value], expected_type) end |