Sha256: a57837454570e017e26986b1939af95ac5a7e81c28ffbd333b4e54ccb89affdf

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

class Eco::Data::Locations::NodeDiff
  module Accessors
    class << self
      def included(base)
        super(base)
        base.extend Eco::Language::Models::ClassHelpers
        base.extend ClassMethods
        base.inheritable_class_vars :exposed_attrs
      end
    end

    module ClassMethods
      include Eco::Data::Strings::SnakeCase

      # Creates the defined accessor attributes against `NodeDiff`
      # It also creates a method with question mark that evaluates true if **any** the attr changed.
      # @note the defined attributes are expected to be the keys within
      #   the source Hashes that are being compared.
      # @note accessing `src_1` (prev) attributes, will have it's method as `prev_[attrName]`
      def attr_expose(*attrs)
        attrs.each do |attr|
          meth  = attr.to_sym
          methp = "#{meth}_prev".to_sym
          methq = "#{meth}?".to_sym

          # current value
          define_method meth do
            attr(meth)
          end
          alias_method snake_case(meth), meth

          # prev value
          define_method methp do
            attr_prev(meth)
          end
          alias_method snake_case(methp), methp

          # has it changed?
          define_method methq do
            diff_attr?(meth)
          end
          alias_method snake_case(methq), methq

          @exposed_attrs = (@exposed_attrs || []) | [meth]
        end
      end

      # Keeps track on what attributes have been exposed.
      def exposed_attrs
        @exposed_attrs ||= []
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eco-helpers-2.7.4 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-2.7.2 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-2.7.1 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-2.7.0 lib/eco/data/locations/node_diff/accessors.rb