yammy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Gemfile +3 -0
  2. data/lib/yammy.rb +77 -0
  3. data/rspec/yammy_spec.rb +0 -0
  4. metadata +50 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport-inflector", "~> 0.1.0"
data/lib/yammy.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Yammy
4
+
5
+ def has_many model, opts = {}
6
+ field = opts[:as] || model.to_s.downcase.pluralize
7
+ with = opts[:with] || nil
8
+ model = model.to_s.singularize
9
+ class_eval do
10
+ define_method field do
11
+ fields = instance_variable_get "@#{field}"
12
+ collection_from_array fields, model, with
13
+ end
14
+ end
15
+ end
16
+
17
+ class Base
18
+
19
+ extend Yammy
20
+
21
+ attr_accessor :name
22
+
23
+ def initialize data={}
24
+ set_defaults
25
+ data.each do |k,v|
26
+ accessor = "#{k}="
27
+ if respond_to? accessor
28
+ self.send accessor, v
29
+ else
30
+ instance_variable_set "@#{k}", v
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.class
36
+ self
37
+ end
38
+
39
+ def self.root_path
40
+ Rails.root.join Rails.application.config.yammy_content_dir
41
+ end
42
+
43
+ def self.relative_path
44
+ self.class.name.to_s.downcase.pluralize
45
+ end
46
+
47
+ def self.find path
48
+ base_path
49
+ end
50
+
51
+ def self.relative_path= p
52
+ @@relative_path = p.to_s
53
+ end
54
+
55
+ def set_defaults
56
+ end
57
+
58
+ def self.load file
59
+ data = YAML.load_file root_path.join relative_path, file.to_s+'.yml'
60
+ data['name'] = file
61
+ self.new data
62
+ end
63
+
64
+ def collection_from_array arr, model, extra_key = nil
65
+ # Return an array of all the references for a thing.
66
+ arr.map do |d|
67
+ id = d.instance_of?(Array) ? d[0] : d
68
+ obj = model.to_s.capitalize.constantize.load id
69
+ obj.send("name=", id)
70
+ obj.send(extra_key.to_s+"=", d[1]) if extra_key
71
+ obj
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ end
File without changes
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yammy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michelle Steigerwalt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Stop abusing i18n! Yammy provides a simple YAML-based way for nontechnical
15
+ stakeholders to drop content and static data into your code's repository.
16
+ email: msteigerwalt@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/yammy.rb
22
+ - rspec/yammy_spec.rb
23
+ - Gemfile
24
+ homepage: http://github.com/Yuffster/yammy
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project: yammy
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Stop abusing i18n! Yammy provides a simple YAML-based way for nontechnical
48
+ stakeholders to drop content and static data into your code's repository.
49
+ test_files: []
50
+ has_rdoc: