Class: ActivePublicResources::RequestCriteria
- Inherits:
-
Object
- Object
- ActivePublicResources::RequestCriteria
- Includes:
- ActiveModel::Serialization
- Defined in:
- lib/active_public_resources/request_criteria.rb
Constant Summary collapse
- SORTS =
[ SORT_RELEVANCE = 'relevance', SORT_RECENT = 'recent', SORT_POPULAR = 'popular' ]
- CONTENT_FILTERS =
[ CONTENT_FILTER_NONE = 'none', CONTENT_FILTER_STRICT = 'strict' ]
Instance Attribute Summary collapse
-
#channel_name ⇒ Object
Returns the value of attribute channel_name.
-
#content_filter ⇒ Object
Returns the value of attribute content_filter.
-
#folder ⇒ Object
Returns the value of attribute folder.
-
#page ⇒ Object
Returns the value of attribute page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#query ⇒ Object
Returns the value of attribute query.
-
#remote_ip ⇒ Object
Returns the value of attribute remote_ip.
-
#sort ⇒ Object
Returns the value of attribute sort.
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(args = {}) ⇒ RequestCriteria
constructor
A new instance of RequestCriteria.
- #set_default_criteria!(default_criteria) ⇒ Object
- #validate_presence(attr_name, *attr_names) ⇒ Object
- #validate_presence!(attr_names) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ RequestCriteria
Returns a new instance of RequestCriteria.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/active_public_resources/request_criteria.rb', line 18 def initialize(args={}) args.each do |k,v| if k.to_s == 'sort' && !SORTS.include?(v) raise ArgumentError.new("sort is invalid. Must be in [#{SORTS.join(', ')}]") end if k.to_s == 'content_filter' && !CONTENT_FILTERS.include?(v) raise ArgumentError.new("content_filter is invalid. Must be in [#{CONTENT_FILTERS.join(', ')}]") end instance_variable_set("@#{k}", v) unless v.nil? end end |
Instance Attribute Details
#channel_name ⇒ Object
Returns the value of attribute channel_name.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def channel_name @channel_name end |
#content_filter ⇒ Object
Returns the value of attribute content_filter.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def content_filter @content_filter end |
#folder ⇒ Object
Returns the value of attribute folder.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def folder @folder end |
#page ⇒ Object
Returns the value of attribute page.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def page @page end |
#per_page ⇒ Object
Returns the value of attribute per_page.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def per_page @per_page end |
#query ⇒ Object
Returns the value of attribute query.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def query @query end |
#remote_ip ⇒ Object
Returns the value of attribute remote_ip.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def remote_ip @remote_ip end |
#sort ⇒ Object
Returns the value of attribute sort.
16 17 18 |
# File 'lib/active_public_resources/request_criteria.rb', line 16 def sort @sort end |
Instance Method Details
#attributes ⇒ Object
68 69 70 |
# File 'lib/active_public_resources/request_criteria.rb', line 68 def attributes instance_values end |
#set_default_criteria!(default_criteria) ⇒ Object
62 63 64 65 66 |
# File 'lib/active_public_resources/request_criteria.rb', line 62 def set_default_criteria!(default_criteria) default_criteria.map do |k, v| instance_variable_set("@#{k}", v) unless validate_presence(k) end end |
#validate_presence(attr_name, *attr_names) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/active_public_resources/request_criteria.rb', line 44 def validate_presence(attr_name, *attr_names) attr_names.unshift(attr_name) attr_names.each do |k| if instance_variable_get("@#{k}").blank? return false end end true end |
#validate_presence!(attr_names) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/active_public_resources/request_criteria.rb', line 54 def validate_presence!(attr_names) attr_names.each do |k| if instance_variable_get("@#{k}").blank? raise ArgumentError.new("must include #{attr_names.join(', ')}") end end end |