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

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

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

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
  "def coerce_\#{name}(value)\n\#{CoercionHelpers.method_body_for_type(@type)} unless value.nil?\nend\n"
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>.

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.

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

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

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
  "def \#{name}\[email protected]\#{name} ||= \#{default}\nend\n"
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)
  #   @pages = coerce_pages(value)
  # end
end

Since:

  • 0.0.1


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

def setter_method
  "def \#{name}=(value)\[email protected]\#{name} = \#{coercion_method_name}(value)\nend\n"
end