Sha256: 393ac8a7fc0d07d740ae20c053a08210f0c2d350df9aaef4f58b605ab46969f1

Contents?: true

Size: 1.2 KB

Versions: 17

Compression:

Stored size: 1.2 KB

Contents

module Shamu
  module Attributes

    # Add  methods to a class to make it easy to build out an object using fluid
    # assignment.
    #
    # @example
    #
    #   # Without fluid
    #   obj = AttributeObject.new
    #   obj.name = '...'
    #   obj.email = '...'
    #
    #   # With fluid
    #   obj = FluidObject.new
    #   obj.name( '...' )
    #      .email( '...' )
    module FluidAssignment
      extend ActiveSupport::Concern

      included do |base|
        raise "Must include Shamu::Attributes first." unless base < Shamu::Attributes
        raise "Must include Shamu::Attributes::Assignment first." unless base < Shamu::Attributes::Assignment
      end

      # DSL for declaring fluid assignment.
      class_methods do

        private

          def define_attribute_reader( name, ** )
            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{ name }( *args )
                if args.length > 0
                  assign_#{ name }( *args )
                  self
                else
                  return @#{ name } if defined? @#{ name }
                  @#{ name } = fetch_#{ name }
                end
              end
            RUBY
          end

      end

    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
shamu-0.0.24 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.21 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.20 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.19 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.18 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.17 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.15 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.14 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.13 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.11 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.9 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.8 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.7 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.5 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.4 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.3 lib/shamu/attributes/fluid_assignment.rb
shamu-0.0.2 lib/shamu/attributes/fluid_assignment.rb