Sha256: 91c4c377439f2d03c16d7a6366eb2a631e6b9a9e496c70ba8659a4fac5767eea

Contents?: true

Size: 1.57 KB

Versions: 36

Compression:

Stored size: 1.57 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"
          methq = :"#{meth}?"

          # 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

36 entries across 36 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.20 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.19 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.18 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.17 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.16 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.15 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.14 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.13 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.12 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.11 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.10 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.9 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.8 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.7 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.6 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.5 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.4 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.3 lib/eco/data/locations/node_diff/accessors.rb
eco-helpers-3.0.2 lib/eco/data/locations/node_diff/accessors.rb