Module: LazyAsJson::AttributeFilter::OnlyKeysFilter
- Defined in:
- lib/lazy_as_json/attribute_filter.rb
Constant Summary collapse
- BANNED_KEYS =
The list of excluded attribute accessor name
[:all, :delete, :delete_all, :destroy_all, :destroy, :update, :save, :create, :create!, :save!, :update_attributes, :update_attribute, :update_attributes!, :find, :where, :increment, :increment!, :decrement, :decrement!, :remove, :object_id]
Instance Method Summary collapse
-
#__allowed_attribute_key?(key) ⇒ Boolean
Return true if the specified attribute key is not banned.
-
#__lazy_mode? ⇒ Boolean
Returns true if the lazy mode is enabled through :only_keys option.
-
#__only_keys ⇒ String
Returns the applied attribute filtering string.
-
#__set_only_keys(key_str) ⇒ Object
Sets the only keys, if you intend to use those directly instead of going through :as_json.
-
#as_json(opts = {}) ⇒ Hash
Returns the JSON serializable hash object with the data from the specified keys.
-
#build_as_json ⇒ Hash
Builds the attributes hash object based on the applied attribute filtering string.
-
#lazy_as_json(opts = {}) ⇒ Hash
Creates a Hash object which lazly assembles attributes from the object or any of their parents.
Instance Method Details
#__allowed_attribute_key?(key) ⇒ Boolean
Return true if the specified attribute key is not banned
88 89 90 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 88 def __allowed_attribute_key?(key) !BANNED_KEYS.include?(key) end |
#__lazy_mode? ⇒ Boolean
Returns true if the lazy mode is enabled through :only_keys option
58 59 60 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 58 def __lazy_mode? __only_keys && !__only_keys.empty? end |
#__only_keys ⇒ String
Returns the applied attribute filtering string
41 42 43 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 41 def __only_keys @__only_keys end |
#__set_only_keys(key_str) ⇒ Object
Sets the only keys, if you intend to use those directly instead of going through :as_json
33 34 35 36 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 33 def __set_only_keys(key_str) @__only_keys = key_str.to_s.strip @__only_keys = @__only_keys.empty? ? nil : @__only_keys end |
#as_json(opts = {}) ⇒ Hash
Returns the JSON serializable hash object with the data from the specified keys.
It Looks up for :only_keys for enabling attribute throttling, in case of absense it returns the actual data from overrode 'as_json' method.
24 25 26 27 28 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 24 def as_json(opts = {}) __set_only_keys opts.delete(:only_keys) __lazy_mode? ? build_as_json : super(opts) end |
#build_as_json ⇒ Hash
Builds the attributes hash object based on the applied attribute filtering string
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 65 def build_as_json @_af_references = {} hash = lazy_as_json keys = [] __only_keys.split(',').each do |key| value = hash for nested_key in find_full_key(key) if Array === value value = value.map { |v| v[nested_key] }.compact elsif Hash === value value = value[nested_key] end end end hash end |
#lazy_as_json(opts = {}) ⇒ Hash
Creates a Hash object which lazly assembles attributes from the object or any of their parents. However, it could be delegated from another object through exposing :source method.
49 50 51 52 53 |
# File 'lib/lazy_as_json/attribute_filter.rb', line 49 def lazy_as_json(opts = {}) Hash.new do |hash, key| hash[key] = _with_lazy(_find_value(key)) end end |