Class: Lean::Attributes::Attribute Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lean-attributes/attributes/attribute.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents an attribute defined by ClassMethods#attribute

See Also:

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Attribute

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Description of method

Parameters:

  • options (Hash) (defaults to: {})

    {} describe options = {}

Options Hash (options):

  • :default (Object)

    default value or method name as a Symbol

  • :name (#to_sym)

    attribute name

  • :type (#to_s)

    class or class name that the attribute will be

See Also:

Since:

  • 0.0.1



25
26
27
28
29
# File 'lib/lean-attributes/attributes/attribute.rb', line 25

def initialize(options = {})
  @default  = options[:default]
  @name     = options[:name].to_sym
  @type     = options[:type].to_s.to_sym
end

Instance Attribute Details

#nameSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns name of the Attribute.

Returns:

  • (Symbol)

    name of the Attribute

Since:

  • 0.0.1



12
13
14
# File 'lib/lean-attributes/attributes/attribute.rb', line 12

def name
  @name
end

Instance Method Details

#coercion_methodString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a method definition as a String with the name ‘coerce_<attribute>`.

Examples:

class Post
  include Lean::Attributes

  attribute :author, String
  attribute :parent, Post

  # generated_methods:
  # def coerce_author(value)
  #   value.to_s unless value.nil?
  # end

  # def coerce_parent(value)
  #   Post.new(value) unless value.nil?
  # end
end

Returns:

  • (String)

    method definition

See Also:

Since:

  • 0.0.1



55
56
57
58
59
60
61
# File 'lib/lean-attributes/attributes/attribute.rb', line 55

def coercion_method
  <<-EOS
    def coerce_#{name}(value)
      #{CoercionHelpers.method_body_for_type(@type)} unless value.nil?
    end
  EOS
end

#coercion_method_nameString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a method with a name ‘coerce_<attribute>`.

Returns:

  • (String)

    method name

See Also:

Since:

  • 0.0.1



68
69
70
# File 'lib/lean-attributes/attributes/attribute.rb', line 68

def coercion_method_name
  "coerce_#{name}"
end

#defaultObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If the configured default is a Symbol but the intended type for this attribute is anything but, interpret the default as a method name to which we will ‘send`. Otherwise, just use the configured default as a String.

Returns:

  • (Object)

    configured default

See Also:

Since:

  • 0.1.0



82
83
84
85
86
87
88
# File 'lib/lean-attributes/attributes/attribute.rb', line 82

def default
  if @default.is_a?(Symbol) && @type != :Symbol
    return "send(:#{@default})"
  end

  @default.inspect
end

#getter_methodString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a getter method definition if the Attribute has a default, or we just use the straightforward ‘attr_reader :attribute_name`.

Examples:

class Post
  include Lean::Attributes

  attribute :author,        String
  attribute :replies_count, Integer, default: 0

  # generated attr_reader:
  # attr_reader :author

  # generated getter with default
  # def replies_count
  #  @replies_count ||= 0
  # end
end

Returns:

  • (String)

    description of returned object

See Also:

Since:

  • 0.0.1



112
113
114
115
116
# File 'lib/lean-attributes/attributes/attribute.rb', line 112

def getter_method
  return getter_method_with_default unless @default.nil?

  "attr_reader :#{@name}"
end

#getter_method_with_defaultString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a getter method definition that lazily sets a default.

Examples:

class Post
  include Lean::Attributes

  attribute :created_at,    Time,     default: :default_created_at
  attribute :replies_count, Integer,  default: 0

  # generated methods:
  # def created_at
  #   @first_name ||= send(:default_created_at)
  # end

  # def replies_count
  #   @replies_count ||= 0
  # end
end

Returns:

  • (String)

    method definition that sets default

See Also:

Since:

  • 0.0.1



141
142
143
144
145
146
147
# File 'lib/lean-attributes/attributes/attribute.rb', line 141

def getter_method_with_default
  <<-EOS
    def #{name}
      @#{name} ||= #{default}
    end
  EOS
end

#setter_methodString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates a setter method definition that coerces values to the configured type if necessary.

Examples:

class Book
  include Lean::Attributes

  attribute :pages, Integer

  # generated method:
  # def pages=(value)
  #  value = coerce_pages(value)
  #  @pages = value
  # end
end

Returns:

  • (String)

    method definition

Since:

  • 0.0.1



166
167
168
169
170
171
172
173
# File 'lib/lean-attributes/attributes/attribute.rb', line 166

def setter_method
  <<-EOS
    def #{name}=(value)
      value = #{coercion_method_name}(value)
      @#{name} = value
    end
  EOS
end