Sha256: 29aeb3c99870a325c05bbb6bd8134444a2c91289a003a3924667e7db28b913fa

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Hari
  class Entity
    class Property
      module Builder

        def property(name, options = {})
          attr_reader name
          define_attribute_method name

          validates_presence_of name if options[:required]

          define_method "#{name}=" do |value|
            unless send(name) == value
              send "#{name}_will_change!"
            end

            instance_variable_set "@#{name}", value
          end

          if options[:type] == Serialization::Boolean
            define_method("#{name}?") { send name }
          end

          properties << Property.new(self, name, options)
        end

        def properties(*args)
          options = args.extract_options!
          args.each { |name| property name, options }

          @properties ||= begin
            if self == Hari::Entity
              []
            else
              entities_ancestors = ancestors.select do |a|
                a.ancestors.include? Hari::Entity
              end

              entity = entities_ancestors[1]
              entity ? entity.properties.dup : []
            end
          end
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.5 lib/hari/entity/property/builder.rb