Module: RestPack::Serializer::Attributes::ClassMethods
- Defined in:
- lib/restpack_serializer/serializable/attributes.rb
Instance Method Summary collapse
- #add_to_serializable(name, options = {}) ⇒ Object
- #attribute(name, options = {}) ⇒ Object
- #attributes(*attrs) ⇒ Object
- #define_attribute_method(name) ⇒ Object
- #define_include_method(name) ⇒ Object
- #serializable_attributes ⇒ Object
- #transform(attrs = [], transform_lambda) ⇒ Object
- #transform_attribute(name, transform_lambda, options = {}) ⇒ Object
Instance Method Details
#add_to_serializable(name, options = {}) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 58 def add_to_serializable(name, = {}) [:key] ||= name.to_sym @serializable_attributes ||= {} @serializable_attributes[[:key]] = name end |
#attribute(name, options = {}) ⇒ Object
31 32 33 34 35 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 31 def attribute(name, ={}) add_to_serializable(name, ) define_attribute_method name define_include_method name end |
#attributes(*attrs) ⇒ Object
13 14 15 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 13 def attributes(*attrs) attrs.each { |attr| attribute attr } end |
#define_attribute_method(name) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 37 def define_attribute_method(name) unless method_defined?(name) define_method name do value = self.default_href if name == :href value ||= @model.send(name) value = value.to_s if name == :id value end end end |
#define_include_method(name) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 48 def define_include_method(name) method = "include_#{name}?".to_sym unless method_defined?(method) define_method method do @context[method].nil? || @context[method] end end end |
#serializable_attributes ⇒ Object
9 10 11 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 9 def serializable_attributes @serializable_attributes end |
#transform(attrs = [], transform_lambda) ⇒ Object
17 18 19 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 17 def transform(attrs = [], transform_lambda) attrs.each { |attr| transform_attribute(attr, transform_lambda) } end |
#transform_attribute(name, transform_lambda, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/restpack_serializer/serializable/attributes.rb', line 21 def transform_attribute(name, transform_lambda, = {}) add_to_serializable(name, ) define_method name do transform_lambda.call(name, @model) end define_include_method name end |