Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 45833b9529
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 348 lines (238 sloc) 6.645 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
NAME
  fattr.rb

INSTALL
  gem install fattrs

URIS
  http://github.com/ahoward/fattr
  http://rubyforge.org/projects/codeforpeople/
  http://codeforpeople.com/

SYNOPSIS
  fattr.rb is a "fatter attr" for ruby

  the implementation of fattr.rb borrows many of the best ideas from the
  metakoans.rb ruby quiz

    http://www.rubyquiz.com/quiz67.html

  in particular the solutions of Christian Neukirchen and Florian Gross along
  with concepts from my original traits.rb lib

  key features provided by fattrs are

    - ability to specify default values for attrs and definition time. values
      can be literal objects or blocks, which are evaluated in the context of
      self to initialize the variable

    - classes remember which fattrs they've defined and this information is
     available to client code

    - a whole suite of methods is defined by calls to #fattrs including
     getter, setter, query (var?) and banger (var! - which forces
     re-initialization from the default value/block)

    - ability to define multiple fattrs at once using key => value pairs

    - fast lookup of whether or not a class has defined a certain fattr

    - fattrs can be defined on objects on a per singleton basis

    - getters acts as setters if an argument is given to them

    - block caching, calling an fattr with a block sets the instance
      variable to that block

    - shortcuts for adding class/module level fattrs

    - class inheritable attributes

  all this in 156 lines of code

SAMPLES

  <========< samples/a.rb >========>

  ~ > cat samples/a.rb

    #
    # basic usage is like attr, but note that fattr defines a suite of methods
    #
      require 'fattr'
    
      class C
        fattr 'a'
      end
    
      c = C.new
    
      c.a = 42
      p c.a #=> 42
      p 'forty-two' if c.a? #=> 'forty-two'
    
    #
    # fattrs works on object too
    #
      o = Object.new
      o.fattr 'answer' => 42
      p o.answer #=> 42

  ~ > ruby samples/a.rb

    42
    "forty-two"
    42


  <========< samples/b.rb >========>

  ~ > cat samples/b.rb

    #
    # default values may be given either directly or as a block which will be
    # evaluated in the context of self. in both cases (value or block) the
    # default is set only once and only if needed - it's a lazy evaluation. the
    # 'banger' method can be used to re-initialize a variable at any point whether
    # or not it's already been initialized.
    #
      require 'fattr'
    
      class C
        fattr :a => 42
        fattr(:b){ Float a }
      end
    
      c = C.new
      p c.a #=> 42
      p c.b #=> 42.0
    
      c.a = 43
      p c.a #=> 43
      c.a!
      p c.a #=> 42

  ~ > ruby samples/b.rb

    42
    42.0
    43
    42


  <========< samples/c.rb >========>

  ~ > cat samples/c.rb

    #
    # multiple name=>default pairs can be given
    #
      require 'fattr'
    
      class C
        fattrs 'x' => 0b101000, 'y' => 0b10
      end
    
      c = C.new
      z = c.x + c.y
      p z #=> 42

  ~ > ruby samples/c.rb

    42


  <========< samples/d.rb >========>

  ~ > cat samples/d.rb

    #
    # a nice feature is that all fattrs are enumerated in the class. this,
    # combined with the fact that the getter method is defined so as to delegate
    # to the setter when an argument is given, means bulk initialization and/or
    # fattr traversal is very easy.
    #
      require 'fattr'
    
      class C
        fattrs %w( x y z )
    
        def fattrs
          self.class.fattrs
        end
    
        def initialize
          fattrs.each_with_index{|a,i| send a, i}
        end
    
        def to_hash
          fattrs.inject({}){|h,a| h.update a => send(a)}
        end
    
        def inspect
          to_hash.inspect
        end
      end
    
      c = C.new
      p c.fattrs
      p c
    
      c.x 'forty-two'
      p c.x

  ~ > ruby samples/d.rb

    ["x", "y", "z"]
    {"x"=>0, "y"=>1, "z"=>2}
    "forty-two"


  <========< samples/e.rb >========>

  ~ > cat samples/e.rb

    #
    # my favourite element of fattrs is that getters can also be setters.
    # this allows incredibly clean looking code like
    #
      require 'fattr'
    
      class Config
        fattrs %w( host port)
        def initialize(&block) instance_eval &block end
      end
    
      conf = Config.new{
        host 'codeforpeople.org'
        port 80
      }
    
      p conf

  ~ > ruby samples/e.rb

    #<Config:0x2cd1c @port=80, @host="codeforpeople.org">


  <========< samples/f.rb >========>

  ~ > cat samples/f.rb

    #
    # of course fattrs works as well at class/module level as at instance
    # level
    #
      require 'fattr'
    
      module Logging
        Level_names = {
          0 => 'INFO',
          # ...
          42 => 'DEBUG',
        }
    
        class << Logging
          fattr 'level' => 42
          fattr('level_name'){ Level_names[level] }
        end
      end
    
    p Logging.level
    p Logging.level_name

  ~ > ruby samples/f.rb

    42
    "DEBUG"


  <========< samples/g.rb >========>

  ~ > cat samples/g.rb

    #
    # you can add class/module fattrs the 'normal' way or using the provided
    # shortcut method
    #
      require 'fattr'
    
      class C
        class << C
          fattr 'a' => 4
        end
    
        Fattr 'b' => 2
      end
    
      p [ C.a, C.b ].join

  ~ > ruby samples/g.rb

    "42"


  <========< samples/h.rb >========>

  ~ > cat samples/h.rb

    #
    # class variable inheritance is supported simply
    #
      require 'fattr'
    
      class A
        Fattr :x, :default => 42, :inheritable => true
      end
    
      class B < A
      end
    
      class C < B
      end
    
      p C.x #=> 42
    
      A.x = 42.0
      B.x = 'forty-two'
    
      p A.x #=> 42.0
      p B.x #=> 'forty-two'
      p C.x #=> 42
    
      C.x! # re-initialize from closest ancestor (B)
    
      p A.x #=> 42.0
      p B.x #=> 'forty-two'
      p C.x #=> 'forty-two'

  ~ > ruby samples/h.rb

    42
    42.0
    "forty-two"
    42
    42.0
    "forty-two"
    "forty-two"



HISTORY
  2.0.0:
    support class/module inheritable attributes

  1.1.0:
    ruby19 testing. move to github.

  1.0.2:
    added Fattr shortcut for adding class/module level fattrs

      class C
        Fattr 'children' => []

        def C.inherited other
          (children << other).uniq!
          super
        end
      end

      class B < C
      end

      p C.children #=> B

  1.0.0:
    port from attributes.rb retaining all the same features of that version of
    attributes.rb
Something went wrong with that request. Please try again.