class Kartograph::Property

Attributes

map[R]
name[R]
options[R]

Public Class Methods

new(name, options = {}, &block) click to toggle source
# File lib/kartograph/property.rb, line 5
def initialize(name, options = {}, &block)
  @name = name
  @options = options

  if mapped_class = options[:include]
    # Perform a safe duplication into our properties map
    # This allows the user to define more attributes on the map should they need to
    @map = mapped_class.kartograph.dup
  end

  if block_given?
    @map ||= Map.new
    block.arity > 0 ? block.call(map) : map.instance_eval(&block)
  end

  @artist = Artist.new(@map)
end

Public Instance Methods

==(other) click to toggle source
# File lib/kartograph/property.rb, line 59
def ==(other)
  %i(name options map).inject(true) do |equals, method|
    break unless equals
    send(method) == other.send(method)
  end
end
dup() click to toggle source
# File lib/kartograph/property.rb, line 53
def dup
  Property.new(name, options.dup).tap do |property|
    property.map = map.dup if self.map
  end
end
key() click to toggle source
# File lib/kartograph/property.rb, line 23
def key
  @key ||= (options[:key] || name).to_s.freeze
  @key
end
map=(map) click to toggle source
# File lib/kartograph/property.rb, line 28
def map=(map)
  @map = map
  @artist = Artist.new(@map)
end
plural?() click to toggle source
# File lib/kartograph/property.rb, line 49
def plural?
  !!options[:plural]
end
scopes() click to toggle source
# File lib/kartograph/property.rb, line 45
def scopes
  Array(options[:scopes] || [])
end
value_for(object, scope = nil) click to toggle source
# File lib/kartograph/property.rb, line 33
def value_for(object, scope = nil)
  value = object.send(name)
  return if value.nil?
  map ? artist_value(value, scope) : value
end
value_from(object, scope = nil) click to toggle source
# File lib/kartograph/property.rb, line 39
def value_from(object, scope = nil)
  return if object.nil?
  value = object.has_key?(key) ? object[key] : object[key.to_sym]
  map ? sculpt_value(value, scope) : value
end

Private Instance Methods

artist_value(value, scope) click to toggle source
# File lib/kartograph/property.rb, line 72
def artist_value(value, scope)
  plural? ? Array(value).map {|v| @artist.draw(v, scope) } : @artist.draw(value, scope)
end
sculpt_value(value, scope) click to toggle source
# File lib/kartograph/property.rb, line 68
def sculpt_value(value, scope)
  plural? ? Array(value).map {|v| Sculptor.new(v, map).sculpt(scope) } : Sculptor.new(value, map).sculpt(scope)
end