Sha256: 7777a2f7bc4266fac820a89e1bb18d7817b62ccef393aca00233acceeebb3a66

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

##
# NPR::Concern::ShallowAttributes
#
# A "shallow attribute" is one that only contains the "$text"
# node. Use this class method to define which attributes those
# are.
#
# Example:
#
#   { "link": { "$text": "http://npr.org" } }
#
#   shallow_attribute "link"
#   @story.link # => "http://npr.org"
module NPR
  module Concern
    module ShallowAttributes
      include AttrTypecast

      def self.included(base)
        base.extend ClassMethods
      end

      #-----------------

      module ClassMethods
        def shallow_attribute(*attrs)
          _shallow_attributes.push *attrs
          attr_accessor *attrs
        end

        #-----------------

        def _shallow_attributes
          @shallow_attributes ||= []
        end

        #-----------------

        private
        attr_writer :_shallow_attributes
      end

      #-----------------

      private

      #-----------------
      # Extract the defined shallow_attributes from the JSON
      # and set the corresponding attribute.
      #
      # If the JSON doesn't contain a key for an attribute,
      # then attribute will remain at its default state (nil).
      #
      # Arguments: Hash of the JSON for this object
      #
      # Example:
      #
      #   def initialize(json)
      #     extract_shallow_attributes(json)
      #   end
      #
      def extract_shallow_attributes(json)
        self.class._shallow_attributes.each do |key|
          if node = json[key]
            send "#{key}=", attr_typecast(key, node["$text"].to_s)
          end
        end
      end
    end # ShallowAttribute
  end # Concern
end # NPR

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
npr-3.0.0 lib/npr/concern/shallow_attributes.rb
npr-2.0.2 lib/npr/concern/shallow_attributes.rb
npr-2.0.1 lib/npr/concern/shallow_attributes.rb
npr-2.0.0 lib/npr/concern/shallow_attributes.rb
npr-1.2.0 lib/npr/concern/shallow_attributes.rb