Module: AttributeExt::HiddenAttributes
- Defined in:
- lib/attribute_ext/hidden_attributes.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#as_json_with_hidden_attrs(options = nil, &block) ⇒ Object
:nodoc:.
-
#hidden_attribute_names(format, options = {}) ⇒ Object
Returns an array with attributes to hide from serialization.
-
#serializable_hash_with_hidden_attrs(options = nil) ⇒ Object
:nodoc:.
-
#to_xml_with_hidden_attrs(options = nil, &block) ⇒ Object
:nodoc:.
Class Method Details
.included(base) ⇒ Object
:nodoc:
3 4 5 6 7 8 |
# File 'lib/attribute_ext/hidden_attributes.rb', line 3 def self.included(base) # :nodoc: base.extend(ClassMethods) base.alias_method_chain :to_xml, :hidden_attrs base.alias_method_chain :as_json, :hidden_attrs base.alias_method_chain :serializable_hash, :hidden_attrs end |
Instance Method Details
#as_json_with_hidden_attrs(options = nil, &block) ⇒ Object
:nodoc:
78 79 80 81 82 83 84 |
# File 'lib/attribute_ext/hidden_attributes.rb', line 78 def as_json_with_hidden_attrs( = nil, &block) # :nodoc: ||= {} [:except] = hidden_attribute_names(:json, ) [:hidden_attributes_format] = :json as_json_without_hidden_attrs() end |
#hidden_attribute_names(format, options = {}) ⇒ Object
Returns an array with attributes to hide from serialization.
This method should only be used to test own rules without need to run a formatter and validate the generated output. See AttributeExt specs for details.
hidden_attribute_names :format, :options => :hash
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/attribute_ext/hidden_attributes.rb', line 100 def hidden_attribute_names(format, = {}) if [:except].is_a?(Array) names = [:except] else names = [] names << [:except] if [:except] end self.class.hide_attributes.collect do |attrs, opts| next unless opts[:only].empty? or opts[:only].include?(format) next unless opts[:except].empty? or !opts[:except].include?(format) next unless opts[:if].nil? or hidden_attr_call_block(opts[:if], format, ) next unless opts[:unless].nil? or !hidden_attr_call_block(opts[:unless], format, ) names += attrs.collect(&:to_s) end names.uniq end |
#serializable_hash_with_hidden_attrs(options = nil) ⇒ Object
:nodoc:
86 87 88 89 90 91 |
# File 'lib/attribute_ext/hidden_attributes.rb', line 86 def serializable_hash_with_hidden_attrs( = nil) # :nodoc: ||= {} [:except] = hidden_attribute_names(([:hidden_attributes_format] || :hash), ) serializable_hash_without_hidden_attrs() end |
#to_xml_with_hidden_attrs(options = nil, &block) ⇒ Object
:nodoc:
70 71 72 73 74 75 76 |
# File 'lib/attribute_ext/hidden_attributes.rb', line 70 def to_xml_with_hidden_attrs( = nil, &block) # :nodoc: ||= {} [:except] = hidden_attribute_names(:xml, ) [:hidden_attributes_format] = :xml to_xml_without_hidden_attrs() end |