Class: MontageRails::ApplicationResource
- Inherits:
-
Object
- Object
- MontageRails::ApplicationResource
- Defined in:
- lib/montage_rails/application_resource.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
- #class_to_filename ⇒ Object
- #execute_filters ⇒ Object
- #find(id) ⇒ Object
- #query(params) ⇒ Object
- #read_yaml ⇒ Object
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/montage_rails/application_resource.rb', line 4 def data @data end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/montage_rails/application_resource.rb', line 4 def params @params end |
Class Method Details
.fetch_item ⇒ Object
6 7 8 |
# File 'lib/montage_rails/application_resource.rb', line 6 def self.fetch_item self.new.read_yaml.first end |
Instance Method Details
#class_to_filename ⇒ Object
14 15 16 17 |
# File 'lib/montage_rails/application_resource.rb', line 14 def class_to_filename File.join(Rails.root, "test", "montage_resources", "test_data", "#{self.class.to_s.underscore.sub("_resource","")}_test_data.yml") end |
#execute_filters ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/montage_rails/application_resource.rb', line 30 def execute_filters return unless @params["$query"] && @params["$schema"] @params["$query"][0][1].each do |filter| key = filter[0] operator = filter[1].is_a?(Array) ? filter[1][0] : filter[1] case operator when "ieq" #case insensitve equality when "not" #not operator, field != value when "contains" # value in field when "icontains" # case insensitive version of contains when "$in" # field in value @data = @data.select { |item| filter[1][1].include?(item[key]) } when "notin" # field not in value when "$gt" # field > value @data = @data.select{ |item| item[key] > filter[1][1] } when "$gte" # field >= value @data = @data.select{ |item| item[key] >= filter[1][1] } when "$lt" # field < value @data = @data.select{ |item| item[key] < filter[1][1] } when "$lte" # field <= value @data = @data.select{ |item| item[key] <= filter[1][1] } when "startswith" # field.startswith(value) when "istartswith" # case-insensitive starts with when "endswith" # field.endsswith(value) when "iendswith" # case-insensitvie endswith else #default is the 'equal to' operator @data = @data.select{ |item| filter[1] === item[key] } end end end |
#find(id) ⇒ Object
19 20 21 |
# File 'lib/montage_rails/application_resource.rb', line 19 def find(id) read_yaml.select{|item| item["id"] == id}.first end |
#query(params) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/montage_rails/application_resource.rb', line 23 def query(params) @data = self.read_yaml @params = params execute_filters @data end |
#read_yaml ⇒ Object
10 11 12 |
# File 'lib/montage_rails/application_resource.rb', line 10 def read_yaml YAML.load_file(class_to_filename) end |