Sha256: 636ff1a02e6946a3d29a605ef97de71dce017deb9e91858a2beeeaa5772573b0

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module RepoManager

  module AssetAccessors

    # Given an array, create accessors
    # NOTE: This is similar to using method_missing with a whitelist
    #
    # @return [void]
    def create_accessors(*attrs)
      return unless attrs
      raise ArgumentError, "Expected 'user_attributes' to be an array" unless attrs.is_a? Array

      # Define each of the attributes
      attrs.flatten.each do |attr|
        create_accessor(attr)
      end
    end

    def create_accessor(attr)
      create_reader(attr)
      create_writer(attr)
    end

    def create_reader(attr)
      return unless attr

      method = "#{attr}".to_sym

      if self.kind_of? RepoManager::BaseAsset
        return if self.respond_to? method

        self.class.send(:define_method, method) do
          render(attributes[method])
        end
      else
        return if respond_to? method

        define_method(method) do
          render(attributes[method])
        end
      end
    end

    def create_writer(attr)
      return unless attr

      method = "#{attr}=".to_sym

      if self.kind_of? RepoManager::BaseAsset
        return if self.respond_to? method

        self.class.send(:define_method, method) do |value|
          attributes[attr] = value
        end
      else
        return if respond_to? method

        define_method(method) do |value|
          attributes[attr] = value
        end
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
repo_manager-0.7.3 lib/repo_manager/assets/asset_accessors.rb
repo_manager-0.7.2 lib/repo_manager/assets/asset_accessors.rb
repo_manager-0.7.1 lib/repo_manager/assets/asset_accessors.rb